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

Ability to kill the Serve method #2760

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,12 @@ func (s *BgpServer) passConnToPeer(conn *net.TCPConn) {

const firstPeerCaseIndex = 3

func (s *BgpServer) Serve() {
func (s *BgpServer) Serve(optionalCh ...<-chan struct{}) {
s.listeners = make([]*tcpListener, 0, 2)
var stopCh <-chan struct{}
if len(optionalCh) > 0 {
stopCh = optionalCh[0]
}

handlefsmMsg := func(e *fsmMsg) {
fsm := e.fsm
Expand Down Expand Up @@ -456,7 +460,12 @@ func (s *BgpServer) Serve() {
}

for {
cases := make([]reflect.SelectCase, firstPeerCaseIndex+len(s.incomings))
casesSize := firstPeerCaseIndex + len(s.incomings)
if stopCh != nil {
casesSize++
}
cases := make([]reflect.SelectCase, casesSize)

cases[0] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(s.mgmtCh),
Expand All @@ -475,6 +484,12 @@ func (s *BgpServer) Serve() {
Chan: reflect.ValueOf(s.incomings[i-firstPeerCaseIndex].Out()),
}
}
if stopCh != nil {
cases[len(cases)-1] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(stopCh),
}
}

chosen, value, ok := reflect.Select(cases)
switch chosen {
Expand All @@ -487,7 +502,12 @@ func (s *BgpServer) Serve() {
case 2:
ev := value.Interface().(*roaEvent)
s.roaManager.HandleROAEvent(ev)
case len(cases) - 1:
return
default:
if stopCh != nil && chosen == len(cases)-1 {
return
}
// in the case of dynamic peer, handleFSMMessage closed incoming channel so
// nil fsmMsg can happen here.
if ok {
Expand Down