fix: propagate allocated transport ports

This commit is contained in:
Josh Creek
2026-08-31 20:42:19 +01:00
parent a0195987bd
commit 9810ee543f
3 changed files with 69 additions and 5 deletions
+25 -2
View File
@@ -30,6 +30,7 @@ type Config struct {
Environment []string
SDKBaseURL string
ReadyURL string
Transport string
ReadyTimeout time.Duration
PollInterval time.Duration
HTTPClient *http.Client
@@ -51,6 +52,12 @@ func New(config Config) (*Supervisor, error) {
if config.PollInterval <= 0 {
config.PollInterval = 100 * time.Millisecond
}
if config.Transport == "" {
config.Transport = "enet"
}
if config.Transport != "enet" && config.Transport != "steam_sdr" {
return nil, fmt.Errorf("unsupported transport %q", config.Transport)
}
if config.HTTPClient == nil {
config.HTTPClient = http.DefaultClient
}
@@ -68,9 +75,14 @@ func (s *Supervisor) Start(ctx context.Context) error {
if err != nil {
return err
}
env = append(env, "SDR_LISTEN_PORT="+strconv.Itoa(port), "SDR_IP="+address+":"+strconv.Itoa(port))
if s.config.Transport == "steam_sdr" {
env = append(env, "SDR_LISTEN_PORT="+strconv.Itoa(port), "SDR_IP="+address+":"+strconv.Itoa(port))
}
command := withPort(s.config.Command, port)
s.cmd = exec.CommandContext(ctx, command[0], command[1:]...)
} else {
s.cmd = exec.CommandContext(ctx, s.config.Command[0], s.config.Command[1:]...)
}
s.cmd = exec.CommandContext(ctx, s.config.Command[0], s.config.Command[1:]...)
s.cmd.Env = env
if err := s.cmd.Start(); err != nil {
return err
@@ -85,6 +97,17 @@ func (s *Supervisor) Start(ctx context.Context) error {
return s.sdkPost(ctx, "/ready")
}
func withPort(command []string, port int) []string {
result := append([]string(nil), command...)
for i, arg := range result {
if strings.HasPrefix(arg, "--port=") {
result[i] = "--port=" + strconv.Itoa(port)
return result
}
}
return append(result, "--port="+strconv.Itoa(port))
}
func (s *Supervisor) Wait() error {
if s.cmd == nil {
return fmt.Errorf("supervisor has not started")