Skip to content

Commit d6e7edb

Browse files
committed
chore: lower cyclomatic complexity for (*TCPPortForwarder).handleRequest
Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
1 parent 47f7ef4 commit d6e7edb

1 file changed

Lines changed: 64 additions & 57 deletions

File tree

cmd/portforward_tcp.go

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,69 @@ func (p *TCPPortForwarder) Run(
9696
}
9797
}
9898
}
99+
func (p *TCPPortForwarder) handleInboundMessages(
100+
ctx context.Context,
101+
conn net.Conn,
102+
sessionID string,
103+
connectionID string,
104+
ackChan <-chan struct{},
105+
msgChan chan<- *ws.ProtoMsg,
106+
recvChan <-chan *ws.ProtoMsg) {
107+
sendStopMessage := true
108+
defer func() {
109+
conn.Close()
110+
if sendStopMessage {
111+
m := &ws.ProtoMsg{
112+
Header: ws.ProtoHdr{
113+
Proto: ws.ProtoTypePortForward,
114+
MsgType: wspf.MessageTypePortForwardStop,
115+
SessionID: sessionID,
116+
Properties: map[string]interface{}{
117+
wspf.PropertyConnectionID: connectionID,
118+
},
119+
},
120+
}
121+
msgChan <- m
122+
}
123+
}()
124+
125+
for {
126+
select {
127+
case m := <-recvChan:
128+
if m.Header.Proto == ws.ProtoTypePortForward &&
129+
m.Header.MsgType == wspf.MessageTypePortForwardStop {
130+
sendStopMessage = false
131+
return
132+
} else if m.Header.Proto == ws.ProtoTypePortForward &&
133+
m.Header.MsgType == wspf.MessageTypePortForward {
134+
_, err := conn.Write(m.Body)
135+
if err != nil {
136+
if errors.Unwrap(err) != net.ErrClosed {
137+
fmt.Fprintf(os.Stderr, "error: %v\n", err.Error())
138+
}
139+
} else {
140+
// send the ack
141+
m := &ws.ProtoMsg{
142+
Header: ws.ProtoHdr{
143+
Proto: ws.ProtoTypePortForward,
144+
MsgType: wspf.MessageTypePortForwardAck,
145+
SessionID: sessionID,
146+
Properties: map[string]interface{}{
147+
wspf.PropertyConnectionID: connectionID,
148+
},
149+
},
150+
}
151+
msgChan <- m
152+
}
153+
} else if m.Header.Proto == ws.ProtoTypePortForward &&
154+
m.Header.MsgType == wspf.MessageTypePortForwardAck {
155+
<-ackChan
156+
}
157+
case <-ctx.Done():
158+
return
159+
}
160+
}
161+
}
99162

100163
func (p *TCPPortForwarder) handleRequest(
101164
ctx context.Context,
@@ -105,7 +168,6 @@ func (p *TCPPortForwarder) handleRequest(
105168
recvChan chan *ws.ProtoMsg,
106169
msgChan chan *ws.ProtoMsg,
107170
) {
108-
defer conn.Close()
109171

110172
ackChan := make(chan struct{})
111173
defer func() { close(ackChan) }()
@@ -137,66 +199,11 @@ func (p *TCPPortForwarder) handleRequest(
137199
}
138200
msgChan <- m
139201

140-
sendStopMessage := true
141-
defer func() {
142-
conn.Close()
143-
if sendStopMessage {
144-
m := &ws.ProtoMsg{
145-
Header: ws.ProtoHdr{
146-
Proto: ws.ProtoTypePortForward,
147-
MsgType: wspf.MessageTypePortForwardStop,
148-
SessionID: sessionID,
149-
Properties: map[string]interface{}{
150-
wspf.PropertyConnectionID: connectionID,
151-
},
152-
},
153-
}
154-
msgChan <- m
155-
}
156-
}()
157-
158202
// go routine to handle the network connection
159203
go p.handleRequestConnection(dataChan, errChan, conn)
160204

161205
// go routine to handle received messages
162-
go func(connectionID string) {
163-
for {
164-
select {
165-
case m := <-recvChan:
166-
if m.Header.Proto == ws.ProtoTypePortForward &&
167-
m.Header.MsgType == wspf.MessageTypePortForwardStop {
168-
sendStopMessage = false
169-
return
170-
} else if m.Header.Proto == ws.ProtoTypePortForward &&
171-
m.Header.MsgType == wspf.MessageTypePortForward {
172-
_, err := conn.Write(m.Body)
173-
if err != nil {
174-
if errors.Unwrap(err) != net.ErrClosed {
175-
fmt.Fprintf(os.Stderr, "error: %v\n", err.Error())
176-
}
177-
} else {
178-
// send the ack
179-
m := &ws.ProtoMsg{
180-
Header: ws.ProtoHdr{
181-
Proto: ws.ProtoTypePortForward,
182-
MsgType: wspf.MessageTypePortForwardAck,
183-
SessionID: sessionID,
184-
Properties: map[string]interface{}{
185-
wspf.PropertyConnectionID: connectionID,
186-
},
187-
},
188-
}
189-
msgChan <- m
190-
}
191-
} else if m.Header.Proto == ws.ProtoTypePortForward &&
192-
m.Header.MsgType == wspf.MessageTypePortForwardAck {
193-
<-ackChan
194-
}
195-
case <-ctx.Done():
196-
return
197-
}
198-
}
199-
}(connectionID)
206+
go p.handleInboundMessages(ctx, conn, sessionID, connectionID, ackChan, msgChan, recvChan)
200207

201208
// go routine to handle sent messages
202209
for err == nil {

0 commit comments

Comments
 (0)