@@ -3,27 +3,29 @@ package api
3
3
import (
4
4
"context"
5
5
"errors"
6
- "fmt"
7
6
"net/http"
8
- "time"
9
7
10
8
"github.com/tidwall/gjson"
11
9
12
10
"github.com/AgoraIO-Community/agora-rest-client-go/agora"
13
11
"github.com/AgoraIO-Community/agora-rest-client-go/agora/client"
14
12
"github.com/AgoraIO-Community/agora-rest-client-go/agora/log"
15
- "github.com/AgoraIO-Community/agora-rest-client-go/agora/retry"
16
13
)
17
14
18
15
type Start struct {
19
- module string
20
- logger log.Logger
21
- client client.Client
22
- prefixPath string // /v1/apps/{appid}/cloud_recording
16
+ baseHandler
23
17
}
24
18
25
- func NewStart (module string , logger log.Logger , client client.Client , prefixPath string ) * Start {
26
- return & Start {module : module , logger : logger , client : client , prefixPath : prefixPath }
19
+ func NewStart (module string , logger log.Logger , retryCount int , client client.Client , prefixPath string ) * Start {
20
+ return & Start {
21
+ baseHandler : baseHandler {
22
+ module : module ,
23
+ logger : logger ,
24
+ retryCount : retryCount ,
25
+ client : client ,
26
+ prefixPath : prefixPath ,
27
+ },
28
+ }
27
29
}
28
30
29
31
const (
@@ -570,7 +572,7 @@ type StartSuccessResp struct {
570
572
func (s * Start ) Do (ctx context.Context , resourceID string , mode string , payload * StartReqBody ) (* StartResp , error ) {
571
573
path := s .buildPath (resourceID , mode )
572
574
573
- responseData , err := s . doRESTWithRetry (ctx , path , http .MethodPost , payload )
575
+ responseData , err := doRESTWithRetry (ctx , s . module , s . logger , s . retryCount , s . client , path , http .MethodPost , payload )
574
576
if err != nil {
575
577
var internalErr * agora.InternalErr
576
578
if ! errors .As (err , & internalErr ) {
@@ -600,51 +602,3 @@ func (s *Start) Do(ctx context.Context, resourceID string, mode string, payload
600
602
resp .BaseResponse = responseData
601
603
return & resp , nil
602
604
}
603
-
604
- const startRetryCount = 3
605
-
606
- func (s * Start ) doRESTWithRetry (ctx context.Context , path string , method string , requestBody interface {}) (* agora.BaseResponse , error ) {
607
- var (
608
- resp * agora.BaseResponse
609
- err error
610
- retryCount int
611
- )
612
-
613
- err = retry .Do (func (retryCount int ) error {
614
- var doErr error
615
-
616
- resp , doErr = s .client .DoREST (ctx , path , method , requestBody )
617
- if doErr != nil {
618
- return agora .NewRetryErr (false , doErr )
619
- }
620
-
621
- statusCode := resp .HttpStatusCode
622
- switch {
623
- case statusCode == 200 || statusCode == 201 :
624
- return nil
625
- case statusCode >= 400 && statusCode < 499 :
626
- s .logger .Debugf (ctx , s .module , "http status code is %d, no retry,http response:%s" , statusCode , resp .RawBody )
627
- return agora .NewRetryErr (
628
- false ,
629
- agora .NewInternalErr (fmt .Sprintf ("http status code is %d, no retry,http response:%s" , statusCode , resp .RawBody )),
630
- )
631
- default :
632
- s .logger .Debugf (ctx , s .module , "http status code is %d, retry,http response:%s" , statusCode , resp .RawBody )
633
- return fmt .Errorf ("http status code is %d, retry" , resp .RawBody )
634
- }
635
- }, func () bool {
636
- select {
637
- case <- ctx .Done ():
638
- return true
639
- default :
640
- }
641
- return retryCount >= startRetryCount
642
- }, func (i int ) time.Duration {
643
- return time .Second * time .Duration (i + 1 )
644
- }, func (err error ) {
645
- s .logger .Debugf (ctx , s .module , "http request err:%s" , err )
646
- retryCount ++
647
- })
648
-
649
- return resp , err
650
- }
0 commit comments