ringbuf

package module
v0.0.0-...-5d4bfd6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 1 Imported by: 0

README

ringbuf

Ring buffer implementation by GO

Concept

Ringbuf implements ring buffer base on channel so never blocks the writer.

It provider three types ring buffer:

  • Normal[ringbuf.New]: ring buffer with fixed size, and when its buffer is full then the oldest value in the buffer is discarded.
  • Infinity[ringbuf.NewInfinity]: ring buffer with infinity size, and the new value is always can be added.
  • Overflow[ringbuf.NewOverflow]: ring buffer with fixed size, and when its buffer is full then the new value is discarded.

Install

go get github.com/JamesYYang/ringbuf

Example


type Message struct {
	Name  string
	Value string
}

func SendMessage(input chan<- Message) {
	for i := 0; i < 50; i++ {
		msg := Message{
			Name:  fmt.Sprintf("Message %d", i),
			Value: fmt.Sprintf("Value %d", i),
		}
		input <- msg
		log.Printf("sending message: %s\n", msg.Name)
		time.Sleep(1 * time.Second)
	}

}

func ReceiveMessage(output <-chan Message) {
	for msg := range output {
		log.Printf("receiving message: %s\n", msg.Name)
		time.Sleep(5 * time.Second)
	}
}

func main() {

	rb := ringbuf.New[Message](10)

	go SendMessage(rb.In())
	go ReceiveMessage(rb.Out())

	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt, os.Kill)
	<-c
	log.Fatal("program interrupted")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RingBuf

type RingBuf[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](size int) *RingBuf[T]

func NewInfinity

func NewInfinity[T any]() *RingBuf[T]

func NewOverflow

func NewOverflow[T any](size int) *RingBuf[T]

func (*RingBuf[T]) In

func (ch *RingBuf[T]) In() chan<- T

func (*RingBuf[T]) Out

func (ch *RingBuf[T]) Out() <-chan T

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL