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

fix: move protocol cleanup routines to Start() #531

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 13 additions & 6 deletions protocol/blockfetch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Client struct {
startBatchResultChan chan error
busyMutex sync.Mutex
blockUseCallback bool
onceStart sync.Once
onceStop sync.Once
}

Expand Down Expand Up @@ -69,15 +70,21 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
InitialState: StateIdle,
}
c.Protocol = protocol.New(protoConfig)
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.blockChan)
close(c.startBatchResultChan)
}()
return c
}

func (c *Client) Start() {
c.onceStart.Do(func() {
c.Protocol.Start()
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.blockChan)
close(c.startBatchResultChan)
}()
})
}

func (c *Client) Stop() error {
var err error
c.onceStop.Do(func() {
Expand Down
25 changes: 16 additions & 9 deletions protocol/chainsync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Client struct {
firstBlockChan chan common.Point
wantIntersectPoint bool
intersectPointChan chan common.Point
onceStart sync.Once
onceStop sync.Once
}

Expand Down Expand Up @@ -88,18 +89,24 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
InitialState: stateIdle,
}
c.Protocol = protocol.New(protoConfig)
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.intersectResultChan)
close(c.readyForNextBlockChan)
close(c.currentTipChan)
close(c.firstBlockChan)
close(c.intersectPointChan)
}()
return c
}

func (c *Client) Start() {
c.onceStart.Do(func() {
c.Protocol.Start()
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.intersectResultChan)
close(c.readyForNextBlockChan)
close(c.currentTipChan)
close(c.firstBlockChan)
close(c.intersectPointChan)
}()
})
}

func (c *Client) messageHandler(msg protocol.Message) error {
var err error
switch msg.Type() {
Expand Down
20 changes: 10 additions & 10 deletions protocol/keepalive/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
InitialState: StateClient,
}
c.Protocol = protocol.New(protoConfig)
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
// Stop any existing timer
c.timerMutex.Lock()
if c.timer != nil {
c.timer.Stop()
}
c.timerMutex.Unlock()
}()
return c
}

func (c *Client) Start() {
c.onceStart.Do(func() {
c.Protocol.Start()
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
// Stop any existing timer
c.timerMutex.Lock()
if c.timer != nil {
c.timer.Stop()
}
c.timerMutex.Unlock()
}()
c.sendKeepAlive()
})
}
Expand Down
19 changes: 13 additions & 6 deletions protocol/localstatequery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Client struct {
queryResultChan chan []byte
acquireResultChan chan error
currentEra int
onceStart sync.Once
}

// NewClient returns a new LocalStateQuery client object
Expand Down Expand Up @@ -82,15 +83,21 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
c.enableGetRewardInfoPoolsBlock = true
}
c.Protocol = protocol.New(protoConfig)
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.queryResultChan)
close(c.acquireResultChan)
}()
return c
}

func (c *Client) Start() {
c.onceStart.Do(func() {
c.Protocol.Start()
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.queryResultChan)
close(c.acquireResultChan)
}()
})
}

func (c *Client) messageHandler(msg protocol.Message) error {
var err error
switch msg.Type() {
Expand Down
23 changes: 15 additions & 8 deletions protocol/localtxmonitor/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Client struct {
hasTxResultChan chan bool
nextTxResultChan chan []byte
getSizesResultChan chan MsgReplyGetSizesResult
onceStart sync.Once
onceStop sync.Once
}

Expand Down Expand Up @@ -72,17 +73,23 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
InitialState: stateIdle,
}
c.Protocol = protocol.New(protoConfig)
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.acquireResultChan)
close(c.hasTxResultChan)
close(c.nextTxResultChan)
close(c.getSizesResultChan)
}()
return c
}

func (c *Client) Start() {
c.onceStart.Do(func() {
c.Protocol.Start()
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.acquireResultChan)
close(c.hasTxResultChan)
close(c.nextTxResultChan)
close(c.getSizesResultChan)
}()
})
}

func (c *Client) messageHandler(msg protocol.Message) error {
var err error
switch msg.Type() {
Expand Down
17 changes: 12 additions & 5 deletions protocol/localtxsubmission/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Client struct {
config *Config
busyMutex sync.Mutex
submitResultChan chan error
onceStart sync.Once
onceStop sync.Once
}

Expand Down Expand Up @@ -61,14 +62,20 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
InitialState: stateIdle,
}
c.Protocol = protocol.New(protoConfig)
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.submitResultChan)
}()
return c
}

func (c *Client) Start() {
c.onceStart.Do(func() {
c.Protocol.Start()
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-c.Protocol.DoneChan()
close(c.submitResultChan)
}()
})
}

func (c *Client) messageHandler(msg protocol.Message) error {
var err error
switch msg.Type() {
Expand Down
20 changes: 14 additions & 6 deletions protocol/txsubmission/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package txsubmission

import (
"fmt"
"sync"

"github.com/blinklabs-io/gouroboros/protocol"
)
Expand All @@ -27,6 +28,7 @@ type Server struct {
stateDone bool
requestTxIdsResultChan chan []TxIdAndSize
requestTxsResultChan chan []TxBody
onceStart sync.Once
}

func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server {
Expand All @@ -48,15 +50,21 @@ func NewServer(protoOptions protocol.ProtocolOptions, cfg *Config) *Server {
InitialState: stateInit,
}
s.Protocol = protocol.New(protoConfig)
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-s.Protocol.DoneChan()
close(s.requestTxIdsResultChan)
close(s.requestTxsResultChan)
}()
return s
}

func (s *Server) Start() {
s.onceStart.Do(func() {
s.Protocol.Start()
// Start goroutine to cleanup resources on protocol shutdown
go func() {
<-s.Protocol.DoneChan()
close(s.requestTxIdsResultChan)
close(s.requestTxsResultChan)
}()
})
}

func (s *Server) messageHandler(msg protocol.Message) error {
var err error
switch msg.Type() {
Expand Down