Skip to content

Commit

Permalink
fix: resolve race test
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Feb 5, 2021
1 parent 2dbb161 commit 1c74459
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
7 changes: 0 additions & 7 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,9 @@ func (el *eventloop) loopRun(lockOSThread bool) {
defer func() {
el.closeAllConns()
el.ln.close()
if el.idx == 0 && el.svr.opts.Ticker {
close(el.svr.ticktock)
}
el.svr.signalShutdown()
}()

if el.idx == 0 && el.svr.opts.Ticker {
go el.loopTicker()
}

err := el.poller.Polling(el.handleEvent)
el.svr.logger.Infof("Event-loop(%d) is exiting due to error: %v", el.idx, err)
}
Expand Down
11 changes: 1 addition & 10 deletions reactor_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,7 @@ func (svr *server) activateMainReactor(lockOSThread bool) {
defer runtime.UnlockOSThread()
}

defer func() {
if svr.opts.Ticker {
close(svr.ticktock)
}
svr.signalShutdown()
}()

if svr.opts.Ticker {
go svr.mainLoop.loopTicker()
}
defer svr.signalShutdown()

err := svr.mainLoop.poller.Polling(func(fd int, filter int16) error { return svr.acceptNewConnection(fd) })
svr.logger.Infof("Main reactor is exiting due to error: %v", err)
Expand Down
11 changes: 1 addition & 10 deletions reactor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,7 @@ func (svr *server) activateMainReactor(lockOSThread bool) {
defer runtime.UnlockOSThread()
}

defer func() {
if svr.opts.Ticker {
close(svr.ticktock)
}
svr.signalShutdown()
}()

if svr.opts.Ticker {
go svr.mainLoop.loopTicker()
}
defer svr.signalShutdown()

err := svr.mainLoop.poller.Polling(func(fd int, ev uint32) error { return svr.acceptNewConnection(fd) })
svr.logger.Infof("Main reactor is exiting due to error: %v", err)
Expand Down
16 changes: 15 additions & 1 deletion server_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ func (svr *server) activateEventLoops(numEventLoop int) (err error) {
el.calibrateCallback = svr.lb.calibrate
_ = el.poller.AddRead(el.ln.fd)
svr.lb.register(el)

// Start the ticker.
if el.idx == 0 && svr.opts.Ticker {
go el.loopTicker()
}
} else {
return
}
Expand All @@ -145,6 +150,11 @@ func (svr *server) activateReactors(numEventLoop int) error {
el.eventHandler = svr.eventHandler
el.calibrateCallback = svr.lb.calibrate
svr.lb.register(el)

// Start the ticker.
if el.idx == 0 && svr.opts.Ticker {
go el.loopTicker()
}
} else {
return err
}
Expand All @@ -159,7 +169,6 @@ func (svr *server) activateReactors(numEventLoop int) error {
el.idx = -1
el.svr = svr
el.poller = p
el.eventHandler = svr.eventHandler
_ = el.poller.AddRead(el.ln.fd)
svr.mainLoop = el

Expand Down Expand Up @@ -214,6 +223,11 @@ func (svr *server) stop(s Server) {
sniffErrorAndLog(svr.mainLoop.poller.Close())
}

// Stop the ticker.
if svr.opts.Ticker {
close(svr.ticktock)
}

atomic.StoreInt32(&svr.inShutdown, 1)
}

Expand Down
8 changes: 7 additions & 1 deletion server_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func (svr *server) startEventLoops(numEventLoop int) {
el.eventHandler = svr.eventHandler
el.calibrateCallback = svr.lb.calibrate
svr.lb.register(el)

// Start the ticker.
if el.idx == 0 && svr.opts.Ticker {
go el.loopTicker()
}
Expand All @@ -115,7 +117,6 @@ func (svr *server) stop(s Server) {

svr.eventHandler.OnShutdown(s)

close(svr.ticktock)
// Close listener.
svr.ln.close()
svr.listenerWG.Wait()
Expand All @@ -137,6 +138,11 @@ func (svr *server) stop(s Server) {
})
svr.loopWG.Wait()

// Stop the ticker.
if svr.opts.Ticker {
close(svr.ticktock)
}

atomic.StoreInt32(&svr.inShutdown, 1)
}

Expand Down

0 comments on commit 1c74459

Please sign in to comment.