mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
client: Client.ContainerStats: close reader on context cancellation
Use a cancelReadCloser to automatically close the reader when the context is cancelled. Consumers are still recommended to manually close the reader, but the cancelReadCloser makes the Close idempotent. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -43,6 +43,8 @@ type ContainerStatsResult struct {
|
||||
// ContainerStats retrieves live resource usage statistics for the specified
|
||||
// container. The caller must close the [io.ReadCloser] in the returned result
|
||||
// to release associated resources.
|
||||
//
|
||||
// The underlying [io.ReadCloser] is automatically closed if the context is canceled,
|
||||
func (cli *Client) ContainerStats(ctx context.Context, containerID string, options ContainerStatsOptions) (ContainerStatsResult, error) {
|
||||
containerID, err := trimID("container", containerID)
|
||||
if err != nil {
|
||||
@@ -68,6 +70,6 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, optio
|
||||
}
|
||||
|
||||
return ContainerStatsResult{
|
||||
Body: resp.Body,
|
||||
Body: newCancelReadCloser(ctx, resp.Body),
|
||||
}, nil
|
||||
}
|
||||
|
||||
4
vendor/github.com/moby/moby/client/container_stats.go
generated
vendored
4
vendor/github.com/moby/moby/client/container_stats.go
generated
vendored
@@ -43,6 +43,8 @@ type ContainerStatsResult struct {
|
||||
// ContainerStats retrieves live resource usage statistics for the specified
|
||||
// container. The caller must close the [io.ReadCloser] in the returned result
|
||||
// to release associated resources.
|
||||
//
|
||||
// The underlying [io.ReadCloser] is automatically closed if the context is canceled,
|
||||
func (cli *Client) ContainerStats(ctx context.Context, containerID string, options ContainerStatsOptions) (ContainerStatsResult, error) {
|
||||
containerID, err := trimID("container", containerID)
|
||||
if err != nil {
|
||||
@@ -68,6 +70,6 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, optio
|
||||
}
|
||||
|
||||
return ContainerStatsResult{
|
||||
Body: resp.Body,
|
||||
Body: newCancelReadCloser(ctx, resp.Body),
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user