-
Notifications
You must be signed in to change notification settings - Fork 1
Unblock session calls when broker workers shut down #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import ( | |
|
|
||
| // session implements the module.Session interface. | ||
| type session struct { | ||
| done <-chan struct{} // closed when broker workers shut down; unblocks pending session calls | ||
| printCh chan string | ||
| stdinCh chan []byte | ||
| stdoutCh chan []byte | ||
|
|
@@ -28,15 +29,24 @@ type session struct { | |
| } | ||
|
|
||
| func (s *session) Print(a ...any) { | ||
| s.printCh <- fmt.Sprint(a...) | ||
| select { | ||
| case s.printCh <- fmt.Sprint(a...): | ||
| case <-s.done: | ||
| } | ||
| } | ||
|
|
||
| func (s *session) Printf(format string, a ...any) { | ||
| s.printCh <- fmt.Sprintf(format, a...) | ||
| select { | ||
| case s.printCh <- fmt.Sprintf(format, a...): | ||
| case <-s.done: | ||
| } | ||
| } | ||
|
|
||
| func (s *session) Println(a ...any) { | ||
| s.printCh <- fmt.Sprintln(a...) | ||
| select { | ||
| case s.printCh <- fmt.Sprintln(a...): | ||
| case <-s.done: | ||
| } | ||
| } | ||
|
|
||
| //nolint:nonamedreturns | ||
|
|
@@ -72,9 +82,19 @@ func (s *session) RequestFile(name string) (io.Reader, error) { | |
|
|
||
| log.Printf("Module issued file request for: %q", name) | ||
|
|
||
| s.fileReqCh <- name // Send the file request to the client. | ||
| select { | ||
| case s.fileReqCh <- name: | ||
| case <-s.done: | ||
| return nil, fmt.Errorf("session closed before file request %q could be sent", name) | ||
| } | ||
|
|
||
| var file chan []byte | ||
|
|
||
| file := <-s.fileCh // This will block until the client sends the file. | ||
| select { | ||
| case file = <-s.fileCh: | ||
| case <-s.done: | ||
| return nil, fmt.Errorf("session closed while waiting for file %q", name) | ||
| } | ||
|
RiSKeD marked this conversation as resolved.
RiSKeD marked this conversation as resolved.
|
||
|
|
||
| r, err := chanio.NewChanReader(file) | ||
| if err != nil { | ||
|
|
@@ -99,7 +119,15 @@ func (s *session) SendFile(name string, r io.Reader) error { | |
| s.currentFile = name | ||
|
|
||
| file := make(chan []byte, 1) | ||
| s.fileCh <- file | ||
|
|
||
| select { | ||
| case s.fileCh <- file: | ||
| case <-s.done: | ||
| s.currentFile = "" | ||
|
|
||
|
Comment on lines
+123
to
+127
|
||
| return fmt.Errorf("session closed before file %q could be sent", name) | ||
| } | ||
|
|
||
| file <- content | ||
|
|
||
| close(file) // indicate EOF. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -216,7 +216,13 @@ func fromClientWorker(ctx context.Context, stream Stream, s *session) error { | |||||||
| log.Printf("Server received file %q from client", path) | ||||||||
|
|
||||||||
| file := make(chan []byte, 1) | ||||||||
| s.fileCh <- file | ||||||||
|
|
||||||||
| select { | ||||||||
| case <-ctx.Done(): | ||||||||
|
||||||||
| case <-ctx.Done(): | |
| case <-ctx.Done(): | |
| s.currentFile = "" |
Uh oh!
There was an error while loading. Please reload this page.