https://github.com/dozyio/go-lpstream
A length prefixed varint stream ReadWriter for Go
https://github.com/dozyio/go-lpstream
go length-prefixed readwriter stream varint
Last synced: 10 months ago
JSON representation
A length prefixed varint stream ReadWriter for Go
- Host: GitHub
- URL: https://github.com/dozyio/go-lpstream
- Owner: dozyio
- License: mit
- Created: 2024-08-29T10:19:41.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-29T11:43:12.000Z (almost 2 years ago)
- Last Synced: 2025-02-16T03:32:58.078Z (over 1 year ago)
- Topics: go, length-prefixed, readwriter, stream, varint
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go LPStream
A length prefixed varint stream ReadWriter for Go.
## Example length prefixed network.Stream handler
```go
func handler(s network.Stream) {
lps := lpstream.New(s)
buf, err := lps.Read()
if err != nil {
fmt.Printf("Error reading from stream: %v\n", err)
s.Reset()
return
}
// echo the received buf
err = lps.Write(buf)
if err != nil {
fmt.Printf("Error writing to stream: %v\n", err)
s.Reset()
return
}
s.Close()
}
```
## Example with context
```go
func handler(s network.Stream) {
lps := lpstream.New(s)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
buf, err := lps.Read(lpstream.AbortOptions{Context: ctx})
if err != nil {
fmt.Printf("Error reading from stream: %v\n", err)
s.Reset()
return
}
// echo the received buf
err = lps.Write(buf, lpstream.AbortOptions{Context: ctx})
if err != nil {
fmt.Printf("Error writing to stream: %v\n", err)
s.Reset()
return
}
s.Close()
}
```
See tests for examples of writing multiple buffers with WriteV and setting the max length.
## Tests
### Unit tests
```sh
go test -v ./...
```
### Benchmark Tests
```sh
go test -bench=^Benchmark -benchmem ./...
```
### Fuzz Tests
```sh
./fuzz.sh
```