This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
route_result.go
110 lines (95 loc) · 3.28 KB
/
route_result.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
/*
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 (
"bytes"
"encoding/binary"
"github.com/openziti/channel/v2"
"github.com/openziti/fabric/controller/network"
"github.com/openziti/fabric/controller/xt"
"github.com/openziti/fabric/common/ctrl_msg"
"github.com/openziti/fabric/common/pb/ctrl_pb"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
)
type routeResultHandler struct {
network *network.Network
r *network.Router
}
func newRouteResultHandler(network *network.Network, r *network.Router) *routeResultHandler {
return &routeResultHandler{
network: network,
r: r,
}
}
func (self *routeResultHandler) ContentType() int32 {
return ctrl_msg.RouteResultType
}
func (self *routeResultHandler) HandleReceive(msg *channel.Message, _ channel.Channel) {
go self.handleRouteResult(msg)
}
func (self *routeResultHandler) handleRouteResult(msg *channel.Message) {
log := logrus.WithField("routerId", self.r.Id)
if v, found := msg.Headers[ctrl_msg.RouteResultAttemptHeader]; found {
_, success := msg.Headers[ctrl_msg.RouteResultSuccessHeader]
rerr, _ := msg.GetStringHeader(ctrl_msg.RouteResultErrorHeader)
var attempt uint32
buf := bytes.NewBuffer(v)
if err := binary.Read(buf, binary.LittleEndian, &attempt); err == nil {
circuitId := string(msg.Body)
peerData := xt.PeerData{}
for k, v := range msg.Headers {
if k > 0 && (k < ctrl_msg.RouteResultSuccessHeader || k > ctrl_msg.RouteResultErrorCodeHeader) {
peerData[uint32(k)] = v
}
}
rs := &network.RouteStatus{
Router: self.r,
CircuitId: circuitId,
Attempt: attempt,
Success: success,
Err: rerr,
PeerData: peerData,
}
if errCode, hasErrCode := msg.GetByteHeader(ctrl_msg.RouteResultErrorCodeHeader); hasErrCode {
rs.ErrorCode = &errCode
}
routing := self.network.RouteResult(rs)
if !routing && attempt != network.SmartRerouteAttempt {
go self.notRoutingCircuit(circuitId)
}
} else {
log.WithError(err).Error("error reading attempt number from route result")
return
}
} else {
log.Error("missing attempt header in route result")
}
}
func (self *routeResultHandler) notRoutingCircuit(circuitId string) {
log := logrus.WithField("circuitId", circuitId).
WithField("routerId", self.r.Id)
log.Warn("not routing circuit (and not smart re-route), sending unroute")
unroute := &ctrl_pb.Unroute{
CircuitId: circuitId,
Now: true,
}
if body, err := proto.Marshal(unroute); err == nil {
unrouteMsg := channel.NewMessage(int32(ctrl_pb.ContentType_UnrouteType), body)
if err := self.r.Control.Send(unrouteMsg); err != nil {
log.WithError(err).Error("error sending unroute message to router")
}
} else {
log.WithError(err).Error("error sending unroute message to router")
}
}