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

[protocol] Move close channel after close topics #283

Merged
merged 2 commits into from
Jan 12, 2023
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
4 changes: 1 addition & 3 deletions consensus/ibft/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,14 +1471,12 @@ func (i *Ibft) IsLastOfEpoch(number uint64) bool {

// Close closes the IBFT consensus mechanism, and does write back to disk
func (i *Ibft) Close() error {
if i.isClosed.Load() {
if !i.isClosed.CAS(false, true) {
i.logger.Error("IBFT consensus is Closed")

return nil
}

i.isClosed.Store(true)

close(i.closeCh)

if i.config.Path != "" {
Expand Down
18 changes: 8 additions & 10 deletions protocol/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type syncPeerClient struct {
peerConnectionUpdateCh chan *event.PeerEvent // peer connection update channel

shouldEmitBlocks bool // flag for emitting blocks in the topic
isClose *atomic.Bool
isClosed *atomic.Bool
}

func NewSyncPeerClient(
Expand All @@ -53,7 +53,7 @@ func NewSyncPeerClient(
peerStatusUpdateCh: make(chan *NoForkPeer, 1),
peerConnectionUpdateCh: make(chan *event.PeerEvent, 1),
shouldEmitBlocks: true,
isClose: atomic.NewBool(false),
isClosed: atomic.NewBool(false),
}
}

Expand All @@ -71,27 +71,25 @@ func (client *syncPeerClient) Start() error {

// Close terminates running processes for SyncPeerClient
func (client *syncPeerClient) Close() {
if client.isClose.Load() {
if !client.isClosed.CAS(false, true) {
return
}

client.isClose.Store(true)

if client.subscription != nil {
client.subscription.Close()

client.subscription = nil
}

close(client.peerStatusUpdateCh)
close(client.peerConnectionUpdateCh)

if client.topic != nil {
// close topic when needed
client.topic.Close()

client.topic = nil
}

close(client.peerStatusUpdateCh)
close(client.peerConnectionUpdateCh)
}

// DisablePublishingPeerStatus disables publishing own status via gossip
Expand Down Expand Up @@ -208,7 +206,7 @@ func (client *syncPeerClient) handleStatusUpdate(obj interface{}, from peer.ID)

client.logger.Debug("get connected peer status update", "from", from, "status", status.Number)

if client.isClose.Load() {
if client.isClosed.Load() {
client.logger.Debug("client is closed, ignore status update", "from", from, "status", status.Number)

return
Expand Down Expand Up @@ -254,7 +252,7 @@ func (client *syncPeerClient) startPeerEventProcess() {

for e := range peerEventCh {
if e.Type == event.PeerConnected || e.Type == event.PeerDisconnected {
if client.isClose.Load() {
if client.isClosed.Load() {
client.logger.Debug("client is closed, ignore peer connection update", "peer", e.PeerID, "type", e.Type)

return
Expand Down
2 changes: 1 addition & 1 deletion protocol/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newTestSyncPeerClient(network network.Network, blockchain Blockchain) *sync
id: network.AddrInfo().ID.String(),
peerStatusUpdateCh: make(chan *NoForkPeer, 1),
peerConnectionUpdateCh: make(chan *event.PeerEvent, 1),
isClose: atomic.NewBool(false),
isClosed: atomic.NewBool(false),
}

// need to register protocol
Expand Down
4 changes: 1 addition & 3 deletions txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,12 @@ func (p *TxPool) Start() {

// Close shuts down the pool's main loop.
func (p *TxPool) Close() {
if p.isClosed.Load() {
if !p.isClosed.CAS(false, true) {
p.logger.Error("txpool is Closed")

return
}

p.isClosed.Store(true)

p.ddosReductionTicker.Stop()

p.logger.Info("txpool close pruneAccountTicker")
Expand Down