-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathoptions.go
More file actions
59 lines (49 loc) · 1.56 KB
/
options.go
File metadata and controls
59 lines (49 loc) · 1.56 KB
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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package api
import (
"time"
)
// Option is an additional argument to method New to change the behavior
// of API processing.
type Option interface {
Apply(*Config)
}
// Config is a set of knobs to change the behavior of API processing.
// In other words, Config aggregates all the Option-s into one structure.
type Config struct {
// EventTimeout defines time duration for API request to be processed.
// If a request is not processed in time, then an error is returned to the
// client.
EventTimeout time.Duration
// ServerIDFunc defines a custom server ID in API responses.
ServerIDFunc ServerIDFunc
}
// OptionEventTimeout defines time duration for API request to be processed.
// If a request is not processed in time, then an error is returned to the
// client.
type OptionEventTimeout time.Duration
// Apply implements Option.
func (opt OptionEventTimeout) Apply(config *Config) {
config.EventTimeout = (time.Duration)(opt)
}
// OptionServerID defines a custom server ID in API responses.
type OptionServerID string
// Apply implements Option.
func (opt OptionServerID) Apply(config *Config) {
config.ServerIDFunc = func() string {
return string(opt)
}
}
// getConfig converts a set of Option-s into one structure "Config".
func getConfig(opts ...Option) Config {
result := Config{
EventTimeout: DefaultEventTimeout,
}
for _, opt := range opts {
opt.Apply(&result)
}
return result
}