Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpcserver: Convert ws client lifecycle to context. #3025

Merged
merged 3 commits into from
Nov 28, 2022

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Nov 27, 2022

This requires #3024.

This modifies the lifecycle of websocket clients to use the expected pattern for running subsystems based on contexts.

In particular, this replaces the Start and WaitForShutdown methods with a single method named Run and arranges for it to block until the provided context is cancelled. This is more flexible for the caller since it can easily turn blocking code into async code while the reverse is not true.

The new Run method waits for all goroutines that it starts to shutdown before returning to help ensure an orderly shutdown.

Since there are exported methods that send messages to the output and notification handler goroutines via channels and those goroutines are stopped during the shutdown process, all sends select across both the channels in question as well as a quit channel which is closed when the context it cancelled. This ensures callers can't end up blocking on send to a goroutine that is no longer running without needing additional mutexes.

It also contains a couple of other minor consistency cleanup commits.

This is a minor change to only cast the request method once when parsing
a JSON-RPC command.
@davecgh davecgh added this to the 1.8.0 milestone Nov 27, 2022
@@ -124,8 +125,7 @@ func (s *Server) WebsocketHandler(conn *websocket.Conn, remoteAddr string, authe
return
}
s.ntfnMgr.AddClient(client)
client.Start()
client.WaitForShutdown()
client.Run(context.TODO())
Copy link
Member

@dnldd dnldd Nov 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A todo here to pass down the dcrd parent context to synchronize lifecycles would be good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard, just noticed this is being done in #3026.

Copy link
Member

@matheusd matheusd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message for commit rpcserver: Consistent block connected ntfn skip. has typo.

[...] no longer relevant due to the change to the filters are handled some time ago.

This updates the websocket notification manager block connected handler
to skip creation of the associated notification when no clients have
requested it inside of the handler instead of prior to it so it is
consistent with the rest of the handlers.

It was previously done outside of the handler in order to skip a bunch
of work that is no longer relevant due to the change to the filters some
time ago.
This modifies the lifecycle of websocket clients to use the expected
pattern for running subsystems based on contexts.

In particular, this replaces the Start and WaitForShutdown methods with
a single method named Run and arranges for it to block until the
provided context is cancelled.  This is more flexible for the caller
since it can easily turn blocking code into async code while the reverse
is not true.

The new Run method waits for all goroutines that it starts to shutdown
before returning to help ensure an orderly shutdown.

Since there are exported methods that send messages to the output and
notification handler goroutines via channels and those goroutines are
stopped during the shutdown process, all sends select across both the
channels in question as well as a quit channel which is closed when the
context it cancelled.  This ensures callers can't end up blocking on
send to a goroutine that is no longer running without needing additional
mutexes.
@davecgh davecgh force-pushed the rpcserver_websocket_context_lifecycle branch from 32e2937 to d75cc6b Compare November 28, 2022 21:13
@davecgh
Copy link
Member Author

davecgh commented Nov 28, 2022

[...] no longer relevant due to the change to the filters are handled some time ago.

Fixed.

@davecgh davecgh merged commit d75cc6b into decred:master Nov 28, 2022
@davecgh davecgh deleted the rpcserver_websocket_context_lifecycle branch November 28, 2022 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants