ctxcopy

package
v0.0.0-...-e817083 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: MIT, MIT Imports: 3 Imported by: 3

README

ctxcopy

Update: this repo is no longer maintained. Use iocopy instead.

ctxcopy is a Golang package which provides helper functions for performing context-aware copy task.

Google's Context

Context-aware

  • ctxcopy.Copy()'s first parameter is Context type.
  • Copy() will stop if context's cancel() function is called in other goroutines.
  • Copy() will stop if context's deadline exceeded.

Example

// 1. Run "go get github.com/northbright/pathhelper" to install pathhelper.
// 2. Run "go test -c && ./ctxcopy.test"
func ExampleCopy() {
    // Download a zip from web server to local storage to test Copy().
    url := "https://github.com/northbright/plants/archive/master.zip"
    totalTimeoutSeconds := 10 // to make download successful, set it to 300 or more.
    totalTimeout := time.Duration(time.Duration(totalTimeoutSeconds) * time.Second)

    // Make context to carry a deadline(timeout).
    // See http://blog.golang.org/context for more information.
    ctx, cancel := context.WithTimeout(context.Background(), totalTimeout)
    defer cancel()

    // Get response body for source.
    resp, err := http.Get(url)
    if err != nil {
        fmt.Fprintf(os.Stderr, "http.Get(%v) err: %v\n", url, err)
        return
    }
    defer resp.Body.Close()

    // Create a file for destination.
    fileName, _ := pathhelper.GetAbsPath("./1.zip")
    f, err := os.Create(fileName)
    if err != nil {
        fmt.Fprintf(os.Stderr, "os.Create(%v) err: %v\n", fileName, err)
        return
    }
    defer f.Sync()
    defer f.Close()

    buf := make([]byte, 2*1024*1024)

    // Copy starts.
    // Copy operation will be canceled if cancel() is called in other goroutine.
    // Copy operation will be stoped if deadline is exceeded(timeout).
    err = ctxcopy.Copy(ctx, f, resp.Body, buf)
    if err != nil {
        fmt.Fprintf(os.Stderr, "ctxcopy.Copy() err: %v\n", err)
    } else {
        fmt.Fprintf(os.Stderr, "ctxcopy.Copy() succeeded.\n")
    }

    // Output:
}

Go Version Requirement

Documentation

License

References

Documentation

Overview

Package ctxcopy is a Golang package which provides helper functions for performing context-aware copy task.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(ctx context.Context, dst io.Writer, src io.Reader, buf []byte) (err error)

Copy reads bytes to buffer from source and write to destination from buffer with cancelation signal.

Params:
  ctx:
    Google's Context type which carries deadlines, cacelation signals,
    and other request-scoped values across API boundaries and between processes.
    See https://godoc.org/golang.org/x/net/context for more.
  dst: Destination.
  src: Source.
  buf: Buffer(length should >= 0).
Example

1. Run "go get github.com/northbright/pathhelper" to install pathhelper. 2. Run "go test -c && ./ctxcopy.test"

package main

import (
	"fmt"
	"net/http"
	"os"
	"time"

	"github.com/northbright/ctx/ctxcopy"
	"github.com/northbright/pathhelper"
	"golang.org/x/net/context"
)

func main() {
	// Download a zip from web server to local storage to test Copy().
	url := "https://github.com/northbright/plants/archive/master.zip"
	totalTimeoutSeconds := 10 // to make download successful, set it to 300 or more.
	totalTimeout := time.Duration(time.Duration(totalTimeoutSeconds) * time.Second)

	// Make context to carry a deadline(timeout).
	// See http://blog.golang.org/context for more information.
	ctx, cancel := context.WithTimeout(context.Background(), totalTimeout)
	defer cancel()

	// Get response body for source.
	resp, err := http.Get(url)
	if err != nil {
		fmt.Fprintf(os.Stderr, "http.Get(%v) err: %v\n", url, err)
		return
	}
	defer resp.Body.Close()

	// Create a file for destination.
	fileName, _ := pathhelper.GetAbsPath("./1.zip")
	f, err := os.Create(fileName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "os.Create(%v) err: %v\n", fileName, err)
		return
	}
	defer f.Sync()
	defer f.Close()

	buf := make([]byte, 2*1024*1024)

	// Copy starts.
	// Copy operation will be canceled if cancel() is called in other goroutine.
	// Copy operation will be stoped if deadline is exceeded(timeout).
	err = ctxcopy.Copy(ctx, f, resp.Body, buf)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ctxcopy.Copy() err: %v\n", err)
	} else {
		fmt.Fprintf(os.Stderr, "ctxcopy.Copy() succeeded.\n")
	}

}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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