hey guys, quick question. I just started using the components
framework from Stuart Sierra and the reload
workflow. One thing that I still dont understand is that for development he recommends to use alter-var-root
. Why not an atom
together with swap!
. I thought that is the recommended way. Is there any benefit in using vars over atoms?
I’ll take a stab at it. I don’t think it really matters? I’ve used both techniques. Using a var lets you access the system without having to deref which is a convenience, but I think the larger point may be: atoms are for coordinating access to mutable entities, while vars are for accessing constant values. During a given run, most systems are going to be constant.
@carocad atoms are for things that are changed programmatically, for something that is only redefined explicitly in a human entered top level command, a var is fine
ah ok. I was already doubting my Clojure knowledge 😄. Thanks guys
on a related topic. Is it necessary or encourage to call stop
on production to die gracefully? for example: put a hook to SIGTERM and call stop
before exiting? or the JVM takes care of everything automatically?
https://stackoverflow.com/questions/2975248/how-to-handle-a-sigterm
Do you have any invariants not enforced automatically by the os and vm on shutdown?
Eg. membership in some distributed system that you prefer to explicitly exit rather than letting it auto timeout, or tracking of incomplete state
Also, regarding using an atom, don't do that if system startup functions have side effects, consider what would happen in retries (likely stealing a needed http port, or leaving duplicate threadpools or go blocks running)
@noisesmith yeah exactly. My question was precisely in that direction. I checked the swap!
docs and it said that f
must be free of side effects which got me wonder about it since the whole system gets shut down so I was not sure if it would really have an impact
good to know 🙂
regarding shutdown. No, not so far. But I just started using component and its workflow but I couldnt find any references to using stop
on production which got me wondering about it. But yeah I guess for most cases the OS/VM should handle that automatically. I will keep an eye on this that might need to be closed manually anyway
oh, right, if your f passed to swap! did a shutdown then start, you would just have redundant restarts but you wouldn't lose resources