-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tarexport: Plumb ctx, add OTEL spans, handle cancellation
Pass `context.Context` through `tarexport.Load` and `tarexport.Save`. Create OTEL spans for the most time consuming operations. Also, handle context cancellations to actually end saving/loading when the operation is cancelled - before this PR the daemon would still be performing the operation even though the user already cancelled it. Signed-off-by: Paweł Gronowski <[email protected]>
- Loading branch information
Showing
6 changed files
with
195 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,57 @@ | ||
package ioutils | ||
|
||
import ( | ||
"context" | ||
"io" | ||
) | ||
|
||
// CopyCtx copies from src to dst until either EOF is reached on src or a context is cancelled. | ||
// The writer is not closed when the context is cancelled. | ||
// | ||
// After CopyCtx exits due to context cancellation, the goroutine that performed | ||
// the copy may still be running if either the reader or writer blocks. | ||
func CopyCtx(ctx context.Context, dst io.Writer, src io.Reader) (n int64, err error) { | ||
copyDone := make(chan struct{}) | ||
|
||
src = &readerCtx{ctx: ctx, r: src} | ||
|
||
go func() { | ||
n, err = io.Copy(dst, src) | ||
close(copyDone) | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return -1, ctx.Err() | ||
case <-copyDone: | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
type readerCtx struct { | ||
ctx context.Context | ||
r io.Reader | ||
} | ||
|
||
// NewCtxReader wraps the given reader with a reader that doesn't proceed with | ||
// reading if the context is done. | ||
// | ||
// Note: Read will still block if the underlying reader blocks. | ||
func NewCtxReader(ctx context.Context, r io.Reader) io.Reader { | ||
return &readerCtx{ctx: ctx, r: r} | ||
} | ||
|
||
func (r *readerCtx) Read(p []byte) (n int, err error) { | ||
if err := r.ctx.Err(); err != nil { | ||
return 0, err | ||
} | ||
|
||
n, outErr := r.r.Read(p) | ||
|
||
if err := r.ctx.Err(); err != nil { | ||
return 0, err | ||
} | ||
|
||
return n, outErr | ||
} |
Oops, something went wrong.