-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgatewayd_app.go
1170 lines (1057 loc) · 37.9 KB
/
gatewayd_app.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cmd
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"runtime"
"strconv"
"sync/atomic"
"time"
"github.com/NYTimes/gziphandler"
sdkAct "github.com/gatewayd-io/gatewayd-plugin-sdk/act"
sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
"github.com/gatewayd-io/gatewayd/act"
"github.com/gatewayd-io/gatewayd/api"
"github.com/gatewayd-io/gatewayd/config"
gerr "github.com/gatewayd-io/gatewayd/errors"
"github.com/gatewayd-io/gatewayd/logging"
"github.com/gatewayd-io/gatewayd/metrics"
"github.com/gatewayd-io/gatewayd/network"
"github.com/gatewayd-io/gatewayd/plugin"
"github.com/gatewayd-io/gatewayd/pool"
"github.com/gatewayd-io/gatewayd/raft"
usage "github.com/gatewayd-io/gatewayd/usagereport/v1"
"github.com/getsentry/sentry-go"
"github.com/go-co-op/gocron"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var _ io.Writer = &cobraCmdWriter{}
type cobraCmdWriter struct {
*cobra.Command
}
func (c *cobraCmdWriter) Write(p []byte) (int, error) {
c.Print(string(p))
return len(p), nil
}
var UsageReportURL = "localhost:59091"
const (
DefaultMetricsServerProbeTimeout = 5 * time.Second
)
type GatewayDApp struct {
EnableTracing bool
EnableSentry bool
EnableLinting bool
EnableUsageReport bool
DevMode bool
CollectorURL string
PluginConfigFile string
GlobalConfigFile string
conf *config.Config
pluginRegistry *plugin.Registry
actRegistry *act.Registry
metricsServer *http.Server
metricsMerger *metrics.Merger
httpServer *api.HTTPServer
grpcServer *api.GRPCServer
loggers map[string]zerolog.Logger
pools map[string]map[string]*pool.Pool
clients map[string]map[string]*config.Client
proxies map[string]map[string]*network.Proxy
servers map[string]*network.Server
healthCheckScheduler *gocron.Scheduler
stopChan chan struct{}
ranStopGracefully *atomic.Bool
}
// NewGatewayDApp creates a new GatewayDApp instance.
func NewGatewayDApp(cmd *cobra.Command) *GatewayDApp {
app := GatewayDApp{
loggers: make(map[string]zerolog.Logger),
pools: make(map[string]map[string]*pool.Pool),
clients: make(map[string]map[string]*config.Client),
proxies: make(map[string]map[string]*network.Proxy),
servers: make(map[string]*network.Server),
healthCheckScheduler: gocron.NewScheduler(time.UTC),
stopChan: make(chan struct{}),
ranStopGracefully: &atomic.Bool{},
}
app.EnableTracing, _ = cmd.Flags().GetBool("tracing")
app.EnableSentry, _ = cmd.Flags().GetBool("sentry")
app.EnableUsageReport, _ = cmd.Flags().GetBool("usage-report")
app.EnableLinting, _ = cmd.Flags().GetBool("lint")
app.DevMode, _ = cmd.Flags().GetBool("dev")
app.CollectorURL, _ = cmd.Flags().GetString("collector-url")
app.GlobalConfigFile, _ = cmd.Flags().GetString("config")
app.PluginConfigFile, _ = cmd.Flags().GetString("plugin-config")
return &app
}
// loadConfig loads global and plugin configuration.
func (app *GatewayDApp) loadConfig(runCtx context.Context) error {
app.conf = config.NewConfig(runCtx,
config.Config{
GlobalConfigFile: app.GlobalConfigFile,
PluginConfigFile: app.PluginConfigFile,
},
)
if err := app.conf.InitConfig(runCtx); err != nil {
return err
}
return nil
}
// createLoggers creates loggers from the config.
func (app *GatewayDApp) createLoggers(
runCtx context.Context, cmd *cobra.Command,
) zerolog.Logger {
// Use cobra command cmd instead of os.Stdout for the console output.
cmdLogger := &cobraCmdWriter{cmd}
// Create a logger for each tenant.
for name, cfg := range app.conf.Global.Loggers {
app.loggers[name] = logging.NewLogger(runCtx, logging.LoggerConfig{
Output: cfg.GetOutput(),
ConsoleOut: cmdLogger,
Level: config.If(
config.Exists(config.LogLevels, cfg.Level),
config.LogLevels[cfg.Level],
config.LogLevels[config.DefaultLogLevel],
),
TimeFormat: config.If(
config.Exists(config.TimeFormats, cfg.TimeFormat),
config.TimeFormats[cfg.TimeFormat],
config.TimeFormats[config.DefaultTimeFormat],
),
ConsoleTimeFormat: config.If(
config.Exists(
config.ConsoleTimeFormats, cfg.ConsoleTimeFormat),
config.ConsoleTimeFormats[cfg.ConsoleTimeFormat],
config.ConsoleTimeFormats[config.DefaultConsoleTimeFormat],
),
NoColor: cfg.NoColor,
FileName: cfg.FileName,
MaxSize: cfg.MaxSize,
MaxBackups: cfg.MaxBackups,
MaxAge: cfg.MaxAge,
Compress: cfg.Compress,
LocalTime: cfg.LocalTime,
SyslogPriority: cfg.GetSyslogPriority(),
RSyslogNetwork: cfg.RSyslogNetwork,
RSyslogAddress: cfg.RSyslogAddress,
Name: name,
})
}
return app.loggers[config.Default]
}
// createActRegistry creates a new act registry given
// the built-in signals, policies, and actions.
func (app *GatewayDApp) createActRegistry(logger zerolog.Logger) error {
// Create a new act registry given the built-in signals, policies, and actions.
var publisher *act.Publisher
if app.conf.Plugin.ActionRedis.Enabled {
rdb := redis.NewClient(&redis.Options{
Addr: app.conf.Plugin.ActionRedis.Address,
})
var err error
publisher, err = act.NewPublisher(act.Publisher{
Logger: logger,
RedisDB: rdb,
ChannelName: app.conf.Plugin.ActionRedis.Channel,
})
if err != nil {
logger.Error().Err(err).Msg("Failed to create publisher for act registry")
return err //nolint:wrapcheck
}
logger.Info().Msg("Created Redis publisher for Act registry")
}
app.actRegistry = act.NewActRegistry(
act.Registry{
Signals: act.BuiltinSignals(),
Policies: act.BuiltinPolicies(),
Actions: act.BuiltinActions(),
DefaultPolicyName: app.conf.Plugin.DefaultPolicy,
PolicyTimeout: app.conf.Plugin.PolicyTimeout,
DefaultActionTimeout: app.conf.Plugin.ActionTimeout,
TaskPublisher: publisher,
Logger: logger,
},
)
return nil
}
// loadPolicies loads policies from the configuration file and
// adds them to the registry.
func (app *GatewayDApp) loadPolicies(logger zerolog.Logger) error {
// Load policies from the configuration file and add them to the registry.
for _, plc := range app.conf.Plugin.Policies {
policy, err := sdkAct.NewPolicy(
plc.Name, plc.Policy, plc.Metadata,
)
if err != nil || policy == nil {
logger.Error().Err(err).Str("name", plc.Name).Msg("Failed to create policy")
return err //nolint:wrapcheck
}
app.actRegistry.Add(policy)
}
return nil
}
// createPluginRegistry creates a new plugin registry.
func (app *GatewayDApp) createPluginRegistry(runCtx context.Context, logger zerolog.Logger) {
// Create a new plugin registry.
// The plugins are loaded and hooks registered before the configuration is loaded.
app.pluginRegistry = plugin.NewRegistry(
runCtx,
plugin.Registry{
ActRegistry: app.actRegistry,
Logger: logger,
DevMode: app.DevMode,
},
)
}
// startMetricsMerger starts the metrics merger if enabled.
func (app *GatewayDApp) startMetricsMerger(runCtx context.Context, logger zerolog.Logger) {
_, span := otel.Tracer(config.TracerName).Start(runCtx, "Start metrics merger")
defer span.End()
// Start the metrics merger if enabled.
if !app.conf.Plugin.EnableMetricsMerger {
logger.Info().Msg("Metrics merger is disabled")
span.AddEvent("Metrics merger is disabled")
return
}
// Create a new metrics merger.
app.metricsMerger = metrics.NewMerger(runCtx, metrics.Merger{
MetricsMergerPeriod: app.conf.Plugin.MetricsMergerPeriod,
Logger: logger,
})
// Add the plugins to the metrics merger.
app.pluginRegistry.ForEach(
func(_ sdkPlugin.Identifier, plugin *plugin.Plugin) {
metricsEnabled, err := strconv.ParseBool(plugin.Config["metricsEnabled"])
if err == nil && metricsEnabled {
app.metricsMerger.Add(plugin.ID.Name, plugin.Config["metricsUnixDomainSocket"])
logger.Debug().
Str("plugin", plugin.ID.Name).
Msg("Added plugin to metrics merger")
span.AddEvent("Added plugin to metrics merger")
}
},
)
// Start the metrics merger in the background if there are plugins to merge metrics from.
app.metricsMerger.Start() //nolint:contextcheck
}
// startHealthCheckScheduler starts the health check scheduler if enabled.
func (app *GatewayDApp) startHealthCheckScheduler(
runCtx, ctx context.Context, span trace.Span, logger zerolog.Logger,
) {
healthCheck := func() {
_, span := otel.Tracer(config.TracerName).Start(ctx, "Run plugin health check")
defer span.End()
plugins := []string{}
app.pluginRegistry.ForEach(
func(pluginId sdkPlugin.Identifier, plugin *plugin.Plugin) {
err := plugin.Ping()
if err == nil {
logger.Trace().Str("name", pluginId.Name).Msg("Successfully pinged plugin")
plugins = append(plugins, pluginId.Name)
return
}
span.RecordError(err)
logger.Error().Err(err).Msg("Failed to ping plugin")
// Remove the plugin from the metrics merger to prevent errors.
if app.conf.Plugin.EnableMetricsMerger && app.metricsMerger != nil {
app.metricsMerger.Remove(pluginId.Name)
}
// Remove the plugin from the registry.
app.pluginRegistry.Remove(pluginId)
if !app.conf.Plugin.ReloadOnCrash {
return // Do not reload the plugins.
}
// Reload the plugins and register their hooks upon crash.
logger.Info().Str("name", pluginId.Name).Msg("Reloading crashed plugin")
//
pluginConfig := app.conf.Plugin.GetPlugins(pluginId.Name)
if pluginConfig != nil {
// Load the plugins and register their hooks.
app.pluginRegistry.LoadPlugins(
runCtx, pluginConfig, app.conf.Plugin.StartTimeout)
}
},
)
span.SetAttributes(attribute.StringSlice("plugins", plugins))
}
// Ping the plugins to check if they are alive, and remove them if they are not.
startDelay := time.Now().Add(app.conf.Plugin.HealthCheckPeriod)
_, err := app.healthCheckScheduler.
Every(app.conf.Plugin.HealthCheckPeriod).
SingletonMode().
StartAt(startDelay).
Do(healthCheck)
if err != nil {
logger.Error().Err(err).Msg("Failed to start plugin health check scheduler")
span.RecordError(err)
}
// Start the health check scheduler only if there are plugins.
if app.pluginRegistry.Size() > 0 {
logger.Info().Str(
"healthCheckPeriod", app.conf.Plugin.HealthCheckPeriod.String(),
).Msg("Starting plugin health check scheduler")
app.healthCheckScheduler.StartAsync()
span.AddEvent("Started plugin health check scheduler")
}
}
// onConfigLoaded runs the OnConfigLoaded hook and
// merges the global config with the one from the plugins.
func (app *GatewayDApp) onConfigLoaded(
runCtx context.Context, span trace.Span, logger zerolog.Logger,
) error {
// Set the plugin timeout context.
pluginTimeoutCtx, cancel := context.WithTimeout(
context.Background(), app.conf.Plugin.Timeout)
defer cancel()
// The config will be passed to the plugins that register to the "OnConfigLoaded" plugin.
// The plugins can modify the config and return it.
updatedGlobalConfig, err := app.pluginRegistry.Run( //nolint:contextcheck
pluginTimeoutCtx, app.conf.GlobalKoanf.All(), v1.HookName_HOOK_NAME_ON_CONFIG_LOADED)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnConfigLoaded hooks")
span.RecordError(err)
}
if updatedGlobalConfig != nil {
updatedGlobalConfig = app.pluginRegistry.ActRegistry.RunAll(updatedGlobalConfig) //nolint:contextcheck
}
// If the config was modified by the plugins, merge it with the one loaded from the file.
// Only global configuration is merged, which means that plugins cannot modify the plugin
// configurations.
if updatedGlobalConfig != nil {
// Merge the config with the one loaded from the file (in memory).
// The changes won't be persisted to disk.
if err := app.conf.MergeGlobalConfig(runCtx, updatedGlobalConfig); err != nil {
logger.Error().Err(err).Msg("Failed to merge global config")
span.RecordError(err)
return err
}
}
return nil
}
// startMetricsServer starts the metrics server if enabled.
func (app *GatewayDApp) startMetricsServer(
runCtx context.Context, logger zerolog.Logger,
) error {
// Start the metrics server if enabled.
// TODO: Start multiple metrics servers. For now, only one default is supported.
// I should first find a use case for those multiple metrics servers.
_, span := otel.Tracer(config.TracerName).Start(runCtx, "Start metrics server")
defer span.End()
metricsConfig := app.conf.Global.Metrics[config.Default]
// TODO: refactor this to a separate function.
if !metricsConfig.Enabled {
logger.Info().Msg("Metrics server is disabled")
return nil
}
scheme := "http://"
if metricsConfig.KeyFile != "" && metricsConfig.CertFile != "" {
scheme = "https://"
}
fqdn, err := url.Parse(scheme + metricsConfig.Address)
if err != nil {
logger.Error().Err(err).Msg("Failed to parse metrics address")
span.RecordError(err)
return err //nolint:wrapcheck
}
address, err := url.JoinPath(fqdn.String(), metricsConfig.Path)
if err != nil {
logger.Error().Err(err).Msg("Failed to parse metrics path")
span.RecordError(err)
return err //nolint:wrapcheck
}
// Merge the metrics from the plugins with the ones from GatewayD.
mergedMetricsHandler := func(next http.Handler) http.Handler {
handler := func(responseWriter http.ResponseWriter, request *http.Request) {
if _, err := responseWriter.Write(app.metricsMerger.OutputMetrics); err != nil {
logger.Error().Err(err).Msg("Failed to write metrics")
span.RecordError(err)
sentry.CaptureException(err)
}
// The WriteHeader method intentionally does nothing, to prevent a bug
// in the merging metrics that causes the headers to be written twice,
// which results in an error: "http: superfluous response.WriteHeader call".
next.ServeHTTP(
&metrics.HeaderBypassResponseWriter{
ResponseWriter: responseWriter,
},
request)
}
return http.HandlerFunc(handler)
}
handler := func() http.Handler {
return promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer,
promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
DisableCompression: true,
}),
)
}()
mux := http.NewServeMux()
mux.HandleFunc("/", func(responseWriter http.ResponseWriter, _ *http.Request) {
// Serve a static page with a link to the metrics endpoint.
if _, err := responseWriter.Write([]byte(fmt.Sprintf(
`<html><head><title>GatewayD Prometheus Metrics Server</title></head><body><a href="%s">Metrics</a></body></html>`,
address,
))); err != nil {
logger.Error().Err(err).Msg("Failed to write metrics")
span.RecordError(err)
sentry.CaptureException(err)
}
})
if app.conf.Plugin.EnableMetricsMerger && app.metricsMerger != nil {
handler = mergedMetricsHandler(handler)
}
readHeaderTimeout := config.If(
metricsConfig.ReadHeaderTimeout > 0,
metricsConfig.ReadHeaderTimeout,
config.DefaultReadHeaderTimeout,
)
// Check if the metrics server is already running before registering the handler
ctx, cancel := context.WithTimeout(context.Background(), DefaultMetricsServerProbeTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, address, nil) //nolint:contextcheck
if err != nil {
logger.Error().Err(err).Msg("Failed to create request to check metrics server")
span.RecordError(err)
}
if resp, err := http.DefaultClient.Do(req); err != nil {
// The timeout handler limits the nested handlers from running for too long.
mux.Handle(
metricsConfig.Path,
http.TimeoutHandler(
gziphandler.GzipHandler(handler),
readHeaderTimeout,
"The request timed out while fetching the metrics",
),
)
} else {
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
logger.Warn().Msg("Metrics server is already running, consider changing the port")
span.RecordError(err)
}
// Create a new metrics server.
timeout := config.If(
metricsConfig.Timeout > 0,
metricsConfig.Timeout,
config.DefaultMetricsServerTimeout,
)
app.metricsServer = &http.Server{
Addr: metricsConfig.Address,
Handler: mux,
ReadHeaderTimeout: readHeaderTimeout,
ReadTimeout: timeout,
WriteTimeout: timeout,
IdleTimeout: timeout,
}
logger.Info().Fields(map[string]any{
"address": address,
"timeout": timeout.String(),
"readHeaderTimeout": readHeaderTimeout.String(),
}).Msg("Metrics are exposed")
if metricsConfig.CertFile != "" && metricsConfig.KeyFile != "" {
// Set up TLS.
app.metricsServer.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS13,
CurvePreferences: []tls.CurveID{
tls.CurveP521,
tls.CurveP384,
tls.CurveP256,
},
CipherSuites: []uint16{
tls.TLS_AES_128_GCM_SHA256,
tls.TLS_AES_256_GCM_SHA384,
tls.TLS_CHACHA20_POLY1305_SHA256,
},
}
app.metricsServer.TLSNextProto = make(
map[string]func(*http.Server, *tls.Conn, http.Handler))
logger.Debug().Msg("Metrics server is running with TLS")
// Start the metrics server with TLS.
if err = app.metricsServer.ListenAndServeTLS(
metricsConfig.CertFile, metricsConfig.KeyFile); !errors.Is(err, http.ErrServerClosed) {
logger.Error().Err(err).Msg("Failed to start metrics server")
span.RecordError(err)
}
} else {
// Start the metrics server without TLS.
if err = app.metricsServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
logger.Error().Err(err).Msg("Failed to start metrics server")
span.RecordError(err)
}
}
return nil
}
// onNewLogger runs the OnNewLogger hook.
func (app *GatewayDApp) onNewLogger(
span trace.Span, logger zerolog.Logger,
) {
// This is a notification hook, so we don't care about the result.
pluginTimeoutCtx, cancel := context.WithTimeout(context.Background(), app.conf.Plugin.Timeout)
defer cancel()
if data, ok := app.conf.GlobalKoanf.Get("loggers").(map[string]any); ok {
result, err := app.pluginRegistry.Run(
pluginTimeoutCtx, data, v1.HookName_HOOK_NAME_ON_NEW_LOGGER)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewLogger hooks")
span.RecordError(err)
}
if result != nil {
_ = app.pluginRegistry.ActRegistry.RunAll(result)
}
} else {
logger.Error().Msg("Failed to get loggers from config")
}
}
// createPoolAndClients creates pools of connections and clients.
func (app *GatewayDApp) createPoolAndClients(
runCtx context.Context, span trace.Span,
) error {
// Create and initialize pools of connections.
for configGroupName, configGroup := range app.conf.Global.Pools {
for configBlockName, cfg := range configGroup {
logger := app.loggers[configGroupName]
// Check if the pool size is greater than zero.
currentPoolSize := config.If(
cfg.Size > 0,
// Check if the pool size is greater than the minimum pool size.
config.If(
cfg.Size > config.MinimumPoolSize,
cfg.Size,
config.MinimumPoolSize,
),
config.DefaultPoolSize,
)
if _, ok := app.pools[configGroupName]; !ok {
app.pools[configGroupName] = make(map[string]*pool.Pool)
}
app.pools[configGroupName][configBlockName] = pool.NewPool(runCtx, currentPoolSize)
span.AddEvent("Create pool", trace.WithAttributes(
attribute.String("name", configBlockName),
attribute.Int("size", currentPoolSize),
))
if _, ok := app.clients[configGroupName]; !ok {
app.clients[configGroupName] = make(map[string]*config.Client)
}
// Get client config from the config file.
if clientConfig, ok := app.conf.Global.Clients[configGroupName][configBlockName]; !ok {
// This ensures that the default client config is used if the pool name is not
// found in the clients section.
app.clients[configGroupName][configBlockName] = app.conf.Global.Clients[config.Default][config.DefaultConfigurationBlock] //nolint:lll
} else {
// Merge the default client config with the one from the pool.
app.clients[configGroupName][configBlockName] = clientConfig
}
// Fill the missing and zero values with the default ones.
app.clients[configGroupName][configBlockName].TCPKeepAlivePeriod = config.If(
app.clients[configGroupName][configBlockName].TCPKeepAlivePeriod > 0,
app.clients[configGroupName][configBlockName].TCPKeepAlivePeriod,
config.DefaultTCPKeepAlivePeriod,
)
app.clients[configGroupName][configBlockName].ReceiveDeadline = config.If(
app.clients[configGroupName][configBlockName].ReceiveDeadline > 0,
app.clients[configGroupName][configBlockName].ReceiveDeadline,
config.DefaultReceiveDeadline,
)
app.clients[configGroupName][configBlockName].ReceiveTimeout = config.If(
app.clients[configGroupName][configBlockName].ReceiveTimeout > 0,
app.clients[configGroupName][configBlockName].ReceiveTimeout,
config.DefaultReceiveTimeout,
)
app.clients[configGroupName][configBlockName].SendDeadline = config.If(
app.clients[configGroupName][configBlockName].SendDeadline > 0,
app.clients[configGroupName][configBlockName].SendDeadline,
config.DefaultSendDeadline,
)
app.clients[configGroupName][configBlockName].ReceiveChunkSize = config.If(
app.clients[configGroupName][configBlockName].ReceiveChunkSize > 0,
app.clients[configGroupName][configBlockName].ReceiveChunkSize,
config.DefaultChunkSize,
)
app.clients[configGroupName][configBlockName].DialTimeout = config.If(
app.clients[configGroupName][configBlockName].DialTimeout > 0,
app.clients[configGroupName][configBlockName].DialTimeout,
config.DefaultDialTimeout,
)
// Add clients to the pool.
for range currentPoolSize {
clientConfig := app.clients[configGroupName][configBlockName]
clientConfig.GroupName = configGroupName
clientConfig.BlockName = configBlockName
client := network.NewClient(
runCtx, clientConfig, logger,
network.NewRetry(
network.Retry{
Retries: clientConfig.Retries,
Backoff: config.If(
clientConfig.Backoff > 0,
clientConfig.Backoff,
config.DefaultBackoff,
),
BackoffMultiplier: clientConfig.BackoffMultiplier,
DisableBackoffCaps: clientConfig.DisableBackoffCaps,
Logger: app.loggers[configBlockName],
},
),
)
if client == nil {
return errors.New("failed to create client, please check the configuration")
}
eventOptions := trace.WithAttributes(
attribute.String("name", configBlockName),
attribute.String("group", configGroupName),
attribute.String("network", client.Network),
attribute.String("address", client.Address),
attribute.Int("receiveChunkSize", client.ReceiveChunkSize),
attribute.String("receiveDeadline", client.ReceiveDeadline.String()),
attribute.String("receiveTimeout", client.ReceiveTimeout.String()),
attribute.String("sendDeadline", client.SendDeadline.String()),
attribute.String("dialTimeout", client.DialTimeout.String()),
attribute.Bool("tcpKeepAlive", client.TCPKeepAlive),
attribute.String("tcpKeepAlivePeriod", client.TCPKeepAlivePeriod.String()),
attribute.String("localAddress", client.LocalAddr()),
attribute.String("remoteAddress", client.RemoteAddr()),
attribute.Int("retries", clientConfig.Retries),
attribute.String("backoff", client.Retry().Backoff.String()),
attribute.Float64("backoffMultiplier", clientConfig.BackoffMultiplier),
attribute.Bool("disableBackoffCaps", clientConfig.DisableBackoffCaps),
)
if client.ID != "" {
eventOptions = trace.WithAttributes(
attribute.String("id", client.ID),
)
}
span.AddEvent("Create client", eventOptions)
pluginTimeoutCtx, cancel := context.WithTimeout(
context.Background(), app.conf.Plugin.Timeout)
defer cancel()
clientCfg := map[string]any{
"id": client.ID,
"name": configBlockName,
"group": configGroupName,
"network": client.Network,
"address": client.Address,
"receiveChunkSize": client.ReceiveChunkSize,
"receiveDeadline": client.ReceiveDeadline.String(),
"receiveTimeout": client.ReceiveTimeout.String(),
"sendDeadline": client.SendDeadline.String(),
"dialTimeout": client.DialTimeout.String(),
"tcpKeepAlive": client.TCPKeepAlive,
"tcpKeepAlivePeriod": client.TCPKeepAlivePeriod.String(),
"localAddress": client.LocalAddr(),
"remoteAddress": client.RemoteAddr(),
"retries": clientConfig.Retries,
"backoff": client.Retry().Backoff.String(),
"backoffMultiplier": clientConfig.BackoffMultiplier,
"disableBackoffCaps": clientConfig.DisableBackoffCaps,
}
result, err := app.pluginRegistry.Run( //nolint:contextcheck
pluginTimeoutCtx, clientCfg, v1.HookName_HOOK_NAME_ON_NEW_CLIENT)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewClient hooks")
span.RecordError(err)
}
if result != nil {
_ = app.pluginRegistry.ActRegistry.RunAll(result) //nolint:contextcheck
}
err = app.pools[configGroupName][configBlockName].Put(client.ID, client)
if err != nil {
logger.Error().Err(err).Msg("Failed to add client to the pool")
span.RecordError(err)
}
}
// Verify that the pool is properly populated.
logger.Info().Fields(map[string]any{
"name": configBlockName,
"count": strconv.Itoa(app.pools[configGroupName][configBlockName].Size()),
}).Msg("There are clients available in the pool")
if app.pools[configGroupName][configBlockName].Size() != currentPoolSize {
logger.Error().Msg(
"The pool size is incorrect, either because " +
"the clients cannot connect due to no network connectivity " +
"or the server is not running. exiting...")
app.pluginRegistry.Shutdown()
return errors.New("failed to initialize pool, please check the configuration")
}
// Run the OnNewPool hook.
pluginTimeoutCtx, cancel := context.WithTimeout(
context.Background(), app.conf.Plugin.Timeout)
defer cancel()
result, err := app.pluginRegistry.Run( //nolint:contextcheck
pluginTimeoutCtx,
map[string]any{"name": configBlockName, "size": currentPoolSize},
v1.HookName_HOOK_NAME_ON_NEW_POOL)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewPool hooks")
span.RecordError(err)
}
if result != nil {
_ = app.pluginRegistry.ActRegistry.RunAll(result) //nolint:contextcheck
}
}
}
return nil
}
// createProxies creates proxies.
func (app *GatewayDApp) createProxies(runCtx context.Context, span trace.Span) {
// Create and initialize prefork proxies with each pool of clients.
for configGroupName, configGroup := range app.conf.Global.Proxies {
for configBlockName, cfg := range configGroup {
logger := app.loggers[configGroupName]
clientConfig := app.clients[configGroupName][configBlockName]
// Fill the missing and zero value with the default one.
cfg.HealthCheckPeriod = config.If(
cfg.HealthCheckPeriod > 0,
cfg.HealthCheckPeriod,
config.DefaultHealthCheckPeriod,
)
if _, ok := app.proxies[configGroupName]; !ok {
app.proxies[configGroupName] = make(map[string]*network.Proxy)
}
app.proxies[configGroupName][configBlockName] = network.NewProxy(
runCtx,
network.Proxy{
GroupName: configGroupName,
BlockName: configBlockName,
AvailableConnections: app.pools[configGroupName][configBlockName],
PluginRegistry: app.pluginRegistry,
HealthCheckPeriod: cfg.HealthCheckPeriod,
ClientConfig: clientConfig,
Logger: logger,
PluginTimeout: app.conf.Plugin.Timeout,
},
)
span.AddEvent("Create proxy", trace.WithAttributes(
attribute.String("name", configBlockName),
attribute.String("healthCheckPeriod", cfg.HealthCheckPeriod.String()),
))
pluginTimeoutCtx, cancel := context.WithTimeout(
context.Background(), app.conf.Plugin.Timeout)
defer cancel()
if data, ok := app.conf.GlobalKoanf.Get("proxies").(map[string]any); ok {
result, err := app.pluginRegistry.Run( //nolint:contextcheck
pluginTimeoutCtx, data, v1.HookName_HOOK_NAME_ON_NEW_PROXY)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewProxy hooks")
span.RecordError(err)
}
if result != nil {
_ = app.pluginRegistry.ActRegistry.RunAll(result) //nolint:contextcheck
}
} else {
logger.Error().Msg("Failed to get proxy from config")
}
}
}
}
// createServers creates servers.
func (app *GatewayDApp) createServers(
runCtx context.Context, span trace.Span, raftNode *raft.Node,
) {
// Create and initialize servers.
for name, cfg := range app.conf.Global.Servers {
logger := app.loggers[name]
var serverProxies []network.IProxy
for _, proxy := range app.proxies[name] {
serverProxies = append(serverProxies, proxy)
}
app.servers[name] = network.NewServer(
runCtx,
network.Server{
GroupName: name,
Network: cfg.Network,
Address: cfg.Address,
TickInterval: config.If(
cfg.TickInterval > 0,
cfg.TickInterval,
config.DefaultTickInterval,
),
Options: network.Option{
// Can be used to send keepalive messages to the client.
EnableTicker: cfg.EnableTicker,
},
Proxies: serverProxies,
Logger: logger,
PluginRegistry: app.pluginRegistry,
PluginTimeout: app.conf.Plugin.Timeout,
EnableTLS: cfg.EnableTLS,
CertFile: cfg.CertFile,
KeyFile: cfg.KeyFile,
HandshakeTimeout: cfg.HandshakeTimeout,
LoadbalancerStrategyName: cfg.LoadBalancer.Strategy,
LoadbalancerRules: cfg.LoadBalancer.LoadBalancingRules,
LoadbalancerConsistentHash: cfg.LoadBalancer.ConsistentHash,
RaftNode: raftNode,
},
)
span.AddEvent("Create server", trace.WithAttributes(
attribute.String("name", name),
attribute.String("network", cfg.Network),
attribute.String("address", cfg.Address),
attribute.String("tickInterval", cfg.TickInterval.String()),
attribute.String("pluginTimeout", app.conf.Plugin.Timeout.String()),
attribute.Bool("enableTLS", cfg.EnableTLS),
attribute.String("certFile", cfg.CertFile),
attribute.String("keyFile", cfg.KeyFile),
attribute.String("handshakeTimeout", cfg.HandshakeTimeout.String()),
))
pluginTimeoutCtx, cancel := context.WithTimeout(
context.Background(), app.conf.Plugin.Timeout)
defer cancel()
if data, ok := app.conf.GlobalKoanf.Get("servers").(map[string]any); ok {
result, err := app.pluginRegistry.Run( //nolint:contextcheck
pluginTimeoutCtx, data, v1.HookName_HOOK_NAME_ON_NEW_SERVER)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewServer hooks")
span.RecordError(err)
}
if result != nil {
_ = app.pluginRegistry.ActRegistry.RunAll(result) //nolint:contextcheck
}
} else {
logger.Error().Msg("Failed to get the servers configuration")
}
}
}
// startAPIServers starts the API servers.
func (app *GatewayDApp) startAPIServers(
runCtx context.Context, logger zerolog.Logger, raftNode *raft.Node,
) {
// Start the HTTP and gRPC APIs.
if !app.conf.Global.API.Enabled {
logger.Info().Msg("API is not enabled, skipping")
return
}
apiOptions := api.Options{
Logger: logger,
GRPCNetwork: app.conf.Global.API.GRPCNetwork,
GRPCAddress: app.conf.Global.API.GRPCAddress,
HTTPAddress: app.conf.Global.API.HTTPAddress,
Servers: app.servers,
RaftNode: raftNode,
}
apiObj := &api.API{
Options: &apiOptions,
Config: app.conf,
PluginRegistry: app.pluginRegistry,
Pools: app.pools,
Proxies: app.proxies,
Servers: app.servers,
}
app.grpcServer = api.NewGRPCServer(
runCtx,
api.GRPCServer{
API: apiObj,
HealthChecker: &api.HealthChecker{Servers: app.servers},
},
)
if app.grpcServer != nil {
go app.grpcServer.Start()
logger.Info().Str("address", apiOptions.HTTPAddress).Msg("Started the HTTP API")
app.httpServer = api.NewHTTPServer(&apiOptions) //nolint:contextcheck
go app.httpServer.Start()
logger.Info().Fields(
map[string]any{
"network": apiOptions.GRPCNetwork,
"address": apiOptions.GRPCAddress,
},
).Msg("Started the gRPC Server")
}
}
// reportUsage reports usage statistics.
func (app *GatewayDApp) reportUsage(logger zerolog.Logger) {
if !app.EnableUsageReport {
logger.Info().Msg("Usage reporting is not enabled, skipping")
return
}
// Report usage statistics.
go func() {
conn, err := grpc.NewClient(
UsageReportURL,
grpc.WithTransportCredentials(
credentials.NewTLS(
&tls.Config{
MinVersion: tls.VersionTLS12,
},
),
),
)
if err != nil {
logger.Trace().Err(err).Msg(
"Failed to dial to the gRPC server for usage reporting")
}
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
logger.Trace().Err(err).Msg("Failed to close the connection to the usage report service")