bismark
Phoenix Socket draining
We’re attempting to make our deployment process a little less… disruptive, so we’d like to find a way of manually draining all open websockets.
Background
We’re running on AWS autoscaling groups with an Application Load Balancer. When we deploy, we register the a new instance with the load balancer target group, then deregister the old one. Unfortunately, target groups deregistration is ignorant of websockets, so it waits the 300s for all open connections to complete (which of course the websockets do not do), and then forcefully closes all of the connections. This causes a bit of a stampede (yes, yes, this is partly client issue, let’s just assume the clients are bad actors) and we much prefer to do this more gently.
Question
Is there any “official” way of traversing the open websockets and closing them down? Aside from spelunking through the Supervision tree that is.
Though I’m open to hearing about other options
.
Most Liked
bismark
We’ve come up with a not-so-great workaround for now.
SocketDrano had a couple issues for us:
- it relied on channel join telemetry events, so it would miss sockets that do not join a channel
- it was oriented around draining upon sigterm, but we’re relying on CodeDeploy lifecycle events for triggering the draining
So, we ended up “extending” Phoenix.Socket by creating our own Socket module that has overrideable callbacks:
Then we create a custom init callback:
@impl Phoenix.Socket.Transport
def init(state) do
super(state)
|> tap(fn
{:ok, {_state, %Phoenix.Socket{transport_pid: pid}}} when is_pid(pid) ->
track_socket(pid)
_ ->
:ok
end)
end
track_socket then does essentially what SocketDrano does (monitor the socket pids, disconnects them when told to).
I’ve opened a thread on the Phoenix Core mailing list asking about better solutions being added Phoenix itself: https://groups.google.com/g/phoenix-core/c/1umhh2X1oAM/m/_sfxU8xIBgAJ
mweidner
For folks coming across this recently: Phoenix sockets now have built-in connection draining (since 1.7.2). You can configure it with the drainer option to socket/3: Phoenix.Endpoint — Phoenix v1.8.8
rjk
Presence.track can store key/values so you could store hostname and userid? So on a quit signal do a lookup on hostname and you have a list of connected sockets for that node? And you could use Phoenix.PubSub — Phoenix.PubSub v2.2.0 to keep the broadcast local to the node.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








