-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker.go
More file actions
237 lines (194 loc) · 5.69 KB
/
worker.go
File metadata and controls
237 lines (194 loc) · 5.69 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2025 Blindspot Software
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dutagent
import (
"context"
"errors"
"fmt"
"io"
"log"
"github.com/BlindspotSoftware/dutctl/internal/chanio"
pb "github.com/BlindspotSoftware/dutctl/protobuf/gen/dutctl/v1"
)
// toClientWorker sends messages from the module session to the client.
// This function is an infinite loop. It terminates when the session's done channel is closed.
//
//nolint:cyclop, funlen
func toClientWorker(ctx context.Context, stream Stream, s *session) error {
for {
select {
case <-ctx.Done():
return nil
case str := <-s.printCh:
res := &pb.RunResponse{
Msg: &pb.RunResponse_Print{Print: &pb.Print{Text: []byte(str)}},
}
err := stream.Send(res)
if err != nil {
return err
}
case bytes := <-s.stdoutCh:
res := &pb.RunResponse{
Msg: &pb.RunResponse_Console{Console: &pb.Console{Data: &pb.Console_Stdout{Stdout: bytes}}},
}
err := stream.Send(res)
if err != nil {
return err
}
case bytes := <-s.stderrCh:
res := &pb.RunResponse{
Msg: &pb.RunResponse_Console{Console: &pb.Console{Data: &pb.Console_Stderr{Stderr: bytes}}},
}
err := stream.Send(res)
if err != nil {
return err
}
case name := <-s.fileReqCh:
res := &pb.RunResponse{
Msg: &pb.RunResponse_FileRequest{FileRequest: &pb.FileRequest{Path: name}},
}
err := stream.Send(res)
if err != nil {
return err
}
s.currentFile = name
case file := <-s.fileCh:
r, err := chanio.NewChanReader(file)
if err != nil {
return err
}
content, err := io.ReadAll(r)
if err != nil {
return err
}
log.Printf("Received file from module, sending to client. Name: %q, Size %d", s.currentFile, len(content))
res := &pb.RunResponse{
Msg: &pb.RunResponse_File{
File: &pb.File{
Path: s.currentFile,
Content: content,
},
},
}
err = stream.Send(res)
if err != nil {
return err
}
s.currentFile = ""
}
}
}
// fromClientWorker reads messages from the client and passes them to the module session.
// This function is an infinite loop. It terminates when the session's done channel is closed.
//
//nolint:cyclop,funlen,gocognit
func fromClientWorker(ctx context.Context, stream Stream, s *session) error {
type recvResult struct {
req *pb.RunRequest
err error
}
// Single goroutine performing blocking Receive calls and forwarding results.
resCh := make(chan recvResult)
// Receive loop goroutine rationale:
//
// We offload blocking stream.Receive calls to this goroutine so the main select
// can remain responsive to ctx cancellation. The goroutine will keep calling
// Receive until an error (including io.EOF) occurs, then return.
//
// Potential leak concern: If ctx is cancelled while Receive is blocked the
// goroutine keeps waiting. This is acceptable because, by contract, the RPC
// stream is closed by the client (EOF) or ends with an error shortly after
// module completion / broker cancellation; that closure unblocks Receive and
// the goroutine exits, so it does not leak for the lifetime of the process.
go func() {
for {
req, err := stream.Receive()
resCh <- recvResult{req: req, err: err}
if err != nil { // stop receiving after any error (including EOF)
return
}
}
}()
for {
select {
case <-ctx.Done():
// Cancellation path: opportunistically drain one pending receive.
select {
case r := <-resCh:
if r.err != nil && !errors.Is(r.err, io.EOF) {
return r.err
}
return nil
default:
return nil
}
case r := <-resCh:
if r.err != nil {
if errors.Is(r.err, io.EOF) {
return nil
}
return r.err
}
if r.req == nil { // Defensive: shouldn't happen unless stream.Receive misbehaves
log.Println("Received nil request without error; ignoring")
continue
}
reqMsg := r.req.GetMsg()
switch msg := reqMsg.(type) {
case *pb.RunRequest_Console:
msgConsoleData := msg.Console.GetData()
switch consoleMsg := msgConsoleData.(type) {
case *pb.Console_Stdin:
stdin := consoleMsg.Stdin
if stdin == nil {
log.Println("Received nil stdin message")
continue
}
log.Printf("Server received stdin from client: %q", string(stdin))
select {
case <-ctx.Done():
return nil
case s.stdinCh <- stdin:
}
log.Println("Passed stdin to module")
default:
log.Printf("Unexpected Console message %T", consoleMsg)
}
case *pb.RunRequest_File:
fileMsg := msg.File
if fileMsg == nil {
log.Println("Received empty file message")
return fmt.Errorf("bad file transfer: received empty file-message")
}
if s.currentFile == "" {
log.Println("Received file without a request")
return fmt.Errorf("bad file transfer: received file-message without a former request")
}
path := fileMsg.GetPath()
content := fileMsg.GetContent()
if content == nil {
log.Println("Received file message with empty content")
return fmt.Errorf("bad file transfer: received file-message without content")
}
if path != s.currentFile {
log.Printf("Received unexpected file %q - ignoring!", path)
return fmt.Errorf("bad file transfer: received file-message %q but requested %q", path, s.currentFile)
}
log.Printf("Server received file %q from client", path)
file := make(chan []byte, 1)
select {
case <-ctx.Done():
return nil
case s.fileCh <- file:
}
file <- content
close(file)
log.Println("Passed file to module (buffered in the session)")
s.currentFile = ""
default:
log.Printf("Unexpected message type %T", msg)
}
}
}
}