-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
59 lines (48 loc) · 1.07 KB
/
options.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
package handler
import (
"log"
"net/http"
)
// Option is a common interface for defining options
// to change default Handler's behaviour.
type Option interface {
apply(h *Handler)
}
type clientOption struct {
client *http.Client
}
// WithClient creates new Option which replaces
// default HTTP client with user-provided one.
func WithClient(client *http.Client) Option {
return &clientOption{
client: client,
}
}
func (opt *clientOption) apply(h *Handler) {
h.client = opt.client
}
type loggerOption struct {
logger *log.Logger
}
// WithLogger creates new Option which sets custom logger.
func WithLogger(logger *log.Logger) Option {
return &loggerOption{
logger: logger,
}
}
func (opt *loggerOption) apply(h *Handler) {
h.logger = opt.logger
}
type limitRequestsOption struct {
limit int
}
// LimitRequests creates new Option which sets number
// of Handler's maximum concurrent incoming requests
func LimitRequests(limit int) Option {
return &limitRequestsOption{
limit: limit,
}
}
func (opt *limitRequestsOption) apply(h *Handler) {
h.maxRequests = opt.limit
}