Skip to content

Commit 9a97418

Browse files
committed
ensure that changed config reloads seeding cap enforcement
1 parent accbce7 commit 9a97418

File tree

6 files changed

+79
-15
lines changed

6 files changed

+79
-15
lines changed

seeding/automod.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"seeding/internal/crcon"
1313
"strconv"
1414
"syscall"
15+
"time"
1516
)
1617

1718
var (
@@ -33,6 +34,10 @@ var (
3334
horizontalCaps = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
3435
)
3536

37+
type CRConClient interface {
38+
GetAutoModSeedingConfig(ctx context.Context) (*crcon.AutoModSeedingConfig, error)
39+
}
40+
3641
func main() {
3742
level := slog.LevelInfo
3843
if _, ok := os.LookupEnv("DEBUG"); ok {
@@ -45,14 +50,52 @@ func main() {
4550
rconBackendUrl = "http://backend:8000"
4651
}
4752

48-
ctx := context.Background()
53+
ctx, cancel := context.WithCancel(context.Background())
4954
a := internal.NewAuthentication(hllPassword, rconWebApiSecret)
50-
ac, err := crcon.NewClient(rconBackendUrl, a).GetAutoModSeedingConfig(ctx)
51-
if err != nil {
52-
logger.Error("find-seeding-automod-config", "error", err)
53-
return
55+
c := crcon.NewClient(rconBackendUrl, a)
56+
57+
go scheduleWorker(ctx, logger, c)
58+
59+
stop := make(chan os.Signal, 1)
60+
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
61+
<-stop
62+
63+
logger.Info("graceful-shutdown")
64+
cancel()
65+
}
66+
67+
func scheduleWorker(ctx context.Context, logger *slog.Logger, c CRConClient) {
68+
refreshConfig := time.NewTicker(time.Second * 5)
69+
70+
var (
71+
childCtx, cancelFn = context.WithCancel(ctx)
72+
previous *crcon.AutoModSeedingConfig
73+
)
74+
for {
75+
select {
76+
case <-ctx.Done():
77+
refreshConfig.Stop()
78+
cancelFn()
79+
return
80+
case <-refreshConfig.C:
81+
ac, err := c.GetAutoModSeedingConfig(childCtx)
82+
if err != nil {
83+
logger.Error("find-seeding-automod-config", "error", err)
84+
continue
85+
}
86+
if ac.Equals(previous) {
87+
continue
88+
}
89+
logger.Info("config-updated-restart")
90+
cancelFn()
91+
previous = ac
92+
childCtx, cancelFn = context.WithCancel(ctx)
93+
initialize(childCtx, logger, ac)
94+
}
5495
}
96+
}
5597

98+
func initialize(ctx context.Context, logger *slog.Logger, ac *crcon.AutoModSeedingConfig) {
5699
// Prefilling fences for first-spawners on the server. Whenever a player joins a server, they are randomly assigned
57100
// a spawn on either side (even before side selection). To not give them a warning that they are outside of the spawns
58101
// during seeding, the HQ lines for each team opponent are allowlisted as well.
@@ -241,10 +284,4 @@ func main() {
241284
go w.Run(ctx)
242285
logger.Info("started-worker")
243286
}
244-
245-
stop := make(chan os.Signal, 1)
246-
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
247-
<-stop
248-
249-
logger.Info("graceful-shutdown")
250287
}

seeding/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.1
44

55
require (
66
github.com/floriansw/go-hll-rcon v0.0.0-20250421114312-973708b529c4
7-
github.com/floriansw/hll-geofences v0.0.0-20250424221049-a62713ee5a0c
7+
github.com/floriansw/hll-geofences v0.0.0-20250425215853-4c30cf65a19f
88
)
99

1010
require gopkg.in/yaml.v3 v3.0.1 // indirect

seeding/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/floriansw/go-hll-rcon v0.0.0-20250421114312-973708b529c4 h1:GVl9QM8D0XVfACR9yoPr1fjSF3A/j58i5W8BIFOIW2M=
22
github.com/floriansw/go-hll-rcon v0.0.0-20250421114312-973708b529c4/go.mod h1:ICGqIYtkRM6GJv6oBwbskUIWq5Ar4C1SNxXBPdGYgDw=
3-
github.com/floriansw/hll-geofences v0.0.0-20250424221049-a62713ee5a0c h1:8F3PjoYJU9ZKO+OvioJxNip26oqu7EYldtM8orDn6JQ=
4-
github.com/floriansw/hll-geofences v0.0.0-20250424221049-a62713ee5a0c/go.mod h1:dZb6SSJb0bL4ijBW7TYYS0YUK42haOGgwXyWxcrI45o=
3+
github.com/floriansw/hll-geofences v0.0.0-20250425215853-4c30cf65a19f h1:MxlhdCkj7X0bA2nQ33OyJV9tDhL5fbm7Tzl46yeVuNs=
4+
github.com/floriansw/hll-geofences v0.0.0-20250425215853-4c30cf65a19f/go.mod h1:dZb6SSJb0bL4ijBW7TYYS0YUK42haOGgwXyWxcrI45o=
55
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
66
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
77
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=

seeding/internal/crcon/data.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,28 @@ type AutoModSeedingConfig struct {
66
EnforceCapFight EnforceCapFight
77
}
88

9+
func (c *AutoModSeedingConfig) Equals(o *AutoModSeedingConfig) bool {
10+
if o == nil {
11+
return false
12+
}
13+
if c.Enabled != o.Enabled || c.DryRun != o.DryRun || !c.EnforceCapFight.Equals(o.EnforceCapFight) {
14+
return false
15+
}
16+
return true
17+
}
18+
919
type EnforceCapFight struct {
1020
MinPlayers int
1121
MaxPlayers int
1222
MaxCaps int
1323
SkipWarning bool
1424
ViolationMessage string
1525
}
26+
27+
func (c *EnforceCapFight) Equals(o EnforceCapFight) bool {
28+
return c.MinPlayers == o.MinPlayers ||
29+
c.MaxPlayers == o.MaxPlayers ||
30+
c.MaxCaps == o.MaxCaps ||
31+
c.SkipWarning == o.SkipWarning ||
32+
c.ViolationMessage == o.ViolationMessage
33+
}

seeding/vendor/github.com/floriansw/hll-geofences/worker/worker.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

seeding/vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
github.com/floriansw/go-hll-rcon/rcon
44
github.com/floriansw/go-hll-rcon/rconv2
55
github.com/floriansw/go-hll-rcon/rconv2/api
6-
# github.com/floriansw/hll-geofences v0.0.0-20250424221049-a62713ee5a0c
6+
# github.com/floriansw/hll-geofences v0.0.0-20250425215853-4c30cf65a19f
77
## explicit; go 1.24.1
88
github.com/floriansw/hll-geofences/data
99
github.com/floriansw/hll-geofences/worker

0 commit comments

Comments
 (0)