Files
ziti/controller/handler_edge_ctrl/connect_events.go
Paul Lorenz ae806045b5 Convert self-describing receive handlers to channel/v5. For #3983
- channel.TypedReceiveHandler -> channel.ContentTypeReceiver
- binding.AddTypedReceiveHandler(h) -> channel.AddReceiveHandlers(binding, h)

channel/v5 repurposes TypedReceiveHandler for the senders-typed handler and replaces the
self-describing pattern with ContentTypeReceiver plus the AddReceiveHandlers free function
(openziti/channel#262). Mechanical conversion; does not build on its own.
2026-06-18 12:51:02 -04:00

123 lines
3.8 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_edge_ctrl
import (
"slices"
"time"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v5"
"github.com/openziti/foundation/v2/goroutines"
"github.com/openziti/ziti/v2/common/ctrlchan"
"github.com/openziti/ziti/v2/common/pb/edge_ctrl_pb"
"github.com/openziti/ziti/v2/controller/env"
"github.com/openziti/ziti/v2/controller/event"
"google.golang.org/protobuf/proto"
)
type connectEventsHandler struct {
appEnv *env.AppEnv
ch ctrlchan.CtrlChannel
pool goroutines.Pool
}
// NewConnectEventsHandler creates a handler that processes identity connect/disconnect
// events from a router. Each handler gets its own single-worker pool to ensure events
// from the same router are processed in order.
func NewConnectEventsHandler(appEnv *env.AppEnv, ch ctrlchan.CtrlChannel) channel.ContentTypeReceiver {
cfg := appEnv.GetConfig().ConnectEventsConfig
pool, err := goroutines.NewPool(goroutines.PoolConfig{
QueueSize: cfg.QueueSize,
MinWorkers: 0,
MaxWorkers: 1,
IdleTime: cfg.IdleTime,
CloseNotify: ch.GetChannel().CloseNotify(),
PanicHandler: func(err interface{}) {
pfxlog.Logger().WithField("routerId", ch.PeerId()).
Errorf("panic in connect events handler: %v", err)
},
})
if err != nil {
pfxlog.Logger().WithField("routerId", ch.PeerId()).WithError(err).
Fatal("failed to create connect events pool")
}
return &connectEventsHandler{
appEnv: appEnv,
ch: ch,
pool: pool,
}
}
func (self *connectEventsHandler) ContentType() int32 {
return int32(edge_ctrl_pb.ContentType_ConnectEventsTypes)
}
func (self *connectEventsHandler) HandleReceive(msg *channel.Message, ch channel.Channel) {
req := &edge_ctrl_pb.ConnectEvents{}
if err := proto.Unmarshal(msg.Body, req); err != nil {
pfxlog.Logger().WithError(err).Error("could not convert message to ConnectEvents")
return
}
if err := self.pool.QueueOrError(func() {
self.HandleConnectEvents(req, ch)
}); err != nil {
pfxlog.Logger().WithError(err).Error("failed to queue connect events for processing")
}
}
func (self *connectEventsHandler) HandleConnectEvents(req *edge_ctrl_pb.ConnectEvents, ch channel.Channel) {
identityManager := self.appEnv.Managers.Identity
if req.FullState {
identityManager.GetConnectionTracker().SyncAllFromRouter(req, self.ch)
}
var events []*event.ConnectEvent
for _, identityEvent := range req.Events {
for _, connect := range identityEvent.ConnectTimes {
events = append(events, &event.ConnectEvent{
Namespace: event.ConnectEventNS,
SrcType: event.ConnectSourceIdentity,
DstType: event.ConnectDestinationRouter,
SrcId: identityEvent.IdentityId,
SrcAddr: connect.SrcAddr,
DstId: ch.Id(),
DstAddr: connect.DstAddr,
Timestamp: time.UnixMilli(connect.ConnectTime),
})
}
if !req.FullState {
if identityEvent.IsConnected {
identityManager.GetConnectionTracker().MarkConnected(identityEvent.IdentityId, self.ch)
} else {
identityManager.GetConnectionTracker().MarkDisconnected(identityEvent.IdentityId, self.ch)
}
}
}
slices.SortFunc(events, func(a, b *event.ConnectEvent) int {
return int(a.Timestamp.UnixMilli() - b.Timestamp.UnixMilli())
})
for _, evt := range events {
self.appEnv.GetEventDispatcher().AcceptConnectEvent(evt)
}
}