This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
director.go
189 lines (155 loc) · 5.31 KB
/
director.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
// Package http provides the http implementation of the Director interface.
package http
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/featurebasedb/featurebase/v3/dax"
"github.com/featurebasedb/featurebase/v3/dax/controller"
"github.com/featurebasedb/featurebase/v3/errors"
"github.com/featurebasedb/featurebase/v3/logger"
)
// Ensure type implements interface.
var _ controller.Director = (*Director)(nil)
// Director is an http implementation of the Director interface.
type Director struct {
// directivePath is the path portion of the URI to which directives should
// be POSTed.
directivePath string
// snapshotRequestPath is the path portion of the URI to which snapshot
// requests should be POSTed.
snapshotRequestPath string
client *http.Client
logger logger.Logger
}
func NewDirector(cfg DirectorConfig) *Director {
var logr = logger.NopLogger
if cfg.Logger != nil {
logr = cfg.Logger
}
return &Director{
directivePath: cfg.DirectivePath,
snapshotRequestPath: cfg.SnapshotRequestPath,
logger: logr,
client: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 2 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 3 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
},
}
}
type DirectorConfig struct {
DirectivePath string
SnapshotRequestPath string
Logger logger.Logger
}
func (d *Director) SendDirective(ctx context.Context, dir *dax.Directive) error {
url := fmt.Sprintf("%s/%s", dir.Address.WithScheme("http"), d.directivePath)
d.logger.Printf("SEND HTTP directive to: %s\n", url)
d.logger.Debugf("Directive: %+v", dir)
// Encode the request.
postBody, err := json.Marshal(dir)
if err != nil {
return errors.Wrap(err, "marshalling directive to json")
}
requestBody := bytes.NewBuffer(postBody)
// Post the request.
request, _ := http.NewRequest(http.MethodPost, url, requestBody)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Accept", "application/json")
resp, err := d.client.Do(request)
if err != nil {
return errors.Wrap(err, "doing send directive")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
}
return nil
}
func (d *Director) SendSnapshotShardDataRequest(ctx context.Context, req *dax.SnapshotShardDataRequest) error {
url := fmt.Sprintf("%s/%s/shard-data", req.Address.WithScheme("http"), d.snapshotRequestPath)
// Encode the request.
postBody, err := json.Marshal(req)
if err != nil {
return errors.Wrap(err, "marshalling snapshot shard data request to json")
}
requestBody := bytes.NewBuffer(postBody)
// Post the request.
request, _ := http.NewRequest(http.MethodPost, url, requestBody)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Accept", "application/json")
resp, err := d.client.Do(request)
if err != nil {
return errors.Wrap(err, "doing snapshot shard data request")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
}
return nil
}
func (d *Director) SendSnapshotTableKeysRequest(ctx context.Context, req *dax.SnapshotTableKeysRequest) error {
url := fmt.Sprintf("%s/%s/table-keys", req.Address.WithScheme("http"), d.snapshotRequestPath)
d.logger.Printf("SEND HTTP snapshot table keys request to: %s\n", url)
// Encode the request.
postBody, err := json.Marshal(req)
if err != nil {
return errors.Wrap(err, "marshalling snapshot table keys request to json")
}
requestBody := bytes.NewBuffer(postBody)
// Post the request.
request, _ := http.NewRequest(http.MethodPost, url, requestBody)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Accept", "application/json")
resp, err := d.client.Do(request)
if err != nil {
return errors.Wrap(err, "doing snapshot table keys request")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
}
return nil
}
func (d *Director) SendSnapshotFieldKeysRequest(ctx context.Context, req *dax.SnapshotFieldKeysRequest) error {
url := fmt.Sprintf("%s/%s/field-keys", req.Address.WithScheme("http"), d.snapshotRequestPath)
d.logger.Printf("SEND HTTP snapshot field keys request to: %s\n", url)
// Encode the request.
postBody, err := json.Marshal(req)
if err != nil {
return errors.Wrap(err, "marshalling snapshot field keys request to json")
}
requestBody := bytes.NewBuffer(postBody)
// Post the request.
request, _ := http.NewRequest(http.MethodPost, url, requestBody)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Accept", "application/json")
resp, err := d.client.Do(request)
if err != nil {
return errors.Wrap(err, "doing snapshot field keys request")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
return errors.Errorf("status code: %d: %s", resp.StatusCode, b)
}
return nil
}