-
Notifications
You must be signed in to change notification settings - Fork 1
/
tomoe.go
110 lines (91 loc) · 2.24 KB
/
tomoe.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
package tomoe
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"sync"
"time"
)
// NewClient initializes a new HTTP client with a base URL and timeout.
func NewClient(baseURL string, timeout time.Duration, retries int, backoff time.Duration, headers map[string]string) *Client {
return &Client{
baseURL: baseURL,
httpClient: &http.Client{
Timeout: timeout,
},
headers: headers,
retries: retries,
backoff: backoff,
}
}
func (c *Client) ParallelRequests(ctx context.Context, opts []RequestOptions) ([]*http.Response, error) {
results := make([]*http.Response, len(opts))
errCh := make(chan error, len(opts))
var wg sync.WaitGroup
for i, opt := range opts {
wg.Add(1)
go func(i int, opt RequestOptions) {
defer wg.Done()
res, err := c.Do(ctx, opt)
if err != nil {
errCh <- err
return
}
results[i] = res
}(i, opt)
}
go func() {
wg.Wait()
close(errCh)
}()
for err := range errCh {
return nil, err
}
return results, nil
}
func (c *Client) Do(ctx context.Context, opts RequestOptions) (*http.Response, error) {
var lastErr error
for attempt := 1; attempt <= c.retries; attempt++ {
if attempt > 1 {
time.Sleep(c.backoff) // Exponential backoff
}
result, err := c.executeRequest(ctx, opts)
if err != nil {
lastErr = fmt.Errorf("attempt %d failed: %w", attempt, err)
continue
}
return result, nil
}
return nil, fmt.Errorf("all retries failed: %v", lastErr)
}
func (c *Client) executeRequest(ctx context.Context, opts RequestOptions) (*http.Response, error) {
// Construct the URL
reqURL, err := url.Parse(c.baseURL + opts.Path)
if err != nil {
return nil, fmt.Errorf("invalid URL: %w", err)
}
// Add query parameters
q := reqURL.Query()
for key, value := range opts.QueryParams {
q.Add(key, value)
}
reqURL.RawQuery = q.Encode()
// Create the request
req, err := http.NewRequestWithContext(ctx, opts.Method, reqURL.String(), opts.Body)
if err != nil {
log.Println(err)
return nil, fmt.Errorf("failed to create request: %v", err)
}
// Set headers
for key, value := range c.headers {
req.Header.Set(key, value)
}
// Perform the request
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
return resp, nil
}