Code Monkey home page Code Monkey logo

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

Watchers

 avatar  avatar  avatar  avatar

Forkers

andrejacobs

contextio's Issues

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?

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
}

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.