Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

stop using goprocess to control teardown #278

Merged
merged 3 commits into from
Sep 8, 2021
Merged

Conversation

marten-seemann
Copy link
Contributor

Depends on libp2p/go-libp2p-core#212.

This PR:

  • removes Swarm.Process() goprocess.Process
  • removes Swarm.Context() context.Context
  • removes the context.Context from NewSwarm

Annoyingly, this means that we can't attach the closing of the peerstore in the testing package any more:

// Call AddChildNoWait because we can't call AddChild after the process
// may have been closed (e.g., if the context was canceled).
s.Process().AddChildNoWait(goprocess.WithTeardown(ps.Close))

We therefore return a network.Network from testing.GenSwarm, and close the peerstore in Close. This change is responsible for 95% of the LOC changed in this PR.

Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

❤️

swarm.go Show resolved Hide resolved
swarm.go Show resolved Hide resolved
@@ -293,16 +278,6 @@ func (s *Swarm) Peerstore() peerstore.Peerstore {
return s.peers
}

// Context returns the context of the swarm
func (s *Swarm) Context() context.Context {
Copy link
Member

Choose a reason for hiding this comment

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

For posterity: the user should control their own system's shutdown.

@@ -201,11 +191,6 @@ func (s *Swarm) teardown() error {
return nil
}

// Process returns the Process of the swarm
func (s *Swarm) Process() goprocess.Process {
return s.proc
Copy link
Member

Choose a reason for hiding this comment

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

For posterity: this was very difficult to use correctly because adding child processes raced with the process shutting down (leading to a panic if this process closed before the child was added).

Technically, it could be used correctly with AddChildNoWait, but most callers would use AddCHild and Go (leading to panics on shutdown).

testing/testing.go Outdated Show resolved Hide resolved
transport_test.go Outdated Show resolved Hide resolved
testing/testing.go Outdated Show resolved Hide resolved
@@ -103,11 +102,14 @@ type Swarm struct {
// `extra` interface{} parameter facilitates the future migration. Supported
// elements are:
// - connmgr.ConnectionGater
func NewSwarm(ctx context.Context, local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, extra ...interface{}) *Swarm {
func NewSwarm(local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, extra ...interface{}) *Swarm {
Copy link
Member

Choose a reason for hiding this comment

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

If we're breaking this interface anyways, let's consider using functional options. We can also do that in a followup patch.

@marten-seemann
Copy link
Contributor Author

Deferring merging until we've cut a new -core release.

@marten-seemann marten-seemann merged commit 084250b into master Sep 8, 2021
@marten-seemann marten-seemann deleted the remove-goprocess branch September 8, 2021 16:34
@aschmahmann aschmahmann mentioned this pull request Dec 1, 2021
80 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants