GithubHelp home page GithubHelp logo

dolmen-go / contextio Goto Github PK

View Code? Open in Web Editor NEW
51.0 4.0 1.0 15 KB

Context-aware I/O streams (Writer, Reader) for Go

Home Page: https://pkg.go.dev/github.com/dolmen-go/contextio

License: Apache License 2.0

Go 88.35% Makefile 11.65%
go golang-module context hacktoberfest

contextio's Introduction

contextio - Context-aware I/O streams for Go

GoDoc stability: stable codecov Travis-CI Go Report Card

Author: @dolmen (Olivier Mengué).

Example

go test -run ExampleWriter

func main() {
	// interrupt context after 500ms
	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
	defer cancel()
	// interrupt context with SIGTERM (CTRL+C)
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt)
	go func() {
		<-sigs
		cancel()
	}()

	f, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	// Writer that fails when context is canceled
	out := contextio.NewWriter(ctx, f)

	// Infinite loop. Will only be interrupted once write fails.
	for {
		if _, err := out.Write([]byte{'a', 'b', 'c'}); err != nil {
			fmt.Println("Err:", err)
			break
		}
	}

	fmt.Println("Closing.")
}

See Also

contextio's People

Contributors

dolmen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

andrejacobs

contextio's Issues

Cancel reader when read is blocking

I tried to solve a simular problem then your reader. But I work with streams, that can block for a long time. Here is a short example:

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	scanner := bufio.NewScanner(contextio.NewReader(ctx, os.Stdin))
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		log.Fatalf("Error reading from stdin: %v", err)
	}
}

This program is similar to cat but (should) stop after 5 seconds. Every line to type in stdin is printed back to stdout.

But if you stop typing before the 5 seconds are over, the program does not exists.

I don't know if this kind of problem is out of scope of tihs repo. For every one how is locking for a solution, here is a reader that stops a call to Read(), even when the input source is blocking:

type reader struct {
	ctx context.Context
	r   io.Reader
}

func NewReader(ctx context.Context, r io.Reader) io.Reader {
	return &reader{ctx, r}
}

func (r *reader) Read(p []byte) (n int, err error) {
	wait := make(chan struct{})

	go func() {
		n, err = r.r.Read(p)
		close(wait)
	}()

	select {
	case <-wait:
	case <-r.ctx.Done():
		err = io.EOF
	}
	return n, err
}

Release this module

This is a very useful module and it would be great if it had an official golang release for easy integration with dependabot. Would it be possible to tag the module with a SemVer version e.g. v1.0.0?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.