golang-gopkg-djherbis-stream.v1-dev

golang-gopkg-djherbis-stream.v1-dev 1.3.1-1

 pool/sid/main/g/golang-gopkg-djherbis-stream.v1/golang-gopkg-djherbis-stream.v1-dev_1.3.1-1_all.deb golang-gopkg-djherbis-stream.v1-dev
Stream bytes to multiple independent Readers #golang
Filename pool/sid/main/g/golang-gopkg-djherbis-stream.v1/golang-gopkg-djherbis-stream.v1-dev_1.3.1-1_all.deb
Version1.3.1-1
Age in days707
Release date2024 11. 28.
Size9.18 KB
id113
Namegolang-gopkg-djherbis-stream.v1-dev
Package
Appname
Essential
Vendor
License
Distributionsid
Suitemain
Sourcegolang-gopkg-djherbis-stream.v1
Architecturebinary-i386
MultiArch
MaintainerDebian Go Packaging Team
InstalledSize47
PreDepends
Breaks
Enhances
Sectiondevel
Priorityoptional
DescriptionStream bytes to multiple independent Readers #golang
LongDescription
AutoBuiltPackage
Filenamepool/sid/main/g/golang-gopkg-djherbis-stream.v1/golang-gopkg-djherbis-stream.v1-dev_1.3.1-1_all.deb
MD5sumaa54867b4d675abff47dc4becc7bd9eb
SHA1bb61bb9216e5cd4ebf28d09e4121ee00a6d10cc9
SHA2562f5012c6467a1a0c86694ebdb7bbf30d718a9bf1cd4905df788e818ae11ccad4
SHA512170e4acc075be7c79606da4018b42337de0131cb8fb8f3cdb37ba7856de6f67e4316d30d9b8b39353ad103ff66b6c2cf1dd2598f7c6d1899728e1bd81e5b8b9c
Size9396
Auto-Built-Package
Homepagehttps://github.com/djherbis/stream
Provides
Existing
fileMtime2024-11-28 12:35:02
created2021-03-20 02:24:33
updated2024-11-28 11:35:02
golang-gopkg-djherbis-stream.v1-dev
<>

stream

GoDoc Release Software License go test Coverage Status Go Report Card

Usage

Write and Read concurrently, and independently.

To explain further, if you need to write to multiple places you can use io.MultiWriter, if you need multiple Readers on something you can use io.TeeReader. If you want concurrency you can use io.Pipe().

However all of these methods "tie" each Read/Write together, your readers can't read from different places in the stream, each write must be distributed to all readers in sequence.

This package provides a way for multiple Readers to read off the same Writer, without waiting for the others. This is done by writing to a "File" interface which buffers the input so it can be read at any time from many independent readers. Readers can even be created while writing or after the stream is closed. They will all see a consistent view of the stream and will block until the section of the stream they request is written, all while being unaffected by the actions of the other readers.

The use case for this stems from my other project djherbis/fscache. I needed a byte caching mechanism which allowed many independent clients to have access to the data while it was being written, rather than re-generating the byte stream for each of them or waiting for a complete copy of the stream which could be stored and then re-used.

package main

import(
	"io"
	"log"
	"os"
	"time"

	"github.com/djherbis/stream"
)

func main(){
	w, err := stream.New("mystream")
	if err != nil {
		log.Fatal(err)
	}

	go func(){
		io.WriteString(w, "Hello World!")
		<-time.After(time.Second)
		io.WriteString(w, "Streaming updates...")
		w.Close()
	}()

	waitForReader := make(chan struct{})
	go func(){
		// Read from the stream
		r, err := w.NextReader()
		if err != nil {
			log.Fatal(err)
		}
		io.Copy(os.Stdout, r) // Hello World! (1 second) Streaming updates...
		r.Close()
		close(waitForReader)
	}()

        // Full copy of the stream!
	r, err := w.NextReader() 
	if err != nil {
		log.Fatal(err)
	}
	io.Copy(os.Stdout, r) // Hello World! (1 second) Streaming updates...

	// r supports io.ReaderAt too.
	p := make([]byte, 4)
	r.ReadAt(p, 1) // Read "ello" into p

	r.Close()

	<-waitForReader // don't leave main before go-routine finishes
}

Installation

go get github.com/djherbis/stream