Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

davidmdm/xio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moved away

This repository has been moved to https://github.com/davidmdm/x/xio

XIO

Package xio is a context-aware port of common io operations. This allows long running operations to be canceled.

API

xio.Copy(context.Context, io.Writer, io.Reader)

xio.CopyBuffer(context.Context, io.Writer, io.Reader)

xio.CopyN(context.Context, io.Writer, io.Reader, int64)

xio.ReadAll(context.Context, io.Reader)

The copy functions accept xio.CopyOption variadic function arguments. They are:

  • func Buffer(b []byte) CopyOption -> Allows us to specify the buffer used for copying data
  • func BufferSize(size int) CopyOption -> Allows us to change the size of the internal buffer used for copying (default 32Kb same as standard io). Not used if a Buffer is specified.
  • WaitForLastOp(value bool) CopyOption -> Fundamentally read and write operations are synchronous, and when the context is canceled xio waits for any ongoing write/read to finish before returning. This allows xio to return the correct amount of bytes copied. When false, Copy returns immediately, but the bytes copied total may be inaccurate. Default true.

Example

The following program sets up a cancelable context by SIGINT, and starts a copy operation. If a SIGINT occurs before the copy is finished, the copy operation will exit.

func main() {
    ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT)
    defer stop()

    n, err := xio.Copy(ctx, dst, src)

    fmt.Println("copied %d bytes with error: %v", n, err)
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages