Skip to content

peer: Convert lifecycle to context.#3633

Open
davecgh wants to merge 1 commit intodecred:masterfrom
davecgh:peer_context_lifecycle
Open

peer: Convert lifecycle to context.#3633
davecgh wants to merge 1 commit intodecred:masterfrom
davecgh:peer_context_lifecycle

Conversation

@davecgh
Copy link
Copy Markdown
Member

@davecgh davecgh commented Mar 4, 2026

This requires #3632.

This modifies the lifecycle of peers to use the more modern Run pattern that based on contexts.

In particular, this replaces the Start and WaitForDisconnect methods with a single method named Run and arranges for it to block until the provided context is cancelled or the peer is disconnected. 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 all exported methods that send messages to the various groroutines via channels already select across the quit channel which is closed when the peer disconnects, the peer is now forcibly disconnected when the context is cancelled.

This approach allows the flexibility for callers to use any combination of manually disconnecting peers via the Disconnect method and allowing them to automatically be disconnected when the context is cancelled.

It also updates the server code accordingly.

@davecgh davecgh added this to the 2.2.0 milestone Mar 4, 2026
@davecgh davecgh force-pushed the peer_context_lifecycle branch 2 times, most recently from 858a00e to d084cc9 Compare March 6, 2026 18:30
@davecgh davecgh force-pushed the peer_context_lifecycle branch 5 times, most recently from be31954 to fff233c Compare March 10, 2026 05:31
@davecgh davecgh force-pushed the peer_context_lifecycle branch 2 times, most recently from e3b8d67 to 67005e3 Compare March 17, 2026 06:07
This modifies the lifecycle of peers to use the more modern Run pattern
that based on contexts.

In particular, this replaces the Start and WaitForDisconnect methods
with a single method named Run and arranges for it to block until the
provided context is cancelled or the peer is disconnected.  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 all exported methods that send messages to the various groroutines
via channels already select across the quit channel which is closed when
the peer disconnects, the peer is now forcibly disconnected when the
context is cancelled.

This approach allows the flexibility for callers to use any combination
of manually disconnecting peers via the Disconnect method and allowing
them to automatically be disconnected when the context is cancelled.

It also updates the server code accordingly.
@davecgh davecgh force-pushed the peer_context_lifecycle branch from 67005e3 to 0629a62 Compare April 7, 2026 04:14
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.

2 participants