Skip to content

Commit

Permalink
chore: add configurable tick rate from envar or toml config file
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Feb 5, 2025
1 parent b9213ba commit 0c3c648
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cardinal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
BaseShardSequencerAddress: DefaultBaseShardSequencerAddress,
BaseShardRouterKey: "",
TelemetryTraceEnabled: false,
CardinalTickRate: 0,
}
)

Expand Down Expand Up @@ -77,6 +78,9 @@ type WorldConfig struct {

// TelemetryTraceEnabled When true, Cardinal will collect OpenTelemetry traces
TelemetryTraceEnabled bool `mapstructure:"TELEMETRY_TRACE_ENABLED"`

// CardinalTickRate The number of ticks per second
CardinalTickRate uint64 `mapstructure:"CARDINAL_TICK_RATE"`
}

func loadWorldConfig() (*WorldConfig, error) {
Expand Down
3 changes: 2 additions & 1 deletion cardinal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestWorldConfig_LoadFromEnv(t *testing.T) {
RedisPassword: "bar",
BaseShardSequencerAddress: "localhost:8080",
BaseShardRouterKey: "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ01",
CardinalTickRate: 10,
}

// Set env vars to target config values
Expand All @@ -47,7 +48,7 @@ func TestWorldConfig_LoadFromEnv(t *testing.T) {
t.Setenv("REDIS_PASSWORD", wantCfg.RedisPassword)
t.Setenv("BASE_SHARD_SEQUENCER_ADDRESS", wantCfg.BaseShardSequencerAddress)
t.Setenv("BASE_SHARD_ROUTER_KEY", wantCfg.BaseShardRouterKey)

t.Setenv("CARDINAL_TICK_RATE", strconv.FormatUint(wantCfg.CardinalTickRate, 10))
gotCfg, err := loadWorldConfig()
assert.NilError(t, err)

Expand Down
6 changes: 6 additions & 0 deletions cardinal/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ func NewWorld(opts ...WorldOption) (*World, error) {
}
}

// Set tick rate if provided
// it will be overridden by WithTickChannel option if provided
if cfg.CardinalTickRate > 0 {
world.tickChannel = time.Tick(time.Second / time.Duration(cfg.CardinalTickRate)) //nolint:staticcheck // its ok.
}

// Apply options
for _, opt := range cardinalOptions {
opt(world)
Expand Down

0 comments on commit 0c3c648

Please sign in to comment.