Skip to content

Commit

Permalink
fix: pick correct broadcast adapter on Fly
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Nov 2, 2023
1 parent 46fcd1b commit 2dfc186
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion config/presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (c *Config) loadFlyPreset(defaults *Config) error {
return errors.New("FLY_APP_NAME env is missing")
}

redisEnabled := (c.Redis.URL != defaults.Redis.URL)

// Obtain cluster info
cluster, err := fly.Cluster(appName)

Expand Down Expand Up @@ -107,12 +109,16 @@ func (c *Config) loadFlyPreset(defaults *Config) error {
// Enable embedded NATS by default unless another adapter is set for PubSub
// or Redis URL is provided
if c.PubSubAdapter == defaults.PubSubAdapter {
if c.Redis.URL != defaults.Redis.URL {
if redisEnabled {
c.PubSubAdapter = "redis"
}
}

if multiNode {
if !redisEnabled && c.BroadcastAdapter == defaults.BroadcastAdapter {
c.BroadcastAdapter = "http,nats"
}

if c.PubSubAdapter == defaults.PubSubAdapter {
c.PubSubAdapter = "nats"
}
Expand All @@ -134,6 +140,10 @@ func (c *Config) loadFlyPreset(defaults *Config) error {
if singleNode {
log.WithField("context", "config").Infof("Discovered a single node cluster -> enabling in-memory broker")
c.BrokerAdapter = "memory"

if !redisEnabled && c.BroadcastAdapter == defaults.BroadcastAdapter {
c.BroadcastAdapter = "http"
}
}

if rpcName, ok := os.LookupEnv("ANYCABLE_FLY_RPC_APP_NAME"); ok {
Expand Down
4 changes: 4 additions & 0 deletions config/presets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestFlyPresets_when_single_vm_discovered(t *testing.T) {
// In-memory broker is good enough for single node, no pub/sub needed
assert.Equal(t, false, config.EmbedNats)
assert.Equal(t, "", config.PubSubAdapter)
assert.Equal(t, "http", config.BroadcastAdapter)
assert.Equal(t, "memory", config.BrokerAdapter)
assert.Equal(t, "nats://0.0.0.0:4222", config.EmbeddedNats.ServiceAddr)
}
Expand Down Expand Up @@ -105,6 +106,7 @@ func TestFlyPresets_when_two_vms_discovered(t *testing.T) {
assert.Equal(t, "0.0.0.0", config.Host)
assert.Equal(t, 8989, config.Port)
assert.Equal(t, 8989, config.HTTPBroadcast.Port)
assert.Equal(t, "http,nats", config.BroadcastAdapter)
assert.Equal(t, true, config.EmbedNats)
assert.Equal(t, "nats", config.PubSubAdapter)
// We do not enable broker by default, since it requires at least 3 nodes or exactly 1
Expand Down Expand Up @@ -139,6 +141,7 @@ func TestFlyPresets_when_three_vms_discovered(t *testing.T) {
assert.Equal(t, "0.0.0.0", config.Host)
assert.Equal(t, 8989, config.Port)
assert.Equal(t, 8989, config.HTTPBroadcast.Port)
assert.Equal(t, "http,nats", config.BroadcastAdapter)
assert.Equal(t, true, config.EmbedNats)
assert.Equal(t, "nats", config.PubSubAdapter)
assert.Equal(t, "nats", config.BrokerAdapter)
Expand Down Expand Up @@ -173,6 +176,7 @@ func TestFlyPresets_when_three_vms_from_different_regions(t *testing.T) {
assert.Equal(t, 8989, config.Port)
assert.Equal(t, 8989, config.HTTPBroadcast.Port)
assert.Equal(t, true, config.EmbedNats)
assert.Equal(t, "http,nats", config.BroadcastAdapter)
assert.Equal(t, "nats", config.PubSubAdapter)
// Currently, we do not enable broker for multi-region setup; we need to figure this out later
assert.Equal(t, "", config.BrokerAdapter)
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ The preset provide the following defaults:

- `host`: "0.0.0.0"
- `http_broadcast_port`: `$PORT` (set to the same value as the main HTTP port).
- `broadcast_adapter`: "http" (unless Redis is configured)
- `enats_server_addr`: "nats://0.0.0.0:4222"
- `enats_cluster_addr`: "nats://0.0.0.0:5222"
- `enats_cluster_name`: "\<FLY_APP_NAME\>-\<FLY_REGION\>-cluster"
Expand Down

0 comments on commit 2dfc186

Please sign in to comment.