File tree Expand file tree Collapse file tree 1 file changed +25
-18
lines changed
Expand file tree Collapse file tree 1 file changed +25
-18
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ type Mempool struct {
4646 errorChan chan error
4747 doneChan chan struct {}
4848 wg sync.WaitGroup
49+ stopOnce sync.Once // idempotent Stop (same pattern as pipeline.Pipeline)
4950 oConn * ouroboros.Connection
5051 dialFamily string
5152 dialAddress string
@@ -68,6 +69,7 @@ func New(opts ...MempoolOptionFunc) *Mempool {
6869// see a closed channel; after Stop() they are nil so the next Start() creates
6970// new channels and the pipeline obtains fresh references.
7071func (m * Mempool ) Start () error {
72+ m .stopOnce = sync.Once {} // reset so next Stop() runs (Pipeline resets on restart too)
7173 if m .doneChan != nil {
7274 close (m .doneChan )
7375 m .wg .Wait ()
@@ -95,25 +97,30 @@ func (m *Mempool) Start() error {
9597 return nil
9698}
9799
98- // Stop shuts down the connection and stops polling
100+ // Stop shuts down the connection and stops polling.
101+ // Idempotent and safe to call multiple times, following the Pipeline's
102+ // pattern (pipeline/pipeline.go): shutdown logic runs inside sync.Once so
103+ // multiple Stop() calls never double-close channels.
99104func (m * Mempool ) Stop () error {
100- if m .doneChan != nil {
101- close (m .doneChan )
102- m .doneChan = nil
103- }
104- if m .oConn != nil {
105- _ = m .oConn .Close ()
106- m .oConn = nil
107- }
108- m .wg .Wait ()
109- if m .eventChan != nil {
110- close (m .eventChan )
111- m .eventChan = nil
112- }
113- if m .errorChan != nil {
114- close (m .errorChan )
115- m .errorChan = nil
116- }
105+ m .stopOnce .Do (func () {
106+ if m .doneChan != nil {
107+ close (m .doneChan )
108+ m .doneChan = nil
109+ }
110+ if m .oConn != nil {
111+ _ = m .oConn .Close ()
112+ m .oConn = nil
113+ }
114+ m .wg .Wait ()
115+ if m .eventChan != nil {
116+ close (m .eventChan )
117+ m .eventChan = nil
118+ }
119+ if m .errorChan != nil {
120+ close (m .errorChan )
121+ m .errorChan = nil
122+ }
123+ })
117124 return nil
118125}
119126
You can’t perform that action at this time.
0 commit comments