Skip to content

refactor: wait for connection close #1008

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

Merged
merged 1 commit into from
May 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Connection struct {
handshakeVersion uint16
handshakeVersionData protocol.VersionData
doneChan chan interface{}
connClosedChan chan struct{}
waitGroup sync.WaitGroup
onceClose sync.Once
sendKeepAlives bool
Expand Down Expand Up @@ -101,7 +102,7 @@ func NewConnection(options ...ConnectionOptionFunc) (*Connection, error) {
c := &Connection{
protoErrorChan: make(chan error, 10),
handshakeFinishedChan: make(chan interface{}),
doneChan: make(chan interface{}),
connClosedChan: make(chan struct{}),
// Create a discard logger to throw away logs. We do this so
// we don't have to add guards around every log operation if
// a logger is not configured by the user.
Expand Down Expand Up @@ -173,12 +174,16 @@ func (c *Connection) DialTimeout(

// Close will shutdown the Ouroboros connection
func (c *Connection) Close() error {
var err error
c.onceClose.Do(func() {
if c.doneChan == nil {
return
}
// Close doneChan to signify that we're shutting down
close(c.doneChan)
// Wait for connection to be closed
<-c.connClosedChan
})
return err
return nil
}

// BlockFetch returns the block-fetch protocol handler
Expand Down Expand Up @@ -237,6 +242,8 @@ func (c *Connection) shutdown() {
if c.muxer != nil {
c.muxer.Stop()
}
// Close channel to let Close() know that it can return
close(c.connClosedChan)
// Wait for other goroutines to finish
c.waitGroup.Wait()
// Close consumer error channel to signify connection shutdown
Expand All @@ -254,6 +261,7 @@ func (c *Connection) setupConnection() error {
)
}
// Start Goroutine to shutdown when doneChan is closed
c.doneChan = make(chan interface{})
go func() {
<-c.doneChan
c.shutdown()
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy