mirror of
https://github.com/openziti/ziti.git
synced 2026-10-12 07:18:36 +00:00
Allow router to be shutdown for tests
This commit is contained in:
+23
-14
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/netfoundry/ziti-fabric/controller/xmgmt"
|
||||
"github.com/netfoundry/ziti-foundation/channel2"
|
||||
"github.com/netfoundry/ziti-foundation/profiler"
|
||||
"github.com/netfoundry/ziti-foundation/util/concurrenz"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
@@ -40,11 +41,15 @@ type Controller struct {
|
||||
|
||||
ctrlListener channel2.UnderlayListener
|
||||
mgmtListener channel2.UnderlayListener
|
||||
|
||||
shutdownC chan struct{}
|
||||
isShutdown concurrenz.AtomicBoolean
|
||||
}
|
||||
|
||||
func NewController(cfg *Config) (*Controller, error) {
|
||||
c := &Controller{
|
||||
config: cfg,
|
||||
config: cfg,
|
||||
shutdownC: make(chan struct{}),
|
||||
}
|
||||
|
||||
if n, err := network.NewNetwork(cfg.Id, cfg.Network, cfg.Db, cfg.Metrics); err == nil {
|
||||
@@ -97,23 +102,27 @@ func (c *Controller) Run() error {
|
||||
}
|
||||
|
||||
func (c *Controller) Shutdown() {
|
||||
if c.ctrlListener != nil {
|
||||
if err := c.ctrlListener.Close(); err != nil {
|
||||
pfxlog.Logger().WithError(err).Error("failed to close ctrl channel listener")
|
||||
if c.isShutdown.CompareAndSwap(false, true) {
|
||||
close(c.shutdownC)
|
||||
|
||||
if c.ctrlListener != nil {
|
||||
if err := c.ctrlListener.Close(); err != nil {
|
||||
pfxlog.Logger().WithError(err).Error("failed to close ctrl channel listener")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if c.mgmtListener != nil {
|
||||
if err := c.mgmtListener.Close(); err != nil {
|
||||
pfxlog.Logger().WithError(err).Error("failed to close mgmt channel listener")
|
||||
if c.mgmtListener != nil {
|
||||
if err := c.mgmtListener.Close(); err != nil {
|
||||
pfxlog.Logger().WithError(err).Error("failed to close mgmt channel listener")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.network.Shutdown()
|
||||
c.network.Shutdown()
|
||||
|
||||
if c.config.Db != nil {
|
||||
if err := c.config.Db.Close(); err != nil {
|
||||
pfxlog.Logger().WithError(err).Error("failed to close db")
|
||||
if c.config.Db != nil {
|
||||
if err := c.config.Db.Close(); err != nil {
|
||||
pfxlog.Logger().WithError(err).Error("failed to close db")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,7 +143,7 @@ func (c *Controller) showOptions() error {
|
||||
|
||||
func (c *Controller) startProfiling() {
|
||||
if c.config.Profile.Memory.Path != "" {
|
||||
go profiler.NewMemory(c.config.Profile.Memory.Path, c.config.Profile.Memory.Interval).Run()
|
||||
go profiler.NewMemoryWithShutdown(c.config.Profile.Memory.Path, c.config.Profile.Memory.Interval, c.shutdownC).Run()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -591,3 +591,20 @@ func (network *Network) GetServiceCache() Cache {
|
||||
type Cache interface {
|
||||
RemoveFromCache(id string)
|
||||
}
|
||||
|
||||
type MultipleErrors []error
|
||||
|
||||
func (e MultipleErrors) Error() string {
|
||||
if len(e) == 0 {
|
||||
return "no errors occurred"
|
||||
}
|
||||
if len(e) == 1 {
|
||||
return e[0].Error()
|
||||
}
|
||||
buf := strings.Builder{}
|
||||
buf.WriteString("multiple errors occurred")
|
||||
for idx, err := range e {
|
||||
buf.WriteString(fmt.Sprintf(" %v: %v", idx, err))
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user