Files
ziti/router/handler_ctrl/bind.go
T
Paul Lorenz de2ba06f98 Lift link fault handling off the control channel receive goroutine
A fault was handled by spawning a goroutine per message, so a burst of faults
spawned without limit. Handling them inline instead would put the burst on the
receive goroutine, which carries every other default priority message on that
underlay.

- adds a pool for work lifted off a control channel's receive goroutine, kept separate from the rate limiter pool, whose workers park waiting for controller send capacity; sharing it would make receiving depend on the very back-pressure that lifting work off the receive goroutine exists to survive
- hands fault handling to that pool, and falls back to spawning when the pool is full rather than dropping, since a dropped fault leaves a dead link marked up with no repair loop behind it
- counts the fallbacks, so the pool being too small is visible rather than silent
- sizes the queue generously and the worker count modestly: the work arrives in bursts, one per controller reconnect, but is per control channel and there are only ever a handful of those
2026-10-17 22:07:44 -04:00

144 lines
5.6 KiB
Go

/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handler_ctrl
import (
"fmt"
"runtime/debug"
"time"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v5"
"github.com/openziti/foundation/v2/goroutines"
"github.com/openziti/ziti/v2/common/capabilities"
"github.com/openziti/ziti/v2/common/ctrlchan"
"github.com/openziti/ziti/v2/common/servermetrics"
"github.com/openziti/ziti/v2/common/trace"
"github.com/openziti/ziti/v2/router/env"
"github.com/openziti/ziti/v2/router/forwarder"
"github.com/sirupsen/logrus"
)
type bindHandler struct {
env env.RouterEnv
forwarder *forwarder.Forwarder
xgDialerPool goroutines.Pool
terminatorValidationPool goroutines.Pool
ctrlAddrChangeHandler channel.ContentTypeReceiver
clusterLeaderChangeHandler channel.ContentTypeReceiver
}
func XgressDialerWorker(_ uint32, f func()) {
f()
}
func NewBindHandler(routerEnv env.RouterEnv, forwarder *forwarder.Forwarder) (channel.BindHandler, error) {
xgDialerPoolConfig := goroutines.PoolConfig{
QueueSize: uint32(forwarder.Options.XgressDial.QueueLength),
MinWorkers: 0,
MaxWorkers: uint32(forwarder.Options.XgressDial.WorkerCount),
IdleTime: 30 * time.Second,
CloseNotify: routerEnv.GetCloseNotify(),
PanicHandler: func(err interface{}) {
pfxlog.Logger().WithField(logrus.ErrorKey, err).WithField("backtrace", string(debug.Stack())).Error("panic during xgress dial")
},
WorkerFunction: XgressDialerWorker,
}
servermetrics.ConfigureGoroutinesPoolMetrics(&xgDialerPoolConfig, routerEnv.GetMetricsRegistry(), "pool.route.handler")
xgDialerPool, err := goroutines.NewPool(xgDialerPoolConfig)
if err != nil {
return nil, fmt.Errorf("error creating xgress route handler pool (%w)", err)
}
terminatorValidatorPoolConfig := goroutines.PoolConfig{
QueueSize: uint32(1),
MinWorkers: 0,
MaxWorkers: uint32(50),
IdleTime: 30 * time.Second,
CloseNotify: routerEnv.GetCloseNotify(),
PanicHandler: func(err interface{}) {
pfxlog.Logger().WithField(logrus.ErrorKey, err).WithField("backtrace", string(debug.Stack())).Error("panic during terminator validation operation")
},
WorkerFunction: terminatorValidatorWorker,
}
servermetrics.ConfigureGoroutinesPoolMetrics(&terminatorValidatorPoolConfig, routerEnv.GetMetricsRegistry(), "pool.terminator_validation")
terminatorValidationPool, err := goroutines.NewPool(terminatorValidatorPoolConfig)
if err != nil {
return nil, fmt.Errorf("error creating terminator validation pool (%w)", err)
}
return &bindHandler{
env: routerEnv,
forwarder: forwarder,
xgDialerPool: xgDialerPool,
terminatorValidationPool: terminatorValidationPool,
ctrlAddrChangeHandler: newUpdateCtrlAddressesHandler(routerEnv),
clusterLeaderChangeHandler: newUpdateClusterLeaderHandler(routerEnv),
}, nil
}
func terminatorValidatorWorker(_ uint32, f func()) {
f()
}
func (self *bindHandler) BindChannel(binding channel.Binding) error {
if !capabilities.IsCapable(binding.GetChannel().Underlay().Headers(), capabilities.ControllerSupportsJWTLegacySessions) {
pfxlog.Logger().WithField("ctrlId", binding.GetChannel().Id()).
Error("controller does not support JWT format legacy sessions, use with controller versions 2.0+")
return fmt.Errorf("controller %s does not support JWT format legacy sessions", binding.GetChannel().Id())
}
ctrlCh := binding.GetChannel().GetSenders().(ctrlchan.CtrlChannel)
channel.AddReceiveHandlers(binding, newPeerStateChangeHandler(self.env))
channel.AddReceiveHandlers(binding, newRouteHandler(ctrlCh, self.env, self.forwarder, self.xgDialerPool))
channel.AddReceiveHandlers(binding, newValidateTerminatorsHandler(self.env))
channel.AddReceiveHandlers(binding, newValidateTerminatorsV2Handler(self.env, self.terminatorValidationPool))
channel.AddReceiveHandlers(binding, newUnrouteHandler(self.forwarder))
channel.AddReceiveHandlers(binding, newTraceHandler(self.env.GetRouterId(), self.forwarder.TraceController(), binding.GetChannel()))
channel.AddReceiveHandlers(binding, self.env.GetInspectHandler())
channel.AddReceiveHandlers(binding, newCheckStaleLinksHandler(self.env))
channel.AddReceiveHandlers(binding, newSettingsHandler(self.env))
channel.AddReceiveHandlers(binding, newFaultHandler(self.env))
channel.AddReceiveHandlers(binding, self.ctrlAddrChangeHandler)
channel.AddReceiveHandlers(binding, self.clusterLeaderChangeHandler)
binding.AddPeekHandler(trace.NewChannelPeekHandler(self.env.GetRouterId().Token, binding.GetChannel(), self.forwarder.TraceController()))
ctrl := self.env.GetNetworkControllers().GetNetworkController(binding.GetChannel().Id())
if ctrl == nil {
return fmt.Errorf("controller [%v] not registered, cannot configure", binding.GetChannel().Id())
}
channel.ConfigureHeartbeat(binding, self.env.GetHeartbeatOptions().SendInterval, self.env.GetHeartbeatOptions().CheckInterval, ctrl.HeartbeatCallback())
if self.env.GetTraceHandler() != nil {
binding.AddPeekHandler(self.env.GetTraceHandler())
}
for _, x := range self.env.GetXrctrls() {
if err := x.BindChannel(binding); err != nil {
return err
}
}
return nil
}