add config for signal retry (#2989)

This commit is contained in:
Paul Wells
2024-09-08 19:53:19 -07:00
committed by GitHub
parent 50576b503e
commit b63192a376
2 changed files with 3 additions and 22 deletions

View File

@@ -295,6 +295,7 @@ type SignalRelayConfig struct {
MinRetryInterval time.Duration `yaml:"min_retry_interval,omitempty"`
MaxRetryInterval time.Duration `yaml:"max_retry_interval,omitempty"`
StreamBufferSize int `yaml:"stream_buffer_size,omitempty"`
ConnectAttempts int `yaml:"connect_attempts,omitempty"`
}
// RegionConfig lists available regions and their latitude/longitude, so the selector would prefer
@@ -569,6 +570,7 @@ var DefaultConfig = Config{
MinRetryInterval: 500 * time.Millisecond,
MaxRetryInterval: 4 * time.Second,
StreamBufferSize: 1000,
ConnectAttempts: 3,
},
PSRPC: rpc.DefaultPSRPCConfig,
Keys: map[string]string{},

View File

@@ -42,13 +42,6 @@ import (
"github.com/livekit/psrpc"
)
const (
// TODO: make these configurable
initialBackoffSeconds = 1
maxBackoffSecondsPow = 6 // 64 seconds
startConnectionMaxTries = 3
)
type RTCService struct {
router routing.MessageRouter
roomAllocator RoomAllocator
@@ -236,27 +229,13 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// give it a few attempts to start session
var cr connectionResult
var initialResponse *livekit.SignalResponse
for attempt := 0; attempt < startConnectionMaxTries && r.Context().Err() == nil; attempt++ {
for attempt := 0; attempt < s.config.SignalRelay.ConnectAttempts; attempt++ {
connectionTimeout := 3 * time.Second * time.Duration(attempt+1)
ctx := utils.ContextWithAttempt(r.Context(), attempt)
cr, initialResponse, err = s.startConnection(ctx, roomName, pi, connectionTimeout)
if err == nil || errors.Is(err, context.Canceled) {
break
}
if attempt < startConnectionMaxTries-1 {
// exponential backoff delay. powers of 2.
backoffDelay := time.NewTimer(time.Duration(initialBackoffSeconds<<min(attempt, maxBackoffSecondsPow)) * time.Second)
pLogger.Warnw("failed to start connection, retrying", err,
"attempt", attempt,
"backoffDelay", backoffDelay,
)
select {
case <-backoffDelay.C: // wait for backoff
case <-ctx.Done():
backoffDelay.Stop()
}
}
}
if err != nil {