Skip to content

Commit 0bfe6cc

Browse files
committed
server/fsm: Use local holdtimerResetCh with struct{} type in established state
Replace fsmHandler.holdTimerResetCh (chan bool) with a local channel (chan struct{}) created in established() function. This reduces memory usage and makes channel ownership clearer. Signed-off-by: FUJITA Tomonori <[email protected]>
1 parent 7b38fc3 commit 0bfe6cc

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

pkg/server/fsm.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,11 @@ func (fsm *fsm) sendNotification(msg *bgp.BGPMessage) error {
409409
func (fsm *fsm) start(wg *sync.WaitGroup, callback func(*fsmMsg)) {
410410
ctx, cancel := context.WithCancel(context.Background())
411411
fsm.h = &fsmHandler{
412-
fsm: fsm,
413-
outgoing: fsm.outgoingCh,
414-
holdTimerResetCh: make(chan bool, 2),
415-
ctx: ctx,
416-
ctxCancel: cancel,
417-
callback: callback,
412+
fsm: fsm,
413+
outgoing: fsm.outgoingCh,
414+
ctx: ctx,
415+
ctxCancel: cancel,
416+
callback: callback,
418417
}
419418
wg.Add(1)
420419
go fsm.h.loop(ctx, wg)
@@ -427,13 +426,12 @@ func (fsm *fsm) stop() {
427426
type fsmCallback func(*fsmMsg)
428427

429428
type fsmHandler struct {
430-
fsm *fsm
431-
allowLoopback bool
432-
outgoing *channels.InfiniteChannel
433-
holdTimerResetCh chan bool
434-
ctx context.Context
435-
ctxCancel context.CancelFunc
436-
callback fsmCallback
429+
fsm *fsm
430+
allowLoopback bool
431+
outgoing *channels.InfiniteChannel
432+
ctx context.Context
433+
ctxCancel context.CancelFunc
434+
callback fsmCallback
437435
}
438436

439437
func (h *fsmHandler) idle(ctx context.Context) (bgp.FSMState, *fsmStateReason) {
@@ -1445,7 +1443,7 @@ func (h *fsmHandler) sendMessageloop(ctx context.Context, conn net.Conn, stateRe
14451443
}
14461444
}
14471445

1448-
func (h *fsmHandler) recvMessageloop(ctx context.Context, conn net.Conn, stateReasonCh chan<- fsmStateReason, wg *sync.WaitGroup) {
1446+
func (h *fsmHandler) recvMessageloop(ctx context.Context, conn net.Conn, holdtimerResetCh chan<- struct{}, stateReasonCh chan<- fsmStateReason, wg *sync.WaitGroup) {
14491447
defer wg.Done()
14501448

14511449
for ctx.Err() == nil {
@@ -1462,11 +1460,11 @@ func (h *fsmHandler) recvMessageloop(ctx context.Context, conn net.Conn, stateRe
14621460
case bgp.BGP_MSG_ROUTE_REFRESH:
14631461
// nothing to do here
14641462
case bgp.BGP_MSG_UPDATE:
1465-
// if the length of h.holdTimerResetCh
1463+
// if the length of holdtimerResetCh
14661464
// isn't zero, the timer will be reset
14671465
// soon anyway.
14681466
select {
1469-
case h.holdTimerResetCh <- true:
1467+
case holdtimerResetCh <- struct{}{}:
14701468
default:
14711469
}
14721470
body := m.Body.(*bgp.BGPUpdate)
@@ -1510,11 +1508,11 @@ func (h *fsmHandler) recvMessageloop(ctx context.Context, conn net.Conn, stateRe
15101508
}
15111509
fallthrough
15121510
case bgp.BGP_MSG_KEEPALIVE:
1513-
// if the length of h.holdTimerResetCh
1511+
// if the length of holdtimerResetCh
15141512
// isn't zero, the timer will be reset
15151513
// soon anyway.
15161514
select {
1517-
case h.holdTimerResetCh <- true:
1515+
case holdtimerResetCh <- struct{}{}:
15181516
default:
15191517
}
15201518
if m.Header.Type == bgp.BGP_MSG_KEEPALIVE {
@@ -1574,8 +1572,10 @@ func (h *fsmHandler) established(ctx context.Context) (bgp.FSMState, *fsmStateRe
15741572
// to reasonCh with hold timer expiration. So three buffer is enough.
15751573
reasonCh := make(chan fsmStateReason, 3)
15761574

1575+
holdtimerResetCh := make(chan struct{}, 2)
1576+
15771577
go h.sendMessageloop(ioCtx, fsm.conn, reasonCh, wg)
1578-
go h.recvMessageloop(ioCtx, fsm.conn, reasonCh, wg)
1578+
go h.recvMessageloop(ioCtx, fsm.conn, holdtimerResetCh, reasonCh, wg)
15791579

15801580
defer func() {
15811581
// for to stop the recv goroutine
@@ -1676,7 +1676,7 @@ func (h *fsmHandler) established(ctx context.Context) (bgp.FSMState, *fsmStateRe
16761676
return bgp.BGP_FSM_IDLE, newfsmStateReason(fsmWriteFailed, nil, nil)
16771677
}
16781678
reasonCh <- *newfsmStateReason(fsmNotificationSent, m, nil)
1679-
case <-h.holdTimerResetCh:
1679+
case <-holdtimerResetCh:
16801680
fsm.lock.Lock()
16811681
if fsm.pConf.Timers.State.NegotiatedHoldTime != 0 {
16821682
holdTimer.Reset(time.Second * time.Duration(fsm.pConf.Timers.State.NegotiatedHoldTime))

0 commit comments

Comments
 (0)