Skip to content

Commit 1c77e8c

Browse files
Various improvements
1 parent 0edbd3f commit 1c77e8c

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/KillianMeersman/chaperone
22

3-
go 1.22.0
3+
go 1.23.0
4+
45
toolchain go1.23.7
56

67
require (

pkg/proxy/niceclient.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func (c *NiceClient) RoundTripWithOptions(req *http.Request, options *RequestOpt
127127
// Start span for retry loop
128128
ctx, span := telemetry.Tracer.Start(ctx, "HTTP request retry loop", trace.WithAttributes(attribute.String("http.method", req.Method), attribute.String("http.url", req.URL.String())))
129129
defer span.End()
130+
req = req.WithContext(ctx)
130131

131132
for {
132133
logger.Debug("waiting to make request")
@@ -139,7 +140,7 @@ func (c *NiceClient) RoundTripWithOptions(req *http.Request, options *RequestOpt
139140
logger.Debug("Making request")
140141
ctx, span := telemetry.Tracer.Start(ctx, req.Method, trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(attribute.String("http.method", req.Method), attribute.String("http.url", req.URL.String())))
141142
defer span.End()
142-
req = req.WithContext(ctx)
143+
req := req.WithContext(ctx)
143144

144145
res, err := c.roundtripper.RoundTrip(req)
145146
switch err {
@@ -160,7 +161,7 @@ func (c *NiceClient) RoundTripWithOptions(req *http.Request, options *RequestOpt
160161
}
161162

162163
span.SetAttributes(attribute.Int("http.status_code", res.StatusCode))
163-
logger = logger.With("status_code", fmt.Sprint(res.StatusCode))
164+
logger := logger.With("status_code", fmt.Sprint(res.StatusCode))
164165
logger.Debug("got response", "status_code", fmt.Sprint(res.StatusCode))
165166

166167
switch res.StatusCode {
@@ -180,6 +181,8 @@ func (c *NiceClient) RoundTripWithOptions(req *http.Request, options *RequestOpt
180181
jitterDuration := time.Duration(jitterMilliseconds) * time.Millisecond
181182
blockDuration := time.Duration(waitTime + jitterDuration)
182183
span.AddEvent("blocking throttle", trace.WithAttributes(attribute.Int("duration_seconds", int(blockDuration.Seconds()))))
184+
185+
logger.Warning("blocking throttle", "seconds", fmt.Sprint(blockDuration.Seconds()))
183186
c.throttle.Block(res.Request, blockDuration)
184187
case 301, 302, 307, 308:
185188
// Handle redirects.

pkg/proxy/niceclient_test.go

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"time"
1111
)
1212

13-
type mockRountTripper struct {
13+
type mockRoundTripper struct {
1414
Response []byte
1515
}
1616

17-
func (m *mockRountTripper) RoundTrip(r *http.Request) (*http.Response, error) {
17+
func (m *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
1818
bodyReader := bytes.NewReader(m.Response)
1919
bodyReadCloser := io.NopCloser(bodyReader)
2020

@@ -29,7 +29,7 @@ func TestNiceClient(t *testing.T) {
2929
throttle := NewMemoryHTTPThrottle()
3030
cache := NewMemoryHTTPCache(context.Background(), 1000)
3131

32-
roundTripper := &mockRountTripper{
32+
roundTripper := &mockRoundTripper{
3333
Response: []byte("test"),
3434
}
3535

@@ -47,12 +47,67 @@ func TestNiceClient(t *testing.T) {
4747
if err != nil {
4848
t.Fatal(err)
4949
}
50+
5051
throttle.Block(&http.Request{
5152
URL: url,
5253
Method: "GET",
5354
}, 3*time.Second)
55+
5456
client.RoundTrip(req)
55-
if time.Since(start) < 4*time.Second {
57+
58+
if time.Since(start) < 3*time.Second {
5659
t.FailNow()
5760
}
5861
}
62+
63+
func TestNiceClientThrottle(t *testing.T) {
64+
cache := NewMemoryHTTPCache(context.Background(), 1000)
65+
throttle := NewMemoryHTTPThrottle()
66+
67+
throttleDuration := 20 * time.Millisecond
68+
69+
url, err := url.Parse("http://example.com")
70+
if err != nil {
71+
panic(err)
72+
}
73+
throttle.SetThrottle(&http.Request{
74+
Method: "GET",
75+
URL: url,
76+
}, throttleDuration)
77+
throttle.SetThrottle(&http.Request{
78+
Method: "POST",
79+
URL: url,
80+
}, throttleDuration*100)
81+
82+
url, err = url.Parse("http://example.com/test")
83+
if err != nil {
84+
panic(err)
85+
}
86+
throttle.SetThrottle(&http.Request{
87+
Method: "GET",
88+
URL: url,
89+
}, throttleDuration*100)
90+
91+
roundTripper := &mockRoundTripper{
92+
Response: []byte("test"),
93+
}
94+
95+
client := NewNiceClient(context.Background(), roundTripper, throttle, cache)
96+
97+
for i := 0; i < 100; i++ {
98+
req, _ := http.NewRequest("GET", "http://example.com", nil)
99+
start := time.Now()
100+
client.RoundTrip(req)
101+
102+
timeTaken := time.Since(start)
103+
104+
if timeTaken < throttleDuration-5*time.Millisecond {
105+
t.Fatal("not enough time since last request")
106+
}
107+
108+
if timeTaken > throttleDuration+5*time.Millisecond {
109+
t.Fatal("too much time since last request")
110+
}
111+
}
112+
113+
}

0 commit comments

Comments
 (0)