Skip to content

Commit

Permalink
Make gRPC ServerParameters configurable (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
abatilo authored Sep 7, 2022
1 parent 540cbce commit f755add
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 3 deletions.
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,14 @@ type Config interface {

// GetQueryAuthToken returns the token that must be used to access the /query endpoints
GetQueryAuthToken() string

GetGRPCMaxConnectionIdle() time.Duration

GetGRPCMaxConnectionAge() time.Duration

GetGRPCMaxConnectionAgeGrace() time.Duration

GetGRPCTime() time.Duration

GetGRPCTimeout() time.Duration
}
46 changes: 46 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,49 @@ func TestQueryAuthToken(t *testing.T) {

assert.Equal(t, "MySeekretToken", c.GetQueryAuthToken())
}

func TestGRPCServerParameters(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)

configFile, err := ioutil.TempFile(tmpDir, "*.toml")
assert.NoError(t, err)

_, err = configFile.Write([]byte(`
[GRPCServerParameters]
MaxConnectionIdle = "1m"
MaxConnectionAge = "2m"
MaxConnectionAgeGrace = "3m"
Time = "4m"
Timeout = "5m"
[InMemCollector]
CacheCapacity=1000
[HoneycombMetrics]
MetricsHoneycombAPI="http://honeycomb.io"
MetricsAPIKey="1234"
MetricsDataset="testDatasetName"
MetricsReportingInterval=3
[HoneycombLogger]
LoggerHoneycombAPI="http://honeycomb.io"
LoggerAPIKey="1234"
LoggerDataset="loggerDataset"
`))
assert.NoError(t, err)
configFile.Close()

rulesFile, err := ioutil.TempFile(tmpDir, "*.toml")
assert.NoError(t, err)

c, err := NewConfig(configFile.Name(), rulesFile.Name(), func(err error) {})
assert.NoError(t, err)

assert.Equal(t, 1*time.Minute, c.GetGRPCMaxConnectionIdle())
assert.Equal(t, 2*time.Minute, c.GetGRPCMaxConnectionAge())
assert.Equal(t, 3*time.Minute, c.GetGRPCMaxConnectionAgeGrace())
assert.Equal(t, 4*time.Minute, c.GetGRPCTime())
assert.Equal(t, 5*time.Minute, c.GetGRPCTimeout())
}
52 changes: 52 additions & 0 deletions config/file_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type configContents struct {
EnvironmentCacheTTL time.Duration
DatasetPrefix string
QueryAuthToken string
GRPCServerParameters GRPCServerParameters
}

type InMemoryCollectorCacheCapacity struct {
Expand Down Expand Up @@ -94,6 +95,17 @@ type PeerManagementConfig struct {
RedisIdentifier string
}

// GRPCServerParameters allow you to configure the GRPC ServerParameters used
// by refinery's own GRPC server:
// https://pkg.go.dev/google.golang.org/grpc/keepalive#ServerParameters
type GRPCServerParameters struct {
MaxConnectionIdle time.Duration
MaxConnectionAge time.Duration
MaxConnectionAgeGrace time.Duration
Time time.Duration
Timeout time.Duration
}

// NewConfig creates a new config struct
func NewConfig(config, rules string, errorCallback func(error)) (Config, error) {
c := viper.New()
Expand Down Expand Up @@ -130,6 +142,11 @@ func NewConfig(config, rules string, errorCallback func(error)) (Config, error)
c.SetDefault("HoneycombLogger.LoggerSamplerThroughput", 5)
c.SetDefault("AddHostMetadataToTrace", false)
c.SetDefault("EnvironmentCacheTTL", time.Hour)
c.SetDefault("GRPCServerParameters.MaxConnectionIdle", 1*time.Minute)
c.SetDefault("GRPCServerParameters.MaxConnectionAge", time.Duration(0))
c.SetDefault("GRPCServerParameters.MaxConnectionAgeGrace", time.Duration(0))
c.SetDefault("GRPCServerParameters.Time", 10*time.Second)
c.SetDefault("GRPCServerParameters.Timeout", 2*time.Second)

c.SetConfigFile(config)
err := c.ReadInConfig()
Expand Down Expand Up @@ -791,3 +808,38 @@ func (f *fileConfig) GetQueryAuthToken() string {

return f.conf.QueryAuthToken
}

func (f *fileConfig) GetGRPCMaxConnectionIdle() time.Duration {
f.mux.RLock()
defer f.mux.RUnlock()

return f.conf.GRPCServerParameters.MaxConnectionIdle
}

func (f *fileConfig) GetGRPCMaxConnectionAge() time.Duration {
f.mux.RLock()
defer f.mux.RUnlock()

return f.conf.GRPCServerParameters.MaxConnectionAge
}

func (f *fileConfig) GetGRPCMaxConnectionAgeGrace() time.Duration {
f.mux.RLock()
defer f.mux.RUnlock()

return f.conf.GRPCServerParameters.MaxConnectionAgeGrace
}

func (f *fileConfig) GetGRPCTime() time.Duration {
f.mux.RLock()
defer f.mux.RUnlock()

return f.conf.GRPCServerParameters.Time
}

func (f *fileConfig) GetGRPCTimeout() time.Duration {
f.mux.RLock()
defer f.mux.RUnlock()

return f.conf.GRPCServerParameters.Timeout
}
65 changes: 65 additions & 0 deletions config/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ type MockConfig struct {
EnvironmentCacheTTL time.Duration
DatasetPrefix string
QueryAuthToken string
GRPCMaxConnectionIdle time.Duration
GRPCMaxConnectionAge time.Duration
GRPCMaxConnectionAgeGrace time.Duration
GRPCTime time.Duration
GRPCTimeout time.Duration

Mux sync.RWMutex
}
Expand All @@ -86,77 +91,90 @@ func (m *MockConfig) ReloadConfig() {
callback()
}
}

func (m *MockConfig) RegisterReloadCallback(callback func()) {
m.Mux.Lock()
m.Callbacks = append(m.Callbacks, callback)
m.Mux.Unlock()
}

func (m *MockConfig) GetAPIKeys() ([]string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetAPIKeysVal, m.GetAPIKeysErr
}

func (m *MockConfig) GetCollectorType() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetCollectorTypeVal, m.GetCollectorTypeErr
}

func (m *MockConfig) GetInMemCollectorCacheCapacity() (InMemoryCollectorCacheCapacity, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetInMemoryCollectorCacheCapacityVal, m.GetInMemoryCollectorCacheCapacityErr
}

func (m *MockConfig) GetHoneycombAPI() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetHoneycombAPIVal, m.GetHoneycombAPIErr
}

func (m *MockConfig) GetListenAddr() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetListenAddrVal, m.GetListenAddrErr
}

func (m *MockConfig) GetPeerListenAddr() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetPeerListenAddrVal, m.GetPeerListenAddrErr
}

func (m *MockConfig) GetCompressPeerCommunication() bool {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetCompressPeerCommunicationsVal
}

func (m *MockConfig) GetGRPCListenAddr() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetGRPCListenAddrVal, m.GetGRPCListenAddrErr
}

func (m *MockConfig) GetLoggerType() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetLoggerTypeVal, m.GetLoggerTypeErr
}

func (m *MockConfig) GetHoneycombLoggerConfig() (HoneycombLoggerConfig, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetHoneycombLoggerConfigVal, m.GetHoneycombLoggerConfigErr
}

func (m *MockConfig) GetLoggingLevel() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetLoggingLevelVal, m.GetLoggingLevelErr
}

func (m *MockConfig) GetOtherConfig(name string, iface interface{}) error {
m.Mux.RLock()
defer m.Mux.RUnlock()
Expand All @@ -167,66 +185,77 @@ func (m *MockConfig) GetOtherConfig(name string, iface interface{}) error {
}
return m.GetOtherConfigErr
}

func (m *MockConfig) GetPeers() ([]string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetPeersVal, m.GetPeersErr
}

func (m *MockConfig) GetRedisHost() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetRedisHostVal, m.GetRedisHostErr
}

func (m *MockConfig) GetRedisUsername() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetRedisUsernameVal, m.GetRedisUsernameErr
}

func (m *MockConfig) GetRedisPassword() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetRedisPasswordVal, m.GetRedisPasswordErr
}

func (m *MockConfig) GetUseTLS() (bool, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetUseTLSVal, m.GetUseTLSErr
}

func (m *MockConfig) GetUseTLSInsecure() (bool, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetUseTLSInsecureVal, m.GetUseTLSInsecureErr
}

func (m *MockConfig) GetMetricsType() (string, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetMetricsTypeVal, m.GetMetricsTypeErr
}

func (m *MockConfig) GetHoneycombMetricsConfig() (HoneycombMetricsConfig, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetHoneycombMetricsConfigVal, m.GetHoneycombMetricsConfigErr
}

func (m *MockConfig) GetPrometheusMetricsConfig() (PrometheusMetricsConfig, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetPrometheusMetricsConfigVal, m.GetPrometheusMetricsConfigErr
}

func (m *MockConfig) GetSendDelay() (time.Duration, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()

return m.GetSendDelayVal, m.GetSendDelayErr
}

func (m *MockConfig) GetTraceTimeout() (time.Duration, error) {
m.Mux.RLock()
defer m.Mux.RUnlock()
Expand Down Expand Up @@ -264,6 +293,7 @@ func (m *MockConfig) GetUpstreamBufferSize() int {

return m.GetUpstreamBufferSizeVal
}

func (m *MockConfig) GetPeerBufferSize() int {
m.Mux.RLock()
defer m.Mux.RUnlock()
Expand Down Expand Up @@ -354,3 +384,38 @@ func (f *MockConfig) GetQueryAuthToken() string {

return f.QueryAuthToken
}

func (f *MockConfig) GetGRPCMaxConnectionIdle() time.Duration {
f.Mux.RLock()
defer f.Mux.RUnlock()

return f.GRPCMaxConnectionIdle
}

func (f *MockConfig) GetGRPCMaxConnectionAge() time.Duration {
f.Mux.RLock()
defer f.Mux.RUnlock()

return f.GRPCMaxConnectionAge
}

func (f *MockConfig) GetGRPCMaxConnectionAgeGrace() time.Duration {
f.Mux.RLock()
defer f.Mux.RUnlock()

return f.GRPCMaxConnectionAgeGrace
}

func (f *MockConfig) GetGRPCTime() time.Duration {
f.Mux.RLock()
defer f.Mux.RUnlock()

return f.GRPCTime
}

func (f *MockConfig) GetGRPCTimeout() time.Duration {
f.Mux.RLock()
defer f.Mux.RUnlock()

return f.GRPCTimeout
}
Loading

0 comments on commit f755add

Please sign in to comment.