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
/
circuit_request.go
147 lines (124 loc) · 4.42 KB
/
circuit_request.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
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 (
"github.com/openziti/fabric/controller/xt"
"google.golang.org/protobuf/proto"
"time"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/fabric/controller/network"
"github.com/openziti/fabric/common/ctrl_msg"
"github.com/openziti/fabric/common/logcontext"
"github.com/openziti/fabric/common/pb/ctrl_pb"
"github.com/openziti/identity"
)
type circuitRequestHandler struct {
r *network.Router
network *network.Network
}
func newCircuitRequestHandler(r *network.Router, network *network.Network) *circuitRequestHandler {
return &circuitRequestHandler{r: r, network: network}
}
func (h *circuitRequestHandler) ContentType() int32 {
return int32(ctrl_pb.ContentType_CircuitRequestType)
}
func (h *circuitRequestHandler) HandleReceive(msg *channel.Message, ch channel.Channel) {
log := pfxlog.ContextLogger(ch.Label()).Entry
request := &ctrl_pb.CircuitRequest{}
if err := proto.Unmarshal(msg.Body, request); err == nil {
/*
* This is running in a goroutine because CreateCircuit does a 'SendAndWait', which cannot be invoked from
* inside a ReceiveHandler (without parallel support).
*/
go func() {
id := &identity.TokenId{Token: request.IngressId, Data: request.PeerData}
service := request.Service
if _, err := h.network.Managers.Services.Read(service); err != nil {
if id, _ := h.network.Managers.Services.GetIdForName(service); id != "" {
service = id
}
}
log = log.WithField("serviceId", service)
if circuit, err := h.network.CreateCircuit(h.newCircuitCreateParms(service, h.r, id)); err == nil {
responseMsg := ctrl_msg.NewCircuitSuccessMsg(circuit.Id, circuit.Path.IngressId)
responseMsg.ReplyTo(msg)
//static terminator peer data
for k, v := range circuit.Terminator.GetPeerData() {
responseMsg.Headers[int32(k)] = v
}
//runtime peer data
for k, v := range circuit.PeerData {
responseMsg.Headers[int32(k)] = v
}
if err := responseMsg.WithTimeout(10 * time.Second).Send(h.r.Control); err != nil {
log.Errorf("unable to respond with success to create circuit request for circuit %v (%s)", circuit.Id, err)
if err := h.network.RemoveCircuit(circuit.Id, true); err != nil {
log.WithError(err).WithField("circuitId", circuit.Id).Error("unable to remove circuit")
}
}
} else {
responseMsg := ctrl_msg.NewCircuitFailedMsg(err.Error())
responseMsg.ReplyTo(msg)
if err := h.r.Control.Send(responseMsg); err != nil {
log.WithError(err).Error("unable to respond with failure to create circuit request for service")
}
}
}()
/* */
} else {
log.Errorf("unexpected error (%s)", err)
}
}
func (h *circuitRequestHandler) newCircuitCreateParms(serviceId string, sourceRouter *network.Router, clientId *identity.TokenId) network.CreateCircuitParams {
return &circuitParams{
serviceId: serviceId,
sourceRouter: sourceRouter,
clientId: clientId,
ctx: logcontext.NewContext(),
deadline: time.Now().Add(h.network.GetOptions().RouteTimeout),
}
}
type circuitParams struct {
serviceId string
sourceRouter *network.Router
clientId *identity.TokenId
ctx logcontext.Context
deadline time.Time
}
func (self *circuitParams) GetServiceId() string {
return self.serviceId
}
func (self *circuitParams) GetSourceRouter() *network.Router {
return self.sourceRouter
}
func (self *circuitParams) GetClientId() *identity.TokenId {
return self.clientId
}
func (self *circuitParams) GetCircuitTags(t xt.CostedTerminator) map[string]string {
if t == nil {
return map[string]string{
"serviceId": self.serviceId,
}
}
return map[string]string{
"serviceId": self.serviceId,
"hostId": t.GetHostId(),
}
}
func (self *circuitParams) GetLogContext() logcontext.Context {
return self.ctx
}
func (self *circuitParams) GetDeadline() time.Time {
return self.deadline
}