-
Notifications
You must be signed in to change notification settings - Fork 307
/
client.go
149 lines (123 loc) · 3.21 KB
/
client.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
package features
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime"
"time"
"github.com/cenkalti/backoff"
"github.com/rudderlabs/rudder-server/services/controlplane/identity"
"github.com/rudderlabs/rudder-server/utils/httputil"
)
var (
defaultTimeout = 30 * time.Second
defaultMaxRetries = 3
)
type OptFn func(c *Client)
func WithHTTPClient(httpClient *http.Client) OptFn {
return func(c *Client) {
c.client = httpClient
}
}
func WithTimeout(timeout time.Duration) OptFn {
return func(c *Client) {
c.client.Timeout = timeout
}
}
func WithMaxRetries(retries int) OptFn {
return func(c *Client) {
c.retries = retries
}
}
type Client struct {
client *http.Client
retries int
ua string
url string
identity identity.Identifier
}
type payloadSchema struct {
Components []componentSchema `json:"components"`
}
type componentSchema struct {
Name string `json:"name"`
Features []string `json:"features"`
}
func hostname() string {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
return hostname
}
func NewClient(baseURL string, identity identity.Identifier, fns ...OptFn) *Client {
c := &Client{
url: baseURL,
identity: identity,
client: &http.Client{
Timeout: defaultTimeout,
},
retries: defaultMaxRetries,
ua: fmt.Sprintf("Go-http-client/1.1; %s; control-plane/features; %s", runtime.Version(), hostname()),
}
for _, fn := range fns {
fn(c)
}
return c
}
type PerComponent = map[string][]string
func (c *Client) Send(ctx context.Context, component string, features []string) error {
var url string
switch t := c.identity.(type) {
case *identity.Namespace:
url = fmt.Sprintf("%s/data-plane/v1/namespaces/%s/settings", c.url, c.identity.ID())
case *identity.Workspace:
url = fmt.Sprintf("%s/data-plane/v1/workspaces/%s/settings", c.url, c.identity.ID())
default:
return fmt.Errorf("identity not supported %T", t)
}
payload := payloadSchema{
Components: []componentSchema{
{
Name: component,
Features: features,
},
},
}
body, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("could not marshal payload: %w", err)
}
backoffWithMaxRetry := backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), uint64(c.retries)), ctx)
return backoff.Retry(func() error {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", c.ua)
req.SetBasicAuth(c.identity.BasicAuth())
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("doing http request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
// we don't expect a body, unless there is an error
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read response body: %w", err)
}
err = fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(b))
if !httputil.RetriableStatus(resp.StatusCode) {
return backoff.Permanent(fmt.Errorf("non retriable: %w", err))
}
return err
}
return err
}, backoffWithMaxRetry)
}