netsync: Convert lifecycle to context. #2545
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This requires #2537 and #2543.
This modifies the lifecycle of the sync manager to use the expected pattern for running subsystems based on contexts.
In particular, this replaces the
Start
andStop
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 event handler goroutine via a channel and that goroutine is stopped during the shutdown process, all sends select across both the channel 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.
As part of the conversion, this modifies various functions that respond to channels to select across the quit channel as well to support cancellation.
Finally, the server is modified to take advantage of the new lifecycle semantics by taking ownership of the sync manager directly in its Run method versus the peer handler.