add exponential backoff to room service check retries (#2462)
This commit is contained in:
@@ -297,19 +297,22 @@ type IngressConfig struct {
|
||||
type SIPConfig struct {
|
||||
}
|
||||
|
||||
// not exposed to YAML
|
||||
type APIConfig struct {
|
||||
// amount of time to wait for API to execute, default 2s
|
||||
ExecutionTimeout time.Duration
|
||||
ExecutionTimeout time.Duration `yaml:"execution_timeout,omitempty"`
|
||||
|
||||
// amount of time to wait before checking for operation complete
|
||||
CheckInterval time.Duration
|
||||
// min amount of time to wait before checking for operation complete
|
||||
CheckInterval time.Duration `yaml:"check_interval,omitempty"`
|
||||
|
||||
// max amount of time to wait before checking for operation complete
|
||||
MaxCheckInterval time.Duration `yaml:"max_check_interval,omitempty"`
|
||||
}
|
||||
|
||||
func DefaultAPIConfig() APIConfig {
|
||||
return APIConfig{
|
||||
ExecutionTimeout: 2 * time.Second,
|
||||
CheckInterval: 100 * time.Millisecond,
|
||||
MaxCheckInterval: 300 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+13
-18
@@ -17,8 +17,8 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/avast/retry-go/v4"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/twitchtv/twirp"
|
||||
|
||||
@@ -101,7 +101,7 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
|
||||
defer res.ResponseSource.Close()
|
||||
|
||||
// ensure it's created correctly
|
||||
err = s.confirmExecution(func() error {
|
||||
err = s.confirmExecution(ctx, func() error {
|
||||
_, _, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Name), false)
|
||||
if err != nil {
|
||||
return ErrOperationFailed
|
||||
@@ -299,7 +299,7 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.confirmExecution(func() error {
|
||||
err = s.confirmExecution(ctx, func() error {
|
||||
room, _, err = s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Room), false)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -326,19 +326,14 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
|
||||
return room, nil
|
||||
}
|
||||
|
||||
func (s *RoomService) confirmExecution(f func() error) error {
|
||||
expired := time.After(s.apiConf.ExecutionTimeout)
|
||||
var err error
|
||||
for {
|
||||
select {
|
||||
case <-expired:
|
||||
return err
|
||||
default:
|
||||
err = f()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(s.apiConf.CheckInterval)
|
||||
}
|
||||
}
|
||||
func (s *RoomService) confirmExecution(ctx context.Context, f func() error) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.apiConf.ExecutionTimeout)
|
||||
defer cancel()
|
||||
return retry.Do(
|
||||
f,
|
||||
retry.Context(ctx),
|
||||
retry.Delay(s.apiConf.CheckInterval),
|
||||
retry.MaxDelay(s.apiConf.MaxCheckInterval),
|
||||
retry.DelayType(retry.BackOffDelay),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user