-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcallback.go
72 lines (61 loc) · 1.9 KB
/
callback.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
package main
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"time"
"github.com/cenkalti/backoff"
)
type ReqConfig struct {
Url string `envconfig:"REQ_URL"`
Method string `envconfig:"REQ_METHOD" default:"GET"`
Payload []byte `envconfig:"REQ_PAYLOAD"`
RetryTotal uint `envconfig:"REQ_RETRY_TOTAL" default:"5"`
RetryBackoffFactor float64 `envconfig:"REQ_RETRY_BACKOFF_FACTOR" default:"1.1"`
Timeout string `envconfig:"REQ_TIMEOUT" default:"10"`
Username string `envconfig:"REQ_USERNAME"`
Password string `envconfig:"REQ_PASSWORD"`
SkipTlsVerify bool `envconfig:"REQ_SKIP_TLS_VERIFY" default:"false"`
}
func runCallback(reqConfig ReqConfig) {
if reqConfig.Url == "" {
return
}
client := &http.Client{}
dialer := net.Dialer{}
if reqConfig.SkipTlsVerify {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, "tcp4", addr)
},
}
}
req, err := http.NewRequest(reqConfig.Method, reqConfig.Url, bytes.NewBuffer(reqConfig.Payload))
if err != nil {
log.Printf("failed to create request struct: %s", err)
}
if reqConfig.Username != "" || reqConfig.Password != "" {
req.SetBasicAuth(reqConfig.Username, reqConfig.Password)
}
expBackoff := backoff.NewExponentialBackOff()
expBackoff.MaxElapsedTime = 10 * time.Second
expBackoff.Multiplier = reqConfig.RetryBackoffFactor
err = backoff.Retry(func() error {
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed retry to make req: %w", err)
}
log.Printf("Made request to %s and got status code %d", reqConfig.Url, resp.StatusCode)
return nil
}, expBackoff)
if err != nil {
log.Printf("permanent failed to make request: %s", err)
}
}