forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
103 lines (83 loc) · 2.8 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
package subscriber
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/influxdata/influxdb/monitor/diagnostics"
"github.com/influxdata/influxdb/toml"
)
const (
// DefaultHTTPTimeout is the default HTTP timeout for a Config.
DefaultHTTPTimeout = 30 * time.Second
// DefaultWriteConcurrency is the default write concurrency for a Config.
DefaultWriteConcurrency = 40
// DefaultWriteBufferSize is the default write buffer size for a Config.
DefaultWriteBufferSize = 1000
)
// Config represents a configuration of the subscriber service.
type Config struct {
// Whether to enable to Subscriber service
Enabled bool `toml:"enabled"`
HTTPTimeout toml.Duration `toml:"http-timeout"`
// InsecureSkipVerify gets passed to the http client, if true, it will
// skip https certificate verification. Defaults to false
InsecureSkipVerify bool `toml:"insecure-skip-verify"`
// configure the path to the PEM encoded CA certs file. If the
// empty string, the default system certs will be used
CaCerts string `toml:"ca-certs"`
// The number of writer goroutines processing the write channel.
WriteConcurrency int `toml:"write-concurrency"`
// The number of in-flight writes buffered in the write channel.
WriteBufferSize int `toml:"write-buffer-size"`
}
// NewConfig returns a new instance of a subscriber config.
func NewConfig() Config {
return Config{
Enabled: true,
HTTPTimeout: toml.Duration(DefaultHTTPTimeout),
InsecureSkipVerify: false,
CaCerts: "",
WriteConcurrency: DefaultWriteConcurrency,
WriteBufferSize: DefaultWriteBufferSize,
}
}
// Validate returns an error if the config is invalid.
func (c Config) Validate() error {
if c.HTTPTimeout <= 0 {
return errors.New("http-timeout must be greater than 0")
}
if c.CaCerts != "" && !fileExists(c.CaCerts) {
abspath, err := filepath.Abs(c.CaCerts)
if err != nil {
return fmt.Errorf("ca-certs file %s does not exist. Wrapped Error: %v", c.CaCerts, err)
}
return fmt.Errorf("ca-certs file %s does not exist", abspath)
}
if c.WriteBufferSize <= 0 {
return errors.New("write-buffer-size must be greater than 0")
}
if c.WriteConcurrency <= 0 {
return errors.New("write-concurrency must be greater than 0")
}
return nil
}
func fileExists(fileName string) bool {
info, err := os.Stat(fileName)
return err == nil && !info.IsDir()
}
// Diagnostics returns a diagnostics representation of a subset of the Config.
func (c Config) Diagnostics() (*diagnostics.Diagnostics, error) {
if !c.Enabled {
return diagnostics.RowFromMap(map[string]interface{}{
"enabled": false,
}), nil
}
return diagnostics.RowFromMap(map[string]interface{}{
"enabled": true,
"http-timeout": c.HTTPTimeout,
"write-concurrency": c.WriteConcurrency,
"write-buffer-size": c.WriteBufferSize,
}), nil
}