/
config.go
111 lines (90 loc) · 2.78 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package publicapi
import (
"time"
)
type Config struct {
// These are TCP connection deadlines and not HTTP timeouts. They don't control the time it takes for our handlers
// to complete. Deadlines operate on the connection, so our server will fail to return a result only after
// the handlers try to access connection properties
// ReadHeaderTimeout is the amount of time allowed to read request headers
ReadHeaderTimeout time.Duration
// ReadTimeout is the maximum duration for reading the entire request, including the body
ReadTimeout time.Duration
// WriteTimeout is the maximum duration before timing out writes of the response.
// It doesn't cancel the context and doesn't stop handlers from running even after failing the request.
// It is for added safety and should be a bit longer than the request handler timeout for better error handling.
WriteTimeout time.Duration
// This represents maximum duration for handlers to complete, or else fail the request with 503 error code.
RequestHandlerTimeout time.Duration
// MaxBytesToReadInBody is used by safeHandlerFuncWrapper as the max size of body
MaxBytesToReadInBody string
// ThrottleLimit is the maximum number of requests per second
ThrottleLimit int
// Protocol
Protocol string
// LogLevel is the minimum log level to log requests
LogLevel string
}
// defaultConfig default values for Config
var defaultConfig = Config{
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 20 * time.Second,
WriteTimeout: 45 * time.Second,
RequestHandlerTimeout: 30 * time.Second,
MaxBytesToReadInBody: "10MB",
ThrottleLimit: 1000,
Protocol: "http",
LogLevel: "info",
}
// DefaultConfig returns the default configuration for the public API server.
func DefaultConfig() Config {
return defaultConfig
}
type Option func(*Config)
func WithReadHeaderTimeout(t time.Duration) Option {
return func(c *Config) {
c.ReadHeaderTimeout = t
}
}
func WithReadTimeout(t time.Duration) Option {
return func(c *Config) {
c.ReadTimeout = t
}
}
func WithWriteTimeout(t time.Duration) Option {
return func(c *Config) {
c.WriteTimeout = t
}
}
func WithRequestHandlerTimeout(t time.Duration) Option {
return func(c *Config) {
c.RequestHandlerTimeout = t
}
}
func WithMaxBytesToReadInBody(size string) Option {
return func(c *Config) {
c.MaxBytesToReadInBody = size
}
}
func WithThrottleLimit(limit int) Option {
return func(c *Config) {
c.ThrottleLimit = limit
}
}
func WithProtocol(protocol string) Option {
return func(c *Config) {
c.Protocol = protocol
}
}
func WithLogLevel(logLevel string) Option {
return func(c *Config) {
c.LogLevel = logLevel
}
}
func NewConfig(opts ...Option) *Config {
config := DefaultConfig()
for _, opt := range opts {
opt(&config)
}
return &config
}