Skip to content

Commit c01a05d

Browse files
authored
Merge pull request #375 from gatewayd-io/refactor-getters
Refactor getter functions
2 parents ffb1442 + 91b3fe1 commit c01a05d

File tree

9 files changed

+205
-322
lines changed

9 files changed

+205
-322
lines changed

cmd/run.go

Lines changed: 128 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,33 @@ var runCmd = &cobra.Command{
210210
// Create and initialize loggers from the config.
211211
for name, cfg := range conf.Global.Loggers {
212212
loggers[name] = logging.NewLogger(runCtx, logging.LoggerConfig{
213-
Output: cfg.GetOutput(),
214-
Level: cfg.GetLevel(),
215-
TimeFormat: cfg.GetTimeFormat(),
216-
ConsoleTimeFormat: cfg.GetConsoleTimeFormat(),
217-
NoColor: cfg.NoColor,
218-
FileName: cfg.FileName,
219-
MaxSize: cfg.MaxSize,
220-
MaxBackups: cfg.MaxBackups,
221-
MaxAge: cfg.MaxAge,
222-
Compress: cfg.Compress,
223-
LocalTime: cfg.LocalTime,
224-
SyslogPriority: cfg.GetSyslogPriority(),
225-
RSyslogNetwork: cfg.RSyslogNetwork,
226-
RSyslogAddress: cfg.RSyslogAddress,
213+
Output: cfg.GetOutput(),
214+
Level: config.If[zerolog.Level](
215+
config.Exists[string, zerolog.Level](config.LogLevels, cfg.Level),
216+
config.LogLevels[cfg.Level],
217+
config.LogLevels[config.DefaultLogLevel],
218+
),
219+
TimeFormat: config.If[string](
220+
config.Exists[string, string](config.TimeFormats, cfg.TimeFormat),
221+
config.TimeFormats[cfg.TimeFormat],
222+
config.DefaultTimeFormat,
223+
),
224+
ConsoleTimeFormat: config.If[string](
225+
config.Exists[string, string](
226+
config.ConsoleTimeFormats, cfg.ConsoleTimeFormat),
227+
config.ConsoleTimeFormats[cfg.ConsoleTimeFormat],
228+
config.DefaultConsoleTimeFormat,
229+
),
230+
NoColor: cfg.NoColor,
231+
FileName: cfg.FileName,
232+
MaxSize: cfg.MaxSize,
233+
MaxBackups: cfg.MaxBackups,
234+
MaxAge: cfg.MaxAge,
235+
Compress: cfg.Compress,
236+
LocalTime: cfg.LocalTime,
237+
SyslogPriority: cfg.GetSyslogPriority(),
238+
RSyslogNetwork: cfg.RSyslogNetwork,
239+
RSyslogAddress: cfg.RSyslogAddress,
227240
})
228241
}
229242

@@ -239,10 +252,26 @@ var runCmd = &cobra.Command{
239252
// The plugins are loaded and hooks registered before the configuration is loaded.
240253
pluginRegistry = plugin.NewRegistry(
241254
runCtx,
242-
conf.Plugin.GetPluginCompatibilityPolicy(),
243-
conf.Plugin.GetVerificationPolicy(),
244-
conf.Plugin.GetAcceptancePolicy(),
245-
conf.Plugin.GetTerminationPolicy(),
255+
config.If[config.CompatibilityPolicy](
256+
config.Exists[string, config.CompatibilityPolicy](
257+
config.CompatibilityPolicies, conf.Plugin.CompatibilityPolicy),
258+
config.CompatibilityPolicies[conf.Plugin.CompatibilityPolicy],
259+
config.DefaultCompatibilityPolicy),
260+
config.If[config.VerificationPolicy](
261+
config.Exists[string, config.VerificationPolicy](
262+
config.VerificationPolicies, conf.Plugin.VerificationPolicy),
263+
config.VerificationPolicies[conf.Plugin.VerificationPolicy],
264+
config.DefaultVerificationPolicy),
265+
config.If[config.AcceptancePolicy](
266+
config.Exists[string, config.AcceptancePolicy](
267+
config.AcceptancePolicies, conf.Plugin.AcceptancePolicy),
268+
config.AcceptancePolicies[conf.Plugin.AcceptancePolicy],
269+
config.DefaultAcceptancePolicy),
270+
config.If[config.TerminationPolicy](
271+
config.Exists[string, config.TerminationPolicy](
272+
config.TerminationPolicies, conf.Plugin.TerminationPolicy),
273+
config.TerminationPolicies[conf.Plugin.TerminationPolicy],
274+
config.DefaultTerminationPolicy),
246275
logger,
247276
devMode,
248277
)
@@ -413,14 +442,20 @@ var runCmd = &cobra.Command{
413442
handler = mergedMetricsHandler(handler)
414443
}
415444

445+
readHeaderTimeout := config.If[time.Duration](
446+
metricsConfig.ReadHeaderTimeout > 0,
447+
metricsConfig.ReadHeaderTimeout,
448+
config.DefaultReadHeaderTimeout,
449+
)
450+
416451
// Check if the metrics server is already running before registering the handler.
417452
if _, err = http.Get(address); err != nil { //nolint:gosec
418453
// The timeout handler limits the nested handlers from running for too long.
419454
mux.Handle(
420455
metricsConfig.Path,
421456
http.TimeoutHandler(
422457
gziphandler.GzipHandler(handler),
423-
metricsConfig.GetTimeout(),
458+
readHeaderTimeout,
424459
"The request timed out while fetching the metrics",
425460
),
426461
)
@@ -430,19 +465,24 @@ var runCmd = &cobra.Command{
430465
}
431466

432467
// Create a new metrics server.
468+
timeout := config.If[time.Duration](
469+
metricsConfig.Timeout > 0,
470+
metricsConfig.Timeout,
471+
config.DefaultMetricsServerTimeout,
472+
)
433473
metricsServer = &http.Server{
434474
Addr: metricsConfig.Address,
435475
Handler: mux,
436-
ReadHeaderTimeout: metricsConfig.GetReadHeaderTimeout(),
437-
ReadTimeout: metricsConfig.GetTimeout(),
438-
WriteTimeout: metricsConfig.GetTimeout(),
439-
IdleTimeout: metricsConfig.GetTimeout(),
476+
ReadHeaderTimeout: readHeaderTimeout,
477+
ReadTimeout: timeout,
478+
WriteTimeout: timeout,
479+
IdleTimeout: timeout,
440480
}
441481

442482
logger.Info().Fields(map[string]interface{}{
443483
"address": address,
444-
"timeout": metricsConfig.GetTimeout().String(),
445-
"readHeaderTimeout": metricsConfig.GetReadHeaderTimeout().String(),
484+
"timeout": timeout.String(),
485+
"readHeaderTimeout": readHeaderTimeout.String(),
446486
}).Msg("Metrics are exposed")
447487

448488
if metricsConfig.CertFile != "" && metricsConfig.KeyFile != "" {
@@ -499,11 +539,22 @@ var runCmd = &cobra.Command{
499539
// Create and initialize pools of connections.
500540
for name, cfg := range conf.Global.Pools {
501541
logger := loggers[name]
502-
pools[name] = pool.NewPool(runCtx, cfg.GetSize())
542+
// Check if the pool size is greater than zero.
543+
currentPoolSize := config.If[int](
544+
cfg.Size > 0,
545+
// Check if the pool size is greater than the minimum pool size.
546+
config.If[int](
547+
cfg.Size > config.MinimumPoolSize,
548+
cfg.Size,
549+
config.MinimumPoolSize,
550+
),
551+
config.DefaultPoolSize,
552+
)
553+
pools[name] = pool.NewPool(runCtx, currentPoolSize)
503554

504555
span.AddEvent("Create pool", trace.WithAttributes(
505556
attribute.String("name", name),
506-
attribute.Int("size", cfg.GetSize()),
557+
attribute.Int("size", currentPoolSize),
507558
))
508559

509560
// Get client config from the config file.
@@ -517,21 +568,49 @@ var runCmd = &cobra.Command{
517568
}
518569

519570
// Fill the missing and zero values with the default ones.
520-
clients[name].TCPKeepAlivePeriod = clients[name].GetTCPKeepAlivePeriod()
521-
clients[name].ReceiveDeadline = clients[name].GetReceiveDeadline()
522-
clients[name].ReceiveTimeout = clients[name].GetReceiveTimeout()
523-
clients[name].SendDeadline = clients[name].GetSendDeadline()
524-
clients[name].ReceiveChunkSize = clients[name].GetReceiveChunkSize()
525-
clients[name].DialTimeout = clients[name].GetDialTimeout()
571+
clients[name].TCPKeepAlivePeriod = config.If[time.Duration](
572+
clients[name].TCPKeepAlivePeriod > 0,
573+
clients[name].TCPKeepAlivePeriod,
574+
config.DefaultTCPKeepAlivePeriod,
575+
)
576+
clients[name].ReceiveDeadline = config.If[time.Duration](
577+
clients[name].ReceiveDeadline > 0,
578+
clients[name].ReceiveDeadline,
579+
config.DefaultReceiveDeadline,
580+
)
581+
clients[name].ReceiveTimeout = config.If[time.Duration](
582+
clients[name].ReceiveTimeout > 0,
583+
clients[name].ReceiveTimeout,
584+
config.DefaultReceiveTimeout,
585+
)
586+
clients[name].SendDeadline = config.If[time.Duration](
587+
clients[name].SendDeadline > 0,
588+
clients[name].SendDeadline,
589+
config.DefaultSendDeadline,
590+
)
591+
clients[name].ReceiveChunkSize = config.If[int](
592+
clients[name].ReceiveChunkSize > 0,
593+
clients[name].ReceiveChunkSize,
594+
config.DefaultChunkSize,
595+
)
596+
clients[name].DialTimeout = config.If[time.Duration](
597+
clients[name].DialTimeout > 0,
598+
clients[name].DialTimeout,
599+
config.DefaultDialTimeout,
600+
)
526601

527602
// Add clients to the pool.
528-
for i := 0; i < cfg.GetSize(); i++ {
603+
for i := 0; i < currentPoolSize; i++ {
529604
clientConfig := clients[name]
530605
client := network.NewClient(
531606
runCtx, clientConfig, logger,
532607
network.NewRetry(
533608
clientConfig.Retries,
534-
clientConfig.GetBackoff(),
609+
config.If[time.Duration](
610+
clientConfig.Backoff > 0,
611+
clientConfig.Backoff,
612+
config.DefaultBackoff,
613+
),
535614
clientConfig.BackoffMultiplier,
536615
clientConfig.DisableBackoffCaps,
537616
loggers[name],
@@ -553,7 +632,7 @@ var runCmd = &cobra.Command{
553632
attribute.String("localAddress", client.LocalAddr()),
554633
attribute.String("remoteAddress", client.RemoteAddr()),
555634
attribute.Int("retries", clientConfig.Retries),
556-
attribute.String("backoff", clientConfig.GetBackoff().String()),
635+
attribute.String("backoff", client.Retry().Backoff.String()),
557636
attribute.Float64("backoffMultiplier", clientConfig.BackoffMultiplier),
558637
attribute.Bool("disableBackoffCaps", clientConfig.DisableBackoffCaps),
559638
)
@@ -583,7 +662,7 @@ var runCmd = &cobra.Command{
583662
"localAddress": client.LocalAddr(),
584663
"remoteAddress": client.RemoteAddr(),
585664
"retries": clientConfig.Retries,
586-
"backoff": clientConfig.GetBackoff().String(),
665+
"backoff": client.Retry().Backoff.String(),
587666
"backoffMultiplier": clientConfig.BackoffMultiplier,
588667
"disableBackoffCaps": clientConfig.DisableBackoffCaps,
589668
}
@@ -611,7 +690,7 @@ var runCmd = &cobra.Command{
611690
"count": strconv.Itoa(pools[name].Size()),
612691
}).Msg("There are clients available in the pool")
613692

614-
if pools[name].Size() != cfg.GetSize() {
693+
if pools[name].Size() != currentPoolSize {
615694
logger.Error().Msg(
616695
"The pool size is incorrect, either because " +
617696
"the clients cannot connect due to no network connectivity " +
@@ -626,7 +705,7 @@ var runCmd = &cobra.Command{
626705

627706
_, err = pluginRegistry.Run(
628707
pluginTimeoutCtx,
629-
map[string]interface{}{"name": name, "size": cfg.GetSize()},
708+
map[string]interface{}{"name": name, "size": currentPoolSize},
630709
v1.HookName_HOOK_NAME_ON_NEW_POOL)
631710
if err != nil {
632711
logger.Error().Err(err).Msg("Failed to run OnNewPool hooks")
@@ -642,7 +721,11 @@ var runCmd = &cobra.Command{
642721
logger := loggers[name]
643722
clientConfig := clients[name]
644723
// Fill the missing and zero value with the default one.
645-
cfg.HealthCheckPeriod = cfg.GetHealthCheckPeriod()
724+
cfg.HealthCheckPeriod = config.If[time.Duration](
725+
cfg.HealthCheckPeriod > 0,
726+
cfg.HealthCheckPeriod,
727+
config.DefaultHealthCheckPeriod,
728+
)
646729

647730
proxies[name] = network.NewProxy(
648731
runCtx,
@@ -689,7 +772,11 @@ var runCmd = &cobra.Command{
689772
runCtx,
690773
cfg.Network,
691774
cfg.Address,
692-
cfg.GetTickInterval(),
775+
config.If[time.Duration](
776+
cfg.TickInterval > 0,
777+
cfg.TickInterval,
778+
config.DefaultTickInterval,
779+
),
693780
network.Option{
694781
// Can be used to send keepalive messages to the client.
695782
EnableTicker: cfg.EnableTicker,

config/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,10 @@ const (
145145
DefaultHTTPAPIAddress = "localhost:18080"
146146
DefaultGRPCAPINetwork = "tcp"
147147
DefaultGRPCAPIAddress = "localhost:19090"
148+
149+
// Policies.
150+
DefaultCompatibilityPolicy = Strict
151+
DefaultVerificationPolicy = PassDown
152+
DefaultAcceptancePolicy = Accept
153+
DefaultTerminationPolicy = Stop
148154
)

0 commit comments

Comments
 (0)