forked from earthly/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 1
fix(registry): end a proxied response on half-close, not 50ms of silence #24
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
Open
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3208e03
test(registry): pin what the proxy owes a docker pull
kmannislands 989ed9d
fix(registry): end a proxied response on half-close, not 50ms of silence
kmannislands b78c6a7
refactor(registry): drop StreamRW, which the tunnel no longer needs
kmannislands 2da6a90
refactor(registry): name the copy buffer size and say why it is 32KiB
kmannislands 326d2a6
lint: stop forbidding fmt.Errorf
kmannislands 1e3c8d6
refactor(registry): std errors, and error messages that name the oper…
kmannislands f6fe20c
fix(registry): group the errors import with the standard library
kmannislands eb19b68
refactor(registry): branch on the read error before forwarding bytes
kmannislands File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package earthly_registry_v1 //nolint:revive | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| // copyBufferSize is the size of the buffer each direction reads into. 32KiB is | ||
| // io.Copy's own default, and what session/sshforward.Copy and | ||
| // session/socketforward use for the same job; it is comfortably under gRPC's | ||
| // 4MiB default maximum message size, so a full buffer never needs splitting | ||
| // across messages. Nothing depends on the two ends of the tunnel choosing the | ||
| // same value -- that they happened to agree is what kept StreamRW's leftover | ||
| // handling from ever being exercised -- so this is a throughput knob and | ||
| // nothing more. | ||
| const copyBufferSize = 32 * 1024 | ||
|
|
||
| // Stream is the part of a gRPC bidirectional stream the tunnel needs. Both | ||
| // ends of Registry.Proxy satisfy it, so the same copy runs on the daemon and | ||
| // on the client. | ||
| type Stream interface { | ||
| SendMsg(m any) error | ||
| RecvMsg(m any) error | ||
| } | ||
|
|
||
| // Copy joins a connection to a gRPC stream in both directions and returns once | ||
| // both are done. The bytes are opaque: nothing here knows where one HTTP | ||
| // request or response ends, because nothing needs to. Each direction ends when | ||
| // its source says so -- io.EOF from the connection, or a peer that closed its | ||
| // send direction -- and that end is passed on as a half-close, so the other | ||
| // side can finish what it still owes before the whole conversation is torn | ||
| // down. | ||
| // | ||
| // This mirrors session/sshforward.Copy, which has carried forwarded agent | ||
| // sockets for years; see that file for the same shape with commentary. | ||
| func Copy(ctx context.Context, conn io.ReadWriteCloser, stream Stream, closeStream func() error) error { | ||
| defer conn.Close() | ||
|
|
||
| eg, ctx := errgroup.WithContext(ctx) | ||
|
|
||
| // Peer to connection. | ||
| eg.Go(func() error { | ||
| msg := &ByteMessage{} | ||
| for { | ||
| if err := stream.RecvMsg(msg); err != nil { | ||
| if errors.Is(err, io.EOF) { | ||
| // The peer has finished sending. It is still reading, so | ||
| // close only this direction and leave the response to | ||
| // come back. | ||
| if closeWriter, ok := conn.(interface{ CloseWrite() error }); ok { | ||
| // Best effort: the read half stays open either way. | ||
| closeWriter.CloseWrite() | ||
| } else { | ||
| conn.Close() | ||
| } | ||
| return nil | ||
| } | ||
| conn.Close() | ||
| return fmt.Errorf("receive from stream: %w", err) | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| conn.Close() | ||
| return context.Cause(ctx) | ||
| default: | ||
| } | ||
|
|
||
| if _, err := conn.Write(msg.GetData()); err != nil { | ||
| conn.Close() | ||
| return fmt.Errorf("write to connection: %w", err) | ||
| } | ||
|
|
||
| msg.Data = msg.Data[:0] | ||
| } | ||
| }) | ||
|
|
||
| // Connection to peer. | ||
| eg.Go(func() error { | ||
| buf := make([]byte, copyBufferSize) | ||
|
|
||
| send := func(n int) error { | ||
| if n == 0 { | ||
| return nil | ||
| } | ||
| if err := stream.SendMsg(&ByteMessage{Data: buf[:n]}); err != nil { | ||
| return fmt.Errorf("send to stream: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| for { | ||
| n, err := conn.Read(buf) | ||
| switch { | ||
| case errors.Is(err, io.EOF): | ||
| // Everything the connection had to say has been said. A final | ||
| // read is allowed to hand back bytes alongside the EOF, so | ||
| // they still go out before the stream is closed. | ||
| if err := send(n); err != nil { | ||
| return err | ||
| } | ||
| if closeStream != nil { | ||
| if err := closeStream(); err != nil { | ||
| return fmt.Errorf("close stream: %w", err) | ||
| } | ||
| } | ||
| return nil | ||
| case err != nil: | ||
| // A read error on the connection is terminal. Whatever is in | ||
| // the buffer belongs to a response that will never be | ||
| // completed, so there is nothing worth forwarding. | ||
| return fmt.Errorf("read from connection: %w", err) | ||
| } | ||
|
|
||
| if err := send(n); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| return context.Cause(ctx) | ||
| default: | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| return eg.Wait() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.