Skip to content

Commit

Permalink
feat(router): optionally add jitter to config polling interval (#1506)
Browse files Browse the repository at this point in the history
Co-authored-by: Dustin Deus <[email protected]>
  • Loading branch information
endigma and StarpTech authored Jan 11, 2025
1 parent beab806 commit 1d67742
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 105 deletions.
2 changes: 1 addition & 1 deletion router/cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func NewRouter(ctx context.Context, params Params, additionalOptions ...core.Opt
c.Logger = logging.NewZapAccessLogger(f, cfg.DevelopmentMode, !cfg.JSONLog)
}
} else if cfg.AccessLogs.Output.Stdout.Enabled {

if cfg.AccessLogs.Buffer.Enabled {
bl, err := logging.NewJSONZapBufferedLogger(logging.BufferedLoggerOptions{
WS: os.Stdout,
Expand Down Expand Up @@ -255,6 +254,7 @@ func NewRouter(ctx context.Context, params Params, additionalOptions ...core.Opt
options = append(options, core.WithConfigPollerConfig(&core.RouterConfigPollerConfig{
GraphSignKey: cfg.Graph.SignKey,
PollInterval: cfg.PollInterval,
PollJitter: cfg.PollJitter,
ExecutionConfig: cfg.ExecutionConfig,
}))
}
Expand Down
3 changes: 2 additions & 1 deletion router/core/init_config_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"errors"
"fmt"

"github.com/wundergraph/cosmo/router/pkg/config"
"github.com/wundergraph/cosmo/router/pkg/controlplane/configpoller"
"github.com/wundergraph/cosmo/router/pkg/routerconfig"
Expand Down Expand Up @@ -139,7 +140,7 @@ func InitializeConfigPoller(r *Router, cdnProviders map[string]config.BaseStorag

configPoller := configpoller.New(r.graphApiToken,
configpoller.WithLogger(r.logger),
configpoller.WithPollInterval(r.routerConfigPollerConfig.PollInterval),
configpoller.WithPolling(r.routerConfigPollerConfig.PollInterval, r.routerConfigPollerConfig.PollJitter),
configpoller.WithClient(*primaryClient),
configpoller.WithFallbackClient(fallbackClient),
)
Expand Down
10 changes: 4 additions & 6 deletions router/core/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"crypto/x509"
"errors"
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"net"
"net/http"
"net/url"
"os"
"sync"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"

"github.com/wundergraph/graphql-go-tools/v2/pkg/netpoll"

"github.com/wundergraph/cosmo/router/internal/persistedoperation/apq"
Expand Down Expand Up @@ -129,6 +130,7 @@ type (
RouterConfigPollerConfig struct {
config.ExecutionConfig
PollInterval time.Duration
PollJitter time.Duration
GraphSignKey string
}

Expand Down Expand Up @@ -618,7 +620,6 @@ func (r *Router) listenAndServe(cfg *nodev1.RouterConfig) error {
}

func (r *Router) initModules(ctx context.Context) error {

moduleList := make([]ModuleInfo, 0, len(modules)+len(r.customModules))

for _, module := range modules {
Expand Down Expand Up @@ -974,7 +975,6 @@ func (r *Router) buildClients() error {
StorageConfig: &provider,
Prefix: r.automaticPersistedQueriesConfig.Storage.ObjectPrefix,
})

if err != nil {
return err
}
Expand Down Expand Up @@ -1184,7 +1184,6 @@ func (r *Router) Start(ctx context.Context) error {
// Shutdown gracefully shuts down the router. It blocks until the server is shutdown.
// If the router is already shutdown, the method returns immediately without error.
func (r *Router) Shutdown(ctx context.Context) (err error) {

if !r.shutdown.CompareAndSwap(false, true) {
return nil
}
Expand Down Expand Up @@ -1873,7 +1872,6 @@ func buildAttributesMap(attributes []config.CustomAttribute) map[string]string {

// buildHeaderAttributesMapper returns a function that maps custom attributes to the request headers.
func buildHeaderAttributesMapper(attributes []config.CustomAttribute) func(req *http.Request) []attribute.KeyValue {

if len(attributes) == 0 {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion router/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ type Config struct {
ShutdownDelay time.Duration `yaml:"shutdown_delay" envDefault:"60s" env:"SHUTDOWN_DELAY"`
GracePeriod time.Duration `yaml:"grace_period" envDefault:"30s" env:"GRACE_PERIOD"`
PollInterval time.Duration `yaml:"poll_interval" envDefault:"10s" env:"POLL_INTERVAL"`
PollJitter time.Duration `yaml:"poll_jitter" envDefault:"5s" env:"POLL_JITTER"`
HealthCheckPath string `yaml:"health_check_path" envDefault:"/health" env:"HEALTH_CHECK_PATH"`
ReadinessCheckPath string `yaml:"readiness_check_path" envDefault:"/health/ready" env:"READINESS_CHECK_PATH"`
LivenessCheckPath string `yaml:"liveness_check_path" envDefault:"/health/live" env:"LIVENESS_CHECK_PATH"`
Expand Down Expand Up @@ -879,7 +880,6 @@ func LoadConfig(configFilePath string, envOverride string) (*LoadResult, error)

isDefaultConfigPath := configFilePath == DefaultConfigPath
configFileBytes, err = os.ReadFile(configFilePath)

if err != nil {
if isDefaultConfigPath {
cfg.DefaultLoaded = false
Expand Down
Loading

0 comments on commit 1d67742

Please sign in to comment.