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

netsync: Convert lifecycle to context. #2545

Merged
merged 1 commit into from Jan 11, 2021

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Jan 3, 2021

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 and Stop 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.

@davecgh davecgh added this to the 1.7.0 milestone Jan 3, 2021
@davecgh davecgh force-pushed the netsync_context_lifecycle branch 3 times, most recently from 23ecf14 to 87e0384 Compare January 3, 2021 07:09
internal/netsync/manager.go Outdated Show resolved Hide resolved
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 and Stop 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.
@davecgh davecgh merged commit 9a86940 into decred:master Jan 11, 2021
@davecgh davecgh deleted the netsync_context_lifecycle branch January 11, 2021 00:51
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.

None yet

4 participants