Fix net Listener interface and implement max process backoff time

This commit is contained in:
tutman96 2025-01-05 19:21:37 +00:00
parent 5a05719106
commit 79305da221
2 changed files with 8 additions and 4 deletions

View File

@ -22,7 +22,7 @@ type PluginInstall struct {
manifest *PluginManifest
runningVersion *string
processManager *ProcessManager
rpcListener *net.Listener
rpcListener net.Listener
}
func (p *PluginInstall) GetManifest() (*PluginManifest, error) {
@ -115,7 +115,7 @@ func (p *PluginInstall) ReconcileSubprocess() error {
if err != nil {
return fmt.Errorf("failed to listen on socket: %v", err)
}
p.rpcListener = &listener
p.rpcListener = listener
p.processManager = NewProcessManager(func() *exec.Cmd {
cmd := exec.Command(manifest.BinaryPath)
@ -148,7 +148,7 @@ func (p *PluginInstall) Shutdown() {
}
if p.rpcListener != nil {
(*p.rpcListener).Close()
p.rpcListener.Close()
p.rpcListener = nil
}
}

View File

@ -10,6 +10,7 @@ import (
// TODO: this can probably be defaulted to this, but overwritten on a per-plugin basis
const GRACEFUL_SHUTDOWN_DELAY = 30 * time.Second
const MAX_RESTART_BACKOFF = 30 * time.Second
type ProcessManager struct {
cmdGen func() *exec.Cmd
@ -25,7 +26,7 @@ func NewProcessManager(commandGenerator func() *exec.Cmd) *ProcessManager {
return &ProcessManager{
cmdGen: commandGenerator,
enabled: true,
backoff: time.Second,
backoff: 250 * time.Millisecond,
shutdown: make(chan struct{}),
restartCh: make(chan struct{}, 1),
}
@ -74,6 +75,9 @@ func (pm *ProcessManager) scheduleRestart() {
log.Printf("Restarting process in %v...", pm.backoff)
time.Sleep(pm.backoff)
pm.backoff *= 2 // Exponential backoff
if pm.backoff > MAX_RESTART_BACKOFF {
pm.backoff = MAX_RESTART_BACKOFF
}
pm.restartCh <- struct{}{}
}
}