add config for signal retry (#2989)
This commit is contained in:
@@ -295,6 +295,7 @@ type SignalRelayConfig struct {
|
|||||||
MinRetryInterval time.Duration `yaml:"min_retry_interval,omitempty"`
|
MinRetryInterval time.Duration `yaml:"min_retry_interval,omitempty"`
|
||||||
MaxRetryInterval time.Duration `yaml:"max_retry_interval,omitempty"`
|
MaxRetryInterval time.Duration `yaml:"max_retry_interval,omitempty"`
|
||||||
StreamBufferSize int `yaml:"stream_buffer_size,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
|
// RegionConfig lists available regions and their latitude/longitude, so the selector would prefer
|
||||||
@@ -569,6 +570,7 @@ var DefaultConfig = Config{
|
|||||||
MinRetryInterval: 500 * time.Millisecond,
|
MinRetryInterval: 500 * time.Millisecond,
|
||||||
MaxRetryInterval: 4 * time.Second,
|
MaxRetryInterval: 4 * time.Second,
|
||||||
StreamBufferSize: 1000,
|
StreamBufferSize: 1000,
|
||||||
|
ConnectAttempts: 3,
|
||||||
},
|
},
|
||||||
PSRPC: rpc.DefaultPSRPCConfig,
|
PSRPC: rpc.DefaultPSRPCConfig,
|
||||||
Keys: map[string]string{},
|
Keys: map[string]string{},
|
||||||
|
|||||||
@@ -42,13 +42,6 @@ import (
|
|||||||
"github.com/livekit/psrpc"
|
"github.com/livekit/psrpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
// TODO: make these configurable
|
|
||||||
initialBackoffSeconds = 1
|
|
||||||
maxBackoffSecondsPow = 6 // 64 seconds
|
|
||||||
startConnectionMaxTries = 3
|
|
||||||
)
|
|
||||||
|
|
||||||
type RTCService struct {
|
type RTCService struct {
|
||||||
router routing.MessageRouter
|
router routing.MessageRouter
|
||||||
roomAllocator RoomAllocator
|
roomAllocator RoomAllocator
|
||||||
@@ -236,27 +229,13 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// give it a few attempts to start session
|
// give it a few attempts to start session
|
||||||
var cr connectionResult
|
var cr connectionResult
|
||||||
var initialResponse *livekit.SignalResponse
|
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)
|
connectionTimeout := 3 * time.Second * time.Duration(attempt+1)
|
||||||
ctx := utils.ContextWithAttempt(r.Context(), attempt)
|
ctx := utils.ContextWithAttempt(r.Context(), attempt)
|
||||||
cr, initialResponse, err = s.startConnection(ctx, roomName, pi, connectionTimeout)
|
cr, initialResponse, err = s.startConnection(ctx, roomName, pi, connectionTimeout)
|
||||||
if err == nil || errors.Is(err, context.Canceled) {
|
if err == nil || errors.Is(err, context.Canceled) {
|
||||||
break
|
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 {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user