Revamp DHCP, add basic context management
This started off as a small change to make the network service DHCP client a bit nicer, and ended up basically me half-assedly starting to add context within Smalltown.
In my opionion a simple OnStart/OnStop lifecycle management for services will stop working once we have to start handling failing services. I think taking inspiration from Erlang's OTP and implementing some sort of supervision tree is the way to go. I think this also ties nicely together with Go's context system, at least partially. Implementing the full supervision tree system is out of scope for this change, but at least this introduces .Context() on the base service struct that service implementations can use. Currently each service has its own background context, but again, this should tie into some sort of supervision tree in the future. There will be a design document for this.
I also rejigger the init code to have a context available immediately, and use that to acquire (with timeout) information about DHCP addresses from the network service.
I also fix a bug where the network service is started twice (once by init, once by the smalltown node code; now the smalltown node code takes in a dependency injected network service instead).
I also fix a bug where OnStop would call OnStart. Whoops.
Test Plan: no new functionality, covered by current tests
Bug: T561
X-Origin-Diff: phab/D396
GitOrigin-RevId: adddf3dd2f140b6ea64eb034ff19533d32c4ef23
diff --git a/core/internal/common/service/service.go b/core/internal/common/service/service.go
index e093ff6..1cbedbe 100644
--- a/core/internal/common/service/service.go
+++ b/core/internal/common/service/service.go
@@ -17,9 +17,11 @@
package service
import (
+ "context"
"errors"
- "go.uber.org/zap"
"sync"
+
+ "go.uber.org/zap"
)
var (
@@ -43,6 +45,16 @@
mutex sync.Mutex
running bool
+
+ // A context that represents the lifecycle of a service.
+ // It is created right before impl.OnStart, and canceled
+ // right after impl.OnStop is.
+ // This is a transition mechanism from moving from OnStart/OnStop
+ // based lifecycle management of services to a context-based supervision
+ // tree.
+ // Service implementations should access this via .Context()
+ ctx *context.Context
+ ctxC *context.CancelFunc
}
)
@@ -51,6 +63,8 @@
Logger: logger,
name: name,
impl: impl,
+ ctx: nil,
+ ctxC: nil,
}
}
@@ -63,6 +77,10 @@
return ErrAlreadyRunning
}
+ ctx, ctxC := context.WithCancel(context.Background())
+ b.ctx = &ctx
+ b.ctxC = &ctxC
+
err := b.impl.OnStart()
if err != nil {
b.Logger.Error("Failed to start service", zap.String("service", b.name), zap.Error(err))
@@ -83,7 +101,7 @@
return ErrNotRunning
}
- err := b.impl.OnStart()
+ err := b.impl.OnStop()
if err != nil {
b.Logger.Error("Failed to stop service", zap.String("service", b.name), zap.Error(err))
@@ -91,6 +109,12 @@
}
b.running = false
+
+ // Kill context
+ (*b.ctxC)()
+ b.ctx = nil
+ b.ctxC = nil
+
b.Logger.Info("Stopped service", zap.String("service", b.name))
return nil
}
@@ -102,3 +126,12 @@
return b.running
}
+
+// Context returns a context that can be used within OnStart() to create new
+// lightweight subservices that use a context for lifecycle management.
+// This is a transition measure before the Service library is rewritten to use
+// a more advanced context-and-returned-error supervision tree.
+// This context can also be used for blocking operations like IO, etc.
+func (b *BaseService) Context() context.Context {
+ return *b.ctx
+}