/
manipulator.go
928 lines (729 loc) · 23.2 KB
/
manipulator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package maniphttp
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/gofrs/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"go.aporeto.io/elemental"
"go.aporeto.io/manipulate"
"go.aporeto.io/manipulate/internal/backoff"
"go.aporeto.io/manipulate/internal/idempotency"
"go.aporeto.io/manipulate/internal/snip"
"go.aporeto.io/manipulate/internal/tracing"
)
const (
defaultGlobalContextTimeout = 2 * time.Minute
minContextTimeout = 20 * time.Second
)
type httpManipulator struct {
username string
password string
url string
namespace string
renewLock sync.RWMutex
renewNotifiers map[string]func(string)
renewNotifiersLock sync.RWMutex
disableAutoRetry bool
disableCompression bool
defaultRetryFunc manipulate.RetryFunc
atomicRenewTokenFunc func(context.Context) error
failureSimulations map[float64]error
tokenCookieKey string
backoffCurve []time.Duration
strongBackoffCurve []time.Duration
// optionnable
ctx context.Context
client *http.Client
tlsConfig *tls.Config
tokenManager manipulate.TokenManager
globalHeaders http.Header
transport *http.Transport
encoding elemental.EncodingType
tcpUserTimeout time.Duration
}
// New returns a maniphttp.Manipulator configured according to the given suite of Option.
func New(ctx context.Context, url string, options ...Option) (manipulate.Manipulator, error) {
if url == "" {
panic("empty url")
}
url = strings.TrimRight(url, "/")
// initialize solid varialbles.
m := &httpManipulator{
renewLock: sync.RWMutex{},
renewNotifiersLock: sync.RWMutex{},
renewNotifiers: map[string]func(string){},
ctx: ctx,
url: url,
encoding: elemental.EncodingTypeJSON,
backoffCurve: defaultBackoffCurve,
strongBackoffCurve: strongBackoffCurve,
}
// Apply the options.
for _, opt := range options {
opt(m)
}
if m.client == nil {
m.client = getDefaultClient()
if m.transport == nil {
m.transport, m.url = getDefaultHTTPTransport(url, m.disableCompression, m.tcpUserTimeout)
if m.tlsConfig == nil {
m.tlsConfig = getDefaultTLSConfig()
}
m.transport.TLSClientConfig = m.tlsConfig
}
m.client.Transport = m.transport
}
// if we don't have a internal tls config, we sync with the current client.
if m.tlsConfig == nil {
m.tlsConfig = m.client.Transport.(*http.Transport).TLSClientConfig
}
if m.tokenManager != nil {
if s, ok := m.tokenManager.(manipulate.SelfTokenManager); ok {
s.SetManipulator(m)
}
ictx, cancel := context.WithTimeout(m.ctx, 30*time.Second)
defer cancel()
m.atomicRenewTokenFunc = elemental.AtomicJob(m.renewToken)
token, err := m.tokenManager.Issue(ictx)
if err != nil {
return nil, fmt.Errorf("unable to call initial tokenmanager.Issue: %w", err)
}
m.username = "Bearer"
m.password = token
go func() {
tokenCh := make(chan string)
go m.tokenManager.Run(m.ctx, tokenCh)
for {
select {
case t := <-tokenCh:
m.setPassword(t)
case <-m.ctx.Done():
return
}
}
}()
}
return m, nil
}
func (s *httpManipulator) RetrieveMany(mctx manipulate.Context, dest elemental.Identifiables) error {
if dest == nil {
return manipulate.ErrCannotBuildQuery{Err: fmt.Errorf("nil dest")}
}
if mctx == nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultGlobalContextTimeout)
defer cancel()
mctx = manipulate.NewContext(ctx)
}
sp := tracing.StartTrace(mctx, fmt.Sprintf("maniphttp.retrieve_many.%s", dest.Identity().Category))
defer sp.Finish()
url, err := s.getURLForChildrenIdentity(mctx.Parent(), dest.Identity(), dest.Version(), mctx.Version())
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return manipulate.ErrCannotBuildQuery{Err: err}
}
response, err := s.send(mctx, http.MethodGet, url, nil, dest, sp)
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return err
}
if response.StatusCode == http.StatusNoContent {
return nil
}
// backport all default values that are empty.
for _, o := range dest.List() {
if a, ok := o.(elemental.AttributeSpecifiable); ok {
elemental.ResetDefaultForZeroValues(a)
}
}
return nil
}
func (s *httpManipulator) Retrieve(mctx manipulate.Context, object elemental.Identifiable) error {
if object == nil {
return manipulate.ErrCannotBuildQuery{Err: fmt.Errorf("nil object")}
}
if mctx == nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultGlobalContextTimeout)
defer cancel()
mctx = manipulate.NewContext(ctx)
}
sp := tracing.StartTrace(mctx, fmt.Sprintf("maniphttp.retrieve.object.%s", object.Identity().Name))
defer sp.Finish()
url, err := s.getPersonalURL(object, mctx.Version())
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return manipulate.ErrCannotBuildQuery{Err: err}
}
response, err := s.send(mctx, http.MethodGet, url, nil, object, sp)
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return err
}
if response.StatusCode == http.StatusNoContent {
return nil
}
// backport all default values that are empty.
if a, ok := object.(elemental.AttributeSpecifiable); ok {
elemental.ResetDefaultForZeroValues(a)
}
return nil
}
func (s *httpManipulator) Create(mctx manipulate.Context, object elemental.Identifiable) error {
if object == nil {
return manipulate.ErrCannotBuildQuery{Err: fmt.Errorf("nil object")}
}
if mctx == nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultGlobalContextTimeout)
defer cancel()
mctx = manipulate.NewContext(ctx)
}
kmctx, _ := mctx.(idempotency.Keyer)
if kmctx != nil && kmctx.IdempotencyKey() == "" {
kmctx.SetIdempotencyKey(uuid.Must(uuid.NewV4()).String())
}
sp := tracing.StartTrace(mctx, fmt.Sprintf("maniphttp.create.object.%s", object.Identity().Name))
sp.LogFields(log.String("object_id", object.Identifier()))
defer sp.Finish()
url, err := s.getURLForChildrenIdentity(mctx.Parent(), object.Identity(), object.Version(), mctx.Version())
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return manipulate.ErrCannotBuildQuery{Err: err}
}
data, err := elemental.Encode(s.encoding, object)
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return manipulate.ErrCannotMarshal{Err: err}
}
response, err := s.send(mctx, http.MethodPost, url, bytes.NewReader(data), object, sp)
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return err
}
if response.StatusCode == http.StatusNoContent {
return nil
}
// backport all default values that are empty.
if a, ok := object.(elemental.AttributeSpecifiable); ok {
elemental.ResetDefaultForZeroValues(a)
}
if kmctx != nil {
kmctx.SetIdempotencyKey("")
}
return nil
}
func (s *httpManipulator) Update(mctx manipulate.Context, object elemental.Identifiable) error {
if object == nil {
return manipulate.ErrCannotBuildQuery{Err: fmt.Errorf("nil object")}
}
if mctx == nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultGlobalContextTimeout)
defer cancel()
mctx = manipulate.NewContext(ctx)
}
kmctx, _ := mctx.(idempotency.Keyer)
if kmctx != nil && kmctx.IdempotencyKey() == "" {
kmctx.SetIdempotencyKey(uuid.Must(uuid.NewV4()).String())
}
method := http.MethodPut
if _, ok := object.(elemental.SparseIdentifiable); ok {
method = http.MethodPatch
}
sp := tracing.StartTrace(mctx, fmt.Sprintf("maniphttp.update.object.%s", object.Identity().Name))
sp.LogFields(log.String("object_id", object.Identifier()))
defer sp.Finish()
url, err := s.getPersonalURL(object, mctx.Version())
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return manipulate.ErrCannotBuildQuery{Err: err}
}
data, err := elemental.Encode(s.encoding, object)
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return manipulate.ErrCannotMarshal{Err: err}
}
response, err := s.send(mctx, method, url, bytes.NewReader(data), object, sp)
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return err
}
if response.StatusCode == http.StatusNoContent {
return nil
}
// backport all default values that are empty.
if a, ok := object.(elemental.AttributeSpecifiable); ok {
elemental.ResetDefaultForZeroValues(a)
}
if kmctx != nil {
kmctx.SetIdempotencyKey("")
}
return nil
}
func (s *httpManipulator) Delete(mctx manipulate.Context, object elemental.Identifiable) error {
if object == nil {
return manipulate.ErrCannotBuildQuery{Err: fmt.Errorf("nil object")}
}
if mctx == nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultGlobalContextTimeout)
defer cancel()
mctx = manipulate.NewContext(ctx)
}
sp := tracing.StartTrace(mctx, fmt.Sprintf("maniphttp.delete.object.%s", object.Identity().Name))
sp.LogFields(log.String("object_id", object.Identifier()))
defer sp.Finish()
url, err := s.getPersonalURL(object, mctx.Version())
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return manipulate.ErrCannotBuildQuery{Err: err}
}
response, err := s.send(mctx, http.MethodDelete, url, nil, object, sp)
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return err
}
if response.StatusCode == http.StatusNoContent {
return nil
}
// backport all default values that are empty.
if a, ok := object.(elemental.AttributeSpecifiable); ok {
elemental.ResetDefaultForZeroValues(a)
}
return nil
}
func (s *httpManipulator) DeleteMany(mctx manipulate.Context, identity elemental.Identity) error {
return manipulate.ErrNotImplemented{Err: fmt.Errorf("DeleteMany not implemented in maniphttp")}
}
func (s *httpManipulator) Count(mctx manipulate.Context, identity elemental.Identity) (int, error) {
if mctx == nil {
ctx, cancel := context.WithTimeout(context.Background(), defaultGlobalContextTimeout)
defer cancel()
mctx = manipulate.NewContext(ctx)
}
sp := tracing.StartTrace(mctx, fmt.Sprintf("maniphttp.count.%s", identity.Category))
defer sp.Finish()
url, err := s.getURLForChildrenIdentity(mctx.Parent(), identity, 0, mctx.Version())
if err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return 0, manipulate.ErrCannotBuildQuery{Err: err}
}
if _, err = s.send(mctx, http.MethodHead, url, nil, nil, sp); err != nil {
sp.SetTag("error", true)
sp.LogFields(log.Error(err))
return 0, err
}
return mctx.Count(), nil
}
func (s *httpManipulator) makeAuthorizationHeaders(username, password string) string {
return username + " " + password
}
func (s *httpManipulator) prepareHeaders(request *http.Request, mctx manipulate.Context) {
ns := mctx.Namespace()
if ns == "" {
ns = s.namespace
}
for k, v := range s.globalHeaders {
request.Header[k] = v
}
opaque := mctx.(opaquer).Opaque()
if value, ok := opaque[opaqueKeyAdditionalHeaders]; ok {
for k, v := range value.(http.Header) {
request.Header[k] = v
}
}
if value, ok := opaque[opaqueKeyOverrideHeaderContentType]; ok {
request.Header.Set("Content-Type", value.(string))
} else {
request.Header.Set("Content-Type", string(s.encoding))
}
if value, ok := opaque[opaqueKeyOverrideHeaderAccept]; ok {
request.Header.Set("Accept", value.(string))
} else {
request.Header.Set("Accept", string(s.encoding))
}
if ns != "" {
request.Header.Set("X-Namespace", ns)
}
username, password := mctx.Credentials()
if password == "" {
password = s.currentPassword()
}
if username == "" {
username = s.username
}
if password != "" && s.tokenCookieKey != "" {
request.Header.Add("Cookie", fmt.Sprintf("%s=%s", s.tokenCookieKey, password))
} else if username != "" && password != "" {
request.Header.Set("Authorization", s.makeAuthorizationHeaders(username, password))
}
if v := mctx.ExternalTrackingID(); v != "" {
request.Header.Set("X-External-Tracking-ID", v)
}
if v := mctx.ExternalTrackingType(); v != "" {
request.Header.Set("X-External-Tracking-Type", v)
}
if v := mctx.ReadConsistency(); v != manipulate.ReadConsistencyDefault {
request.Header.Set("X-Read-Consistency", string(v))
}
if v := mctx.WriteConsistency(); v != manipulate.WriteConsistencyDefault {
request.Header.Set("X-Write-Consistency", string(v))
}
if k, ok := mctx.(idempotency.Keyer); ok && k.IdempotencyKey() != "" {
request.Header.Set("Idempotency-Key", k.IdempotencyKey())
}
for _, field := range mctx.Fields() {
request.Header.Add("X-Fields", field)
}
if v := mctx.ClientIP(); v != "" {
request.Header.Set("X-Forwarded-For", v)
}
}
func (s *httpManipulator) readHeaders(response *http.Response, mctx manipulate.Context) {
if mctx == nil {
return
}
t, _ := strconv.Atoi(response.Header.Get("X-Count-Total"))
mctx.SetCount(t)
mctx.SetNext(response.Header.Get("X-Next"))
mctx.SetMessages(response.Header["X-Messages"])
}
func (s *httpManipulator) computeVersion(modelVersion int, mctxVersion int) string {
if mctxVersion > 0 {
return fmt.Sprintf("v/%d/", mctxVersion)
}
if modelVersion > 0 {
return fmt.Sprintf("v/%d/", modelVersion)
}
return ""
}
func (s *httpManipulator) getGeneralURL(o elemental.Identifiable, mctxVersion int) string {
v := s.computeVersion(o.Version(), mctxVersion)
return s.url + "/" + v + o.Identity().Category
}
func (s *httpManipulator) getPersonalURL(o elemental.Identifiable, mctxVersion int) (string, error) {
if o.Identifier() == "" {
return "", fmt.Errorf("cannot GetPersonalURL of an object with no ID set")
}
return s.getGeneralURL(o, mctxVersion) + "/" + o.Identifier(), nil
}
func (s *httpManipulator) getURLForChildrenIdentity(
parent elemental.Identifiable,
childrenIdentity elemental.Identity,
modelVersion int,
mctxVersion int,
) (string, error) {
if parent == nil {
v := s.computeVersion(modelVersion, mctxVersion)
return s.url + "/" + v + childrenIdentity.Category, nil
}
url, err := s.getPersonalURL(parent, mctxVersion)
if err != nil {
return "", err
}
return url + "/" + childrenIdentity.Category, nil
}
func (s *httpManipulator) send(
mctx manipulate.Context,
method string,
requrl string,
body *bytes.Reader,
dest any,
sp opentracing.Span,
) (*http.Response, error) {
if len(s.failureSimulations) > 0 {
for chance, err := range s.failureSimulations {
if rand.Float64() < chance {
return nil, err
}
}
}
var try int // try number. Starts at 0
var lastError error // last error before retry.
var tokenRenewedOnce bool // after an authorization failures token is renewed at most once.
retryCurve := s.backoffCurve // Set the regular backoff curve by default
// We get the context deadline.
deadline, ok := mctx.Context().Deadline()
if !ok {
deadline = time.Now().Add(time.Hour) // long, but not completely unlimited.
}
// We divide the time until deadline into multiple retries
// and make it a minimum of minContextTimeout.
subContextTimeout := time.Until(deadline) / time.Duration(mctx.RetryRatio())
if subContextTimeout < minContextTimeout {
subContextTimeout = minContextTimeout
}
// Helpers to deal with current request canceling
var cancelReq context.CancelFunc
cancelCurrentRequest := func() {
if cancelReq != nil {
cancelReq()
}
}
defer cancelCurrentRequest()
// Helpers to deal with closing the body of the current response
var responseBodyCloser io.ReadCloser
closeCurrentResponseBody := func() {
if responseBodyCloser != nil {
_, _ = io.Copy(io.Discard, responseBodyCloser)
_ = responseBodyCloser.Close() // nolint
}
}
defer closeCurrentResponseBody()
// Function that creates a new request to avoid reusing some buffers.
// It also sets the current request cancel function.
newRequest := func() (*http.Request, error) {
// This var is needed because calling Len() bytes.Reader
// when it's nil panics.
var currentRequestBody io.Reader
if body != nil {
if _, err := body.Seek(0, 0); err != nil {
return nil, manipulate.ErrCannotBuildQuery{Err: err}
}
currentRequestBody = body
}
req, err := http.NewRequest(method, requrl, currentRequestBody)
if err != nil {
return nil, manipulate.ErrCannotBuildQuery{Err: err}
}
if err = addQueryParameters(req, mctx); err != nil {
return nil, err
}
if err = sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(req.Header)); err != nil {
return nil, err
}
// We injects the header from mctx.
s.prepareHeaders(req, mctx)
ctx, cancel := context.WithTimeout(mctx.Context(), subContextTimeout)
cancelReq = cancel
return req.WithContext(ctx), nil
}
// Main retry loop
for {
// We spawn a new request
request, err := newRequest()
if err != nil {
return nil, err
}
// We launch the request
response, err := s.client.Do(request)
if err != nil {
// Per doc, client.Do always returns an *url.Error.
uerr := err.(*url.Error)
// We check for constant errors.
switch uerr.Err {
case context.Canceled:
return nil, manipulate.ErrDisconnected{Err: context.Canceled}
case context.DeadlineExceeded:
if lastError == nil {
lastError = manipulate.ErrCannotCommunicate{Err: fmt.Errorf(snip.Snip(err, s.currentPassword()).Error())}
}
goto RETRY
case io.ErrUnexpectedEOF, io.EOF:
if lastError == nil {
lastError = manipulate.ErrCannotCommunicate{Err: fmt.Errorf(snip.Snip(err, s.currentPassword()).Error())}
}
goto RETRY
}
// We check for error types.
switch uerr.Err.(type) {
case net.Error:
if lastError == nil {
lastError = manipulate.ErrCannotCommunicate{Err: fmt.Errorf(snip.Snip(err, s.currentPassword()).Error())}
}
// check if the connection has been reset by the gateway
// If so we change the retryCurve to be slower
var opErr *net.OpError
var syscallErr *os.SyscallError
if errors.As(uerr.Err, &opErr) &&
errors.As(opErr.Err, &syscallErr) &&
errors.Is(syscallErr.Err, syscall.ECONNRESET) {
retryCurve = s.strongBackoffCurve
}
goto RETRY
case x509.UnknownAuthorityError, x509.CertificateInvalidError, x509.HostnameError, x509.InsecureAlgorithmError, *tls.CertificateVerificationError:
return nil, manipulate.ErrTLS{Err: err}
default:
return nil, manipulate.ErrCannotExecuteQuery{Err: err}
}
}
// We passed the basic error, we have a body.
// We register it so next loop will be clean.
responseBodyCloser = response.Body
// We check for http status codes that triggers a retry
switch response.StatusCode {
case http.StatusBadGateway:
lastError = manipulate.ErrCannotCommunicate{Err: fmt.Errorf("Bad gateway")}
goto RETRY
case http.StatusServiceUnavailable:
lastError = manipulate.ErrCannotCommunicate{Err: fmt.Errorf("Service unavailable")}
goto RETRY
case http.StatusGatewayTimeout:
lastError = manipulate.ErrCannotCommunicate{Err: fmt.Errorf("Gateway timeout")}
goto RETRY
case http.StatusLocked:
lastError = manipulate.ErrLocked{Err: fmt.Errorf("The api has been locked down by the server")}
goto RETRY
case http.StatusRequestTimeout:
lastError = manipulate.ErrCannotCommunicate{Err: fmt.Errorf("Request Timeout")}
goto RETRY
case http.StatusTooManyRequests:
lastError = manipulate.ErrTooManyRequests{Err: fmt.Errorf("Too Many Requests")}
retryCurve = s.strongBackoffCurve
goto RETRY
}
// We backport header info into mctx
s.readHeaders(response, mctx)
// If we have some other errors, we decode them.
if response.StatusCode < 200 || response.StatusCode >= 300 {
errs := elemental.NewErrors()
if err := decodeData(response, &errs); err != nil {
return nil, err
}
if !tokenRenewedOnce && s.tokenManager != nil && (response.StatusCode == http.StatusForbidden || response.StatusCode == http.StatusUnauthorized) {
// This is a special case where we try to renew our token
// in case of 401 or 403 error.
// This is retried twice.
for i := 1; i < 3; i++ {
time.Sleep(time.Duration(i) * time.Second)
err := s.atomicRenewTokenFunc(mctx.Context())
if err == nil {
lastError = errs
tokenRenewedOnce = true
goto RETRY
}
}
}
return nil, errs
}
//
// From now on, this is a success.
//
// If we have content, we return the response.
// The body will be drained by the defered call to closeCurrentBody().
if response.StatusCode == http.StatusNoContent || response.ContentLength == 0 {
responseBodyCloser = nil
return response, nil
}
if dest == nil {
return response, nil
}
// If we have a given dest to decode, we decode it now.
if err := decodeData(response, dest); err != nil {
return nil, err
}
// And we return the response
return response, nil
RETRY:
//
// From now on, this is a failure.
//
// We cancel any pending request context and the pending body.
closeCurrentResponseBody()
cancelCurrentRequest()
// If the manipulator has auto retry disabled we return the last error
if s.disableAutoRetry {
return nil, lastError
}
info := RetryInfo{
URL: requrl,
Method: method,
try: try,
mctx: mctx,
err: lastError,
}
// We run the eventual retry funcs.
if rf := mctx.RetryFunc(); rf != nil {
if rerr := rf(info); rerr != nil {
return nil, rerr
}
} else if s.defaultRetryFunc != nil {
if rerr := s.defaultRetryFunc(info); rerr != nil {
return nil, rerr
}
}
// We check is the main context expired.
select {
case <-mctx.Context().Done():
// If so, we return the last error
return nil, lastError
default:
// Otherwise we sleep backoff and we restart the retry loop.
time.Sleep(backoff.NextWithCurve(try, deadline, retryCurve))
try++
}
}
}
func (s *httpManipulator) registerRenewNotifier(id string, f func(string)) {
s.renewNotifiersLock.Lock()
s.renewNotifiers[id] = f
s.renewNotifiersLock.Unlock()
}
func (s *httpManipulator) unregisterRenewNotifier(id string) {
s.renewNotifiersLock.Lock()
delete(s.renewNotifiers, id)
s.renewNotifiersLock.Unlock()
}
func (s *httpManipulator) setPassword(password string) {
s.renewLock.Lock()
s.password = password
s.renewLock.Unlock()
s.renewNotifiersLock.RLock()
for _, f := range s.renewNotifiers {
if f != nil {
f(password)
}
}
s.renewNotifiersLock.RUnlock()
}
func (s *httpManipulator) currentPassword() string {
s.renewLock.RLock()
p := s.password
s.renewLock.RUnlock()
return p
}
func (s *httpManipulator) renewToken() error {
ctx, cancel := context.WithTimeout(s.ctx, 30*time.Second)
defer cancel()
token, err := s.tokenManager.Issue(ctx)
if err != nil {
return err
}
s.setPassword(token)
return nil
}