-
Notifications
You must be signed in to change notification settings - Fork 129
/
bind.go
121 lines (100 loc) · 4.68 KB
/
bind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
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 (
"runtime/debug"
"time"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/foundation/v2/goroutines"
"github.com/openziti/ziti/common/metrics"
"github.com/openziti/ziti/common/trace"
"github.com/openziti/ziti/router/env"
"github.com/openziti/ziti/router/forwarder"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type bindHandler struct {
env env.RouterEnv
forwarder *forwarder.Forwarder
xgDialerPool goroutines.Pool
terminatorValidationPool goroutines.Pool
ctrlAddressUpdater CtrlAddressUpdater
}
func NewBindHandler(routerEnv env.RouterEnv, forwarder *forwarder.Forwarder, ctrlAddressUpdater CtrlAddressUpdater) (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")
},
}
metrics.ConfigureGoroutinesPoolMetrics(&xgDialerPoolConfig, routerEnv.GetMetricsRegistry(), "pool.route.handler")
xgDialerPool, err := goroutines.NewPool(xgDialerPoolConfig)
if err != nil {
return nil, errors.Wrap(err, "error creating xgress route handler pool")
}
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")
},
}
metrics.ConfigureGoroutinesPoolMetrics(&terminatorValidatorPoolConfig, routerEnv.GetMetricsRegistry(), "pool.terminator_validation")
terminatorValidationPool, err := goroutines.NewPool(terminatorValidatorPoolConfig)
if err != nil {
return nil, errors.Wrap(err, "error creating terminator validation pool")
}
return &bindHandler{
env: routerEnv,
forwarder: forwarder,
xgDialerPool: xgDialerPool,
terminatorValidationPool: terminatorValidationPool,
ctrlAddressUpdater: ctrlAddressUpdater,
}, nil
}
func (self *bindHandler) BindChannel(binding channel.Binding) error {
binding.AddTypedReceiveHandler(newPeerStateChangeHandler(self.env))
binding.AddTypedReceiveHandler(newDialHandler(self.env))
binding.AddTypedReceiveHandler(newRouteHandler(binding.GetChannel(), self.env, self.forwarder, self.xgDialerPool))
binding.AddTypedReceiveHandler(newValidateTerminatorsHandler(self.env))
binding.AddTypedReceiveHandler(newValidateTerminatorsV2Handler(self.env, self.terminatorValidationPool))
binding.AddTypedReceiveHandler(newUnrouteHandler(self.forwarder))
binding.AddTypedReceiveHandler(newTraceHandler(self.env.GetRouterId(), self.forwarder.TraceController(), binding.GetChannel()))
binding.AddTypedReceiveHandler(newInspectHandler(self.env, self.forwarder))
binding.AddTypedReceiveHandler(newSettingsHandler(self.ctrlAddressUpdater))
binding.AddTypedReceiveHandler(newFaultHandler(self.env.GetXlinkRegistry()))
binding.AddTypedReceiveHandler(newUpdateCtrlAddressesHandler(self.ctrlAddressUpdater))
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 errors.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 := binding.Bind(x); err != nil {
return err
}
}
return nil
}