Skip to content

Commit 3a8f4fb

Browse files
rastislavsfujita
authored andcommitted
fsm: Lower minimal ConnectRetry interval to 1s
Lower the minConnectRetryInterval from 5s to 1s. As this interval also defines the wait time before the initial connection attempt, lowering it helps in situations where fast re(connection) times are required. The change also allows for wider variation for the random jitter applied for the ConnectRetry timer. Signed-off-by: Rastislav Szabo <[email protected]>
1 parent b6052cf commit 3a8f4fb

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

pkg/server/fsm.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
)
3838

3939
const (
40-
minConnectRetryInterval = 5
40+
minConnectRetryInterval = 1
4141
)
4242

4343
type fsmStateReasonType uint8
@@ -494,7 +494,7 @@ func (h *fsmHandler) connectLoop(ctx context.Context, wg *sync.WaitGroup) {
494494
defer wg.Done()
495495
fsm := h.fsm
496496

497-
retry, addr, port, password, ttl, ttlMin, mss, localAddress, localPort, bindInterface := func() (int, string, int, string, uint8, uint8, uint16, string, int, string) {
497+
retryInterval, addr, port, password, ttl, ttlMin, mss, localAddress, localPort, bindInterface := func() (int, string, int, string, uint8, uint8, uint16, string, int, string) {
498498
fsm.lock.RLock()
499499
defer fsm.lock.RUnlock()
500500

@@ -527,7 +527,7 @@ func (h *fsmHandler) connectLoop(ctx context.Context, wg *sync.WaitGroup) {
527527
tick := minConnectRetryInterval
528528
for {
529529
r := rand.New(rand.NewSource(time.Now().UnixNano()))
530-
timer := time.NewTimer(time.Duration(r.Intn(tick)+tick) * time.Second)
530+
timer := time.NewTimer(time.Duration(r.Intn(tick*1000)+tick*1000) * time.Millisecond)
531531
select {
532532
case <-ctx.Done():
533533
fsm.logger.Debug("stop connect loop",
@@ -556,7 +556,7 @@ func (h *fsmHandler) connectLoop(ctx context.Context, wg *sync.WaitGroup) {
556556
if err == nil {
557557
d := net.Dialer{
558558
LocalAddr: laddr,
559-
Timeout: time.Duration(tick-1) * time.Second,
559+
Timeout: time.Duration(max(retryInterval-1, minConnectRetryInterval)) * time.Second,
560560
Control: func(network, address string, c syscall.RawConn) error {
561561
return dialerControl(fsm.logger, network, address, c, ttl, ttlMin, mss, password, bindInterface)
562562
},
@@ -594,7 +594,7 @@ func (h *fsmHandler) connectLoop(ctx context.Context, wg *sync.WaitGroup) {
594594
}
595595
}
596596
}
597-
tick = retry
597+
tick = retryInterval
598598
}
599599
}
600600

0 commit comments

Comments
 (0)