-
Notifications
You must be signed in to change notification settings - Fork 131
/
heartbeat.go
104 lines (79 loc) · 2.71 KB
/
heartbeat.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
/*
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 state
import (
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/ziti/common/pb/edge_ctrl_pb"
"github.com/openziti/ziti/common/runner"
"github.com/openziti/ziti/controller/env"
routerEnv "github.com/openziti/ziti/router/env"
"google.golang.org/protobuf/proto"
"time"
)
const maxTokensPerMessage = 10000
type heartbeatOperation struct {
env routerEnv.RouterEnv
*runner.BaseOperation
tokenProvider TokenProvider
}
type TokenProvider interface {
ActiveApiSessionTokens() []string
flushRecentlyRemoved()
}
func newHeartbeatOperation(env routerEnv.RouterEnv, frequency time.Duration, tokenProvider TokenProvider) *heartbeatOperation {
return &heartbeatOperation{
env: env,
tokenProvider: tokenProvider,
BaseOperation: runner.NewBaseOperation("Heartbeat Operation", frequency)}
}
func (operation *heartbeatOperation) Run() error {
tokens := operation.tokenProvider.ActiveApiSessionTokens()
operation.beat(tokens)
operation.tokenProvider.flushRecentlyRemoved()
return nil
}
func (operation *heartbeatOperation) beat(tokens []string) {
var msgs []*channel.Message
pfxlog.Logger().Tracef("heartbeat tokens: %d", len(tokens))
for len(tokens) > 0 {
if maxTokensPerMessage >= len(tokens) {
msg := &edge_ctrl_pb.ApiSessionHeartbeat{
Tokens: tokens,
}
bodyBytes, err := proto.Marshal(msg)
if err != nil {
pfxlog.Logger().Panic("could not marshal SessionHeartbeat type (1)")
}
msgs = append(msgs, channel.NewMessage(env.ApiSessionHeartbeatType, bodyBytes))
tokens = nil
} else {
msg := &edge_ctrl_pb.ApiSessionHeartbeat{
Tokens: tokens[:maxTokensPerMessage],
}
bodyBytes, err := proto.Marshal(msg)
if err != nil {
pfxlog.Logger().Panic("could not marshal SessionHeartbeat type (2)")
}
tokens = tokens[maxTokensPerMessage:]
msgs = append(msgs, channel.NewMessage(env.ApiSessionHeartbeatType, bodyBytes))
}
}
operation.env.GetNetworkControllers().ForEach(func(ctrlId string, ch channel.Channel) {
for _, msg := range msgs {
if err := ch.Send(msg); err != nil {
pfxlog.Logger().WithError(err).Error("could not send heartbeats on control channel")
}
}
})
}