MCPcopy Index your code
hub / github.com/djherbis/bufit

github.com/djherbis/bufit @v1.2.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.2.3 ↗ · + Follow
69 symbols 239 edges 4 files 23 documented · 33%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Bufit

GoDoc Release Software License Build Status Coverage Status Go Report Card

Usage

A moving buffer which supports multiple concurrent readers.

This buffer shares a single in-memory buffer with multiple readers who can independently Read from the buffer.


import (
  "io"
  "sync"

  "github.com/djherbis/bufit"
)

func main(){
  // Start a new buffer
  buf := bufit.New()

  // Create two readers
  r1, r2 := buf.NextReader(), buf.NextReader()

  // Broadcast a message
  io.WriteString(buf, "Hello World\n")

  // Wait
  var grp sync.WaitGroup
  grp.Add(4)

  // Read fast
  go func() {
    defer grp.Done()
    io.Copy(os.Stdout, r1) // "Hello World\n"
  }()

  // Read slow
  go func() {
    defer grp.Done()
    io.CopyN(os.Stdout, r2, 5) // "Hello"
    <-time.After(time.Second)
    io.Copy(os.Stdout, r2) // "World\n"
  }()

  // Both readers will read the entire buffer! The slow reader
  // won't block the fast one from reading ahead either.

  // Late reader
  // Since this reader joins after all existing readers have Read "Hello"
  // "Hello" has already been cleared from the Buffer, this Reader will only see
  // "World\n" and beyond.
  go func() {
    defer grp.Done()
    <-time.After(500 * time.Millisecond)
    r3 := buf.NextReader()
    io.Copy(os.Stdout, r3) // "World\n"
  }()

  // Short Reader
  // **Important!** if your reader isn't going to read until the buffer is empty
  // you'll need to call Close() when you are done with it to tell the buffer
  // it's done reading data.
  go func() {
    defer grp.Done()
    r4 := buf.NextReader()
    io.CopyN(os.Stdout, r4, 5) // "Hello"
    r4.Close()                 // tell the buffer you're done reading
  }()

  // **Important!** mark close so that readers can ret. io.EOF
  buf.Close()

  grp.Wait()
}

Installation

go get github.com/djherbis/bufit

Extension points exported contracts — how you extend this code

Reader (Interface)
Reader provides an io.Reader whose methods MUST be concurrent-safe with the Write method of the Writer from which it was [1 …
bufit.go
Writer (Interface)
Writer accepts bytes and generates Readers who consume those bytes. Generated Readers methods must be concurrent-safe wi [1 …
bufit.go

Core symbols most depended-on inside this repo

Len
called by 33
bufit.go
Close
called by 28
heap.go
NextReader
called by 23
bufit.go
alive
called by 10
bufit.go
New
called by 10
bufit.go
NewCapped
called by 5
bufit.go
newWriter
called by 4
writer.go
split
called by 4
writer.go

Shape

Method 36
Function 25
Struct 4
Interface 2
TypeAlias 2

Languages

Go100%

Modules by API surface

bufit.go26 symbols
bufit_test.go21 symbols
writer.go12 symbols
heap.go10 symbols

For agents

$ claude mcp add bufit \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact