ctxio

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2023 License: BSD-3-Clause Imports: 5 Imported by: 1

Documentation

Overview

Package ctxio contains functions for performing input and output operations that can be cancelled via context.

This package is mainly useful for copying between *os.Files or net.Conns. If you're copying data using net/http or gRPC or some other library that natively supports cancellation, you might not need this package.

Copy is the primary function in this package; other functions are based on it. Copy reads and writes in parallel and checks whether the given context is cancelled before each operation. When the context is cancelled, Copy stops pending Read and Write calls by calling the Close method on its src and dst arguments. The arguments must support calls to Close that are concurrent with Read and Write, and Read and Write must return quickly. This is generally file for *os.File and net.Conn, but check your implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(ctx context.Context, dst io.WriteCloser, src io.ReadCloser) (written int64, err error)

Copy is like io.Copy, but it can be cancelled via context.

rc.Read and wc.Write may block indefinitely. However, both methods must return quickly after Close is called. Copy calls either rc.Close or wc.Close or both after it detects that the context has been cancelled. Copy does not call Close otherwise.

Copy returns the number of bytes copied and an error. The error may be an error returned from wc.Write, wc.Close, rc.Read, rc.Close, or ctx.Err, or it may contain messages from those methods. If Copy observes that the context was cancelled, then errors.Is(err, ctx.Err()) is true.

func CopyN

func CopyN(ctx context.Context, dst io.WriteCloser, src io.ReadCloser, n int64) (written int64, err error)

CopyN is like Copy, but it reads no more than n bytes from src.

func ReadAll

func ReadAll(ctx context.Context, src io.ReadCloser) ([]byte, error)

ReadAll is like io.ReadAll, but the operation may be interrupted when the context is closed. src.Close must be safe to call concurrently with src.Read, and it must cause src.Read to return quickly. See Copy for other constraints.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL