Skip to content

Buffers for synchronised file streaming ๐ŸŽค๐Ÿƒ

License

Notifications You must be signed in to change notification settings

ny0m/syncbuffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

syncbuffer

go.dev reference

Syncbuffer's structs are useful for applications where multiple separate readers need to be kept roughly synchronised with a source; for example, audio or video streaming.

Example

package main

import (
    "fmt"
    "sync"
    "time"

    "github.com/ny0m/syncbuffer"
)

func main() {
    // New buffer with space for 2 items that adds messages every millisecond.
    sb := syncbuffer.NewSyncBuffer(time.Millisecond, 2)
    
    var wg sync.WaitGroup
    wg.Add(1)
    
    // Start streaming somewhere.
    go func() {
        defer wg.Done()
    
        s := syncbuffer.NewStreamer(sb)
        for p := range s.Stream() {
            fmt.Println(p)
        }
    }()
    
    time.Sleep(time.Millisecond)
    
    for i := 0; i < 10; i++ {
        // Add will block until a millisecond has passed.
        sb.Add([]byte{byte(i)})
    }
    
    // Close the buffer and stop its streamers.
    sb.Close()
    
    // Ensure that the program waits for the streamer to finish.
    wg.Wait()
    
    // Output:
    // [0]
    // [1]
    // [2]
    // [3]
    // [4]
    // [5]
    // [6]
    // [7]
    // [8]
    // [9]
}

About

Buffers for synchronised file streaming ๐ŸŽค๐Ÿƒ

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages