#How do Agents work?

29 messages · Page 1 of 1 (latest)

junior viper
#

I've been trying to learn Elixir today. Maybe I'm just misunderstanding the whole threading thing but I don't quite see how Agents work. They are explained to be a simple abstraction around state, allowing you to safely access state from different processes. When calling start_link you usually get a PID to an agent process, right? Does that mean you hand this PID around to every place that has to access this particular instance of state? And you can "start" multiple "links" which are all separate instances of that particular kind of Agent, but they're separate from each other?

The whole promise of thread/process safety comes from how this is inherently based on passing messages around if I am understanding this correctly?

strong nacelle
#

It sounds like you understand

#

You can also register the agent under a name if you don’t want to pass a pid

#

Agents handle a single request at a time like every process, so you can have multiple consumers/clients for a single agent and not need to worry about corrupting state

junior viper
#

How is a pid like this usually passed? If I were to, say, have an agent that stores some common configuration I want separate workers to make use of, do I pass the pid to every worker when I create them?

#

and as long as there's a reference to that pid somewhere the agent isn't GC'd?

strong nacelle
#

The agent doesnt ever get GCed

#

If it’s stopped or linked to something that exists (start_link) its lifetime is a attached to that process, or you can start it in a supervisor

junior viper
#

Where/how would you pass the pid around?

#

when I initialize my other genservers/agents?

strong nacelle
tidal wagon
#

This might also be where process registries come in. A process registry is a place you can store your pids and then find them again later by some name or key. The process registry is then typically named so it's easy to access.

junior viper
#

ohhh

#

that looks very interesting

#

especially the dispatch stuff

#

thanks for pointing that out!

thorn epoch
#

start with genserver. I rarely (if ever) use agent

#

Agent is supposed to be simpler but I think it does itself no favours helping newcomers with all the anonymous functions.

junior viper
#

There's no benefit to using Agents for shared state?

thorn epoch
#

an Agent is a GenServer in a fancy dress

junior viper
#

Also, what if e.g. I store some stuff in an Mnesia db and want to give separate workers access to that. Would I be handing over a handle to the db itself or would I need a genserver/agent to wrap around it?

thorn epoch
#

no idea. just default to postgres whenever you need a db.

junior viper
#

I was told that mnesia should be a good pick for impermanent data that I want to have organized in something more than a map e.g.

#

that was my train of thought

thorn epoch
#

your options are endless. start with a genserver state and move up from there

junior viper
#

Er, considering how "endless" these option seem to be... Where would I go to find out what my options are? e.g. I wouldn't have known about Registry if it wasn't for me asking here. Should I read through everything on the hex docs?

thorn epoch
#

yes, and registry is in the standard library so you would have found it eventually im sure.

However im more talking about how you use these things rather than what you use, for example. you could keep all state in a single GenServer. which may be fine up to 100 or 1000 users depending on what they are doing.

Now say your app is a single player game and that GenServer becomes a bottleneck. You may instead decide to spawn a GenServer for each user to keep their own game state.

Or if its a multiplayer game you might spawn a GenServer for each 'game' thats currently being played and multiple users talk to each GenServer...and somewhere else in your supervision tree there might be a central 'global scoreboard' GenServer that each game sends its final score to at the end of each game.

or if its an MMO where tens of thousands of users need to access the same state. you might need to start thinking about :ets or something else... or you might not, note that every discord server is a GenServer... to give you an idea of what they can handle.