add exponential backoff to room service check retries (#2462)
This commit is contained in:
@@ -3,6 +3,7 @@ module github.com/livekit/livekit-server
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/avast/retry-go/v4 v4.5.1
|
||||||
github.com/bep/debounce v1.2.1
|
github.com/bep/debounce v1.2.1
|
||||||
github.com/d5/tengo/v2 v2.16.1
|
github.com/d5/tengo/v2 v2.16.1
|
||||||
github.com/dustin/go-humanize v1.0.1
|
github.com/dustin/go-humanize v1.0.1
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh7o=
|
||||||
|
github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc=
|
||||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||||
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
|
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
|
||||||
|
|||||||
@@ -297,19 +297,22 @@ type IngressConfig struct {
|
|||||||
type SIPConfig struct {
|
type SIPConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// not exposed to YAML
|
|
||||||
type APIConfig struct {
|
type APIConfig struct {
|
||||||
// amount of time to wait for API to execute, default 2s
|
// 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
|
// min amount of time to wait before checking for operation complete
|
||||||
CheckInterval time.Duration
|
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 {
|
func DefaultAPIConfig() APIConfig {
|
||||||
return APIConfig{
|
return APIConfig{
|
||||||
ExecutionTimeout: 2 * time.Second,
|
ExecutionTimeout: 2 * time.Second,
|
||||||
CheckInterval: 100 * time.Millisecond,
|
CheckInterval: 100 * time.Millisecond,
|
||||||
|
MaxCheckInterval: 300 * time.Second,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-18
@@ -17,8 +17,8 @@ package service
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"github.com/avast/retry-go/v4"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/twitchtv/twirp"
|
"github.com/twitchtv/twirp"
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
|
|||||||
defer res.ResponseSource.Close()
|
defer res.ResponseSource.Close()
|
||||||
|
|
||||||
// ensure it's created correctly
|
// 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)
|
_, _, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Name), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrOperationFailed
|
return ErrOperationFailed
|
||||||
@@ -299,7 +299,7 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
|
|||||||
return nil, err
|
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)
|
room, _, err = s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Room), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -326,19 +326,14 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
|
|||||||
return room, nil
|
return room, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RoomService) confirmExecution(f func() error) error {
|
func (s *RoomService) confirmExecution(ctx context.Context, f func() error) error {
|
||||||
expired := time.After(s.apiConf.ExecutionTimeout)
|
ctx, cancel := context.WithTimeout(ctx, s.apiConf.ExecutionTimeout)
|
||||||
var err error
|
defer cancel()
|
||||||
for {
|
return retry.Do(
|
||||||
select {
|
f,
|
||||||
case <-expired:
|
retry.Context(ctx),
|
||||||
return err
|
retry.Delay(s.apiConf.CheckInterval),
|
||||||
default:
|
retry.MaxDelay(s.apiConf.MaxCheckInterval),
|
||||||
err = f()
|
retry.DelayType(retry.BackOffDelay),
|
||||||
if err == nil {
|
)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
time.Sleep(s.apiConf.CheckInterval)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user