35
35
defaultBackup = false
36
36
defaultDown = false
37
37
defaultWeight = 1
38
+ defaultTimeout = 10 * time .Second
38
39
)
39
40
40
41
// ErrUnsupportedVer means that client's API version is not supported by NGINX plus API.
@@ -46,6 +47,7 @@ type NginxClient struct {
46
47
apiEndpoint string
47
48
apiVersion int
48
49
checkAPI bool
50
+ ctxTimeout time.Duration
49
51
}
50
52
51
53
type Option func (* NginxClient )
@@ -546,13 +548,21 @@ func WithCheckAPI() Option {
546
548
}
547
549
}
548
550
551
+ // WithTimeout sets the timeout per request for the client.
552
+ func WithTimeout (duration time.Duration ) Option {
553
+ return func (o * NginxClient ) {
554
+ o .ctxTimeout = duration
555
+ }
556
+ }
557
+
549
558
// NewNginxClient creates a new NginxClient.
550
559
func NewNginxClient (apiEndpoint string , opts ... Option ) (* NginxClient , error ) {
551
560
c := & NginxClient {
552
561
httpClient : http .DefaultClient ,
553
562
apiEndpoint : apiEndpoint ,
554
563
apiVersion : APIVersion ,
555
564
checkAPI : false ,
565
+ ctxTimeout : defaultTimeout ,
556
566
}
557
567
558
568
for _ , opt := range opts {
@@ -567,8 +577,12 @@ func NewNginxClient(apiEndpoint string, opts ...Option) (*NginxClient, error) {
567
577
return nil , fmt .Errorf ("API version %v is not supported by the client" , c .apiVersion )
568
578
}
569
579
580
+ if c .ctxTimeout <= 0 {
581
+ return nil , fmt .Errorf ("timeout has to be greater than 0 %v" , c .ctxTimeout )
582
+ }
583
+
570
584
if c .checkAPI {
571
- versions , err := getAPIVersions (c .httpClient , apiEndpoint )
585
+ versions , err := c . getAPIVersions (c .httpClient , apiEndpoint )
572
586
if err != nil {
573
587
return nil , fmt .Errorf ("error accessing the API: %w" , err )
574
588
}
@@ -596,8 +610,8 @@ func versionSupported(n int) bool {
596
610
return false
597
611
}
598
612
599
- func getAPIVersions (httpClient * http.Client , endpoint string ) (* versions , error ) {
600
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time . Second )
613
+ func ( client * NginxClient ) getAPIVersions (httpClient * http.Client , endpoint string ) (* versions , error ) {
614
+ ctx , cancel := context .WithTimeout (context .Background (), client . ctxTimeout )
601
615
defer cancel ()
602
616
603
617
req , err := http .NewRequestWithContext (ctx , http .MethodGet , endpoint , nil )
@@ -852,7 +866,7 @@ func (client *NginxClient) getIDOfHTTPServer(upstream string, name string) (int,
852
866
}
853
867
854
868
func (client * NginxClient ) get (path string , data interface {}) error {
855
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time . Second )
869
+ ctx , cancel := context .WithTimeout (context .Background (), client . ctxTimeout )
856
870
defer cancel ()
857
871
858
872
url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .apiVersion , path )
@@ -886,7 +900,7 @@ func (client *NginxClient) get(path string, data interface{}) error {
886
900
}
887
901
888
902
func (client * NginxClient ) post (path string , input interface {}) error {
889
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time . Second )
903
+ ctx , cancel := context .WithTimeout (context .Background (), client . ctxTimeout )
890
904
defer cancel ()
891
905
892
906
url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .apiVersion , path )
@@ -918,7 +932,7 @@ func (client *NginxClient) post(path string, input interface{}) error {
918
932
}
919
933
920
934
func (client * NginxClient ) delete (path string , expectedStatusCode int ) error {
921
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time . Second )
935
+ ctx , cancel := context .WithTimeout (context .Background (), client . ctxTimeout )
922
936
defer cancel ()
923
937
924
938
path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .apiVersion , path )
@@ -943,7 +957,7 @@ func (client *NginxClient) delete(path string, expectedStatusCode int) error {
943
957
}
944
958
945
959
func (client * NginxClient ) patch (path string , input interface {}, expectedStatusCode int ) error {
946
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time . Second )
960
+ ctx , cancel := context .WithTimeout (context .Background (), client . ctxTimeout )
947
961
defer cancel ()
948
962
949
963
path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .apiVersion , path )
0 commit comments