Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/common/net/config.go
4096 views
1
// Package http contains a River serializable definition of the weaveworks weaveworks config in
2
// https://github.com/weaveworks/common/blob/master/server/server.go#L62.
3
package net
4
5
import (
6
"flag"
7
"time"
8
9
weaveworks "github.com/weaveworks/common/server"
10
)
11
12
const (
13
DefaultHTTPPort = 8080
14
DefaultGRPCPort = 8081
15
)
16
17
// ServerConfig is a River configuration that allows one to configure a weaveworks.Server. It
18
// exposes a subset of the available configurations.
19
type ServerConfig struct {
20
// HTTP configures the HTTP weaveworks. Note that despite the block being present or not,
21
// the weaveworks is always started.
22
HTTP *HTTPConfig `river:"http,block,optional"`
23
24
// GRPC configures the gRPC weaveworks. Note that despite the block being present or not,
25
// the weaveworks is always started.
26
GRPC *GRPCConfig `river:"grpc,block,optional"`
27
28
// GracefulShutdownTimeout configures a timeout to gracefully shut down the server.
29
GracefulShutdownTimeout time.Duration `river:"graceful_shutdown_timeout,attr,optional"`
30
}
31
32
// HTTPConfig configures the HTTP weaveworks started by weaveworks.Server.
33
type HTTPConfig struct {
34
ListenAddress string `river:"listen_address,attr,optional"`
35
ListenPort int `river:"listen_port,attr,optional"`
36
ConnLimit int `river:"conn_limit,attr,optional"`
37
ServerReadTimeout time.Duration `river:"server_read_timeout,attr,optional"`
38
ServerWriteTimeout time.Duration `river:"server_write_timeout,attr,optional"`
39
ServerIdleTimeout time.Duration `river:"server_idle_timeout,attr,optional"`
40
}
41
42
// Into applies the configs from HTTPConfig into a weaveworks.Into.
43
func (h *HTTPConfig) Into(c *weaveworks.Config) {
44
c.HTTPListenAddress = h.ListenAddress
45
c.HTTPListenPort = h.ListenPort
46
c.HTTPConnLimit = h.ConnLimit
47
c.HTTPServerReadTimeout = h.ServerReadTimeout
48
c.HTTPServerWriteTimeout = h.ServerWriteTimeout
49
c.HTTPServerIdleTimeout = h.ServerIdleTimeout
50
}
51
52
// GRPCConfig configures the gRPC weaveworks started by weaveworks.Server.
53
type GRPCConfig struct {
54
ListenAddress string `river:"listen_address,attr,optional"`
55
ListenPort int `river:"listen_port,attr,optional"`
56
ConnLimit int `river:"conn_limit,attr,optional"`
57
MaxConnectionAge time.Duration `river:"max_connection_age,attr,optional"`
58
MaxConnectionAgeGrace time.Duration `river:"max_connection_age_grace,attr,optional"`
59
MaxConnectionIdle time.Duration `river:"max_connection_idle,attr,optional"`
60
ServerMaxRecvMsg int `river:"server_max_recv_msg_size,attr,optional"`
61
ServerMaxSendMsg int `river:"server_max_send_msg_size,attr,optional"`
62
ServerMaxConcurrentStreams uint `river:"server_max_concurrent_streams,attr,optional"`
63
}
64
65
// Into applies the configs from GRPCConfig into a weaveworks.Into.
66
func (g *GRPCConfig) Into(c *weaveworks.Config) {
67
c.GRPCListenAddress = g.ListenAddress
68
c.GRPCListenPort = g.ListenPort
69
c.GRPCConnLimit = g.ConnLimit
70
c.GRPCServerMaxConnectionAge = g.MaxConnectionAge
71
c.GRPCServerMaxConnectionAgeGrace = g.MaxConnectionAgeGrace
72
c.GRPCServerMaxConnectionIdle = g.MaxConnectionIdle
73
c.GPRCServerMaxRecvMsgSize = g.ServerMaxRecvMsg
74
c.GRPCServerMaxSendMsgSize = g.ServerMaxSendMsg
75
c.GPRCServerMaxConcurrentStreams = g.ServerMaxConcurrentStreams
76
}
77
78
func (c *ServerConfig) UnmarshalRiver(f func(v interface{}) error) error {
79
type config ServerConfig
80
if err := f((*config)(c)); err != nil {
81
return err
82
}
83
84
return nil
85
}
86
87
// Convert converts the River-based ServerConfig into a weaveworks.Config object.
88
func (c *ServerConfig) Convert() weaveworks.Config {
89
cfg := newDefaultConfig()
90
if c.HTTP != nil {
91
c.HTTP.Into(&cfg)
92
}
93
if c.GRPC != nil {
94
c.GRPC.Into(&cfg)
95
}
96
// If set, override. Don't allow a zero-value since it configure a context.WithTimeout, so the user should at least
97
// give a >0 value to it
98
if c.GracefulShutdownTimeout != 0 {
99
cfg.ServerGracefulShutdownTimeout = c.GracefulShutdownTimeout
100
}
101
return cfg
102
}
103
104
// newDefaultConfig creates a new weaveworks.Config object with some overridden defaults.
105
func newDefaultConfig() weaveworks.Config {
106
c := weaveworks.Config{}
107
c.RegisterFlags(flag.NewFlagSet("empty", flag.ContinueOnError))
108
c.HTTPListenPort = DefaultHTTPPort
109
c.GRPCListenPort = DefaultGRPCPort
110
// By default, do not register instrumentation since every metric is later registered
111
// inside a custom register
112
c.RegisterInstrumentation = false
113
return c
114
}
115
116