-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
89 lines (72 loc) · 2.35 KB
/
config.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
package llmconnector
import (
"fmt"
"github.com/simp-lee/gohttpclient"
"net/url"
"time"
)
// Config is a set of configuration options for all clients.
type Config struct {
APIKey string
ChatURL string
EmbedURL string
CommonConfig
}
// CommonConfig is a set of common configuration options for all clients.
type CommonConfig struct {
Timeout time.Duration
Retries int
// the maximum number of requests allowed per second.
MaxNumRequestPerSecond float64
// the maximum number of requests allowed at once.
MaxNumRequestPerLimit int
ProxyURL string
MaxIdleConns int
MaxConnsPerHost int
IdleConnTimeout time.Duration
}
// DefaultCommonConfig returns a default set of common configuration options.
func DefaultCommonConfig() CommonConfig {
return CommonConfig{
Timeout: 30 * time.Second,
Retries: 3,
MaxNumRequestPerSecond: 10,
MaxNumRequestPerLimit: 10,
MaxIdleConns: 100,
MaxConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
}
}
// createClient creates a new HTTP client with the given configuration options.
func createClient(config CommonConfig, apiKey string) (*gohttpclient.Client, error) {
var options []gohttpclient.ClientOption
if config.Timeout > 0 {
options = append(options, gohttpclient.WithTimeout(config.Timeout))
}
if config.Retries > 0 {
options = append(options, gohttpclient.WithRetries(config.Retries))
}
if config.MaxNumRequestPerLimit > 0 && config.MaxNumRequestPerSecond > 0 {
options = append(options, gohttpclient.WithRateLimit(config.MaxNumRequestPerSecond, config.MaxNumRequestPerLimit))
}
if config.ProxyURL != "" {
proxyURL, err := url.Parse(config.ProxyURL)
if err != nil {
return nil, fmt.Errorf("invalid proxy URL: %w", err)
}
options = append(options, gohttpclient.WithProxy(proxyURL))
}
if config.MaxIdleConns > 0 {
options = append(options, gohttpclient.WithMaxIdleConns(config.MaxIdleConns))
}
if config.MaxConnsPerHost > 0 {
options = append(options, gohttpclient.WithMaxConnsPerHost(config.MaxConnsPerHost))
}
if config.IdleConnTimeout > 0 {
options = append(options, gohttpclient.WithIdleConnTimeout(config.IdleConnTimeout))
}
client := gohttpclient.NewClient(options...)
client.SetHeader("Authorization", fmt.Sprintf("Bearer %s", apiKey))
client.SetHeader("Content-Type", "application/json")
return client, nil
}