buffer

package module
v0.0.0-...-8d6394f Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: GPL-3.0 Imports: 1 Imported by: 0

README

Build Status

Ring Buffer

A simple thread safe ring buffer implementation that supports add and iterate operation. The position in ring buffer does not necessarily indicate data freshness at the tiem when data are added concurrently, but eventually would. An example of such scenario:

Assume we have following ring buffer

+----+----+----+----+----+----
| p0 | p1 | p2 | p3 | p4 |
+----+----+----+----+----+----

And the code is

r := goringbuffer.New(10)
go r.Add(100) // to p0 with old value 10
go r.Add(200) // to p1 with old value 20
sum := []int{0}
go r.Do(func(e interface{}){
   sum[0] += e.(int)
})

Even though the above code may settle 100 into p0 and 200 into p1 eventually, sum[0] may be 30 (10 + 20), 300 (100 + 200), 210 (10 + 200) or 120 (100 + 20).

Perf

Intel Core i5 2.3GHz

size = 16
BenchmarkAdd-8          50000000                36.1 ns/op
BenchmarkDo-8           20000000                104 ns/op
size = 64
BenchmarkAdd-8          30000000                36.7 ns/op
BenchmarkDo-8            5000000                322 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ring

type Ring struct {
	// contains filtered or unexported fields
}

Thread-safe ring buffer implementation NOTE: items in the buffer are not guaranteed to be available in insertion order (i.e. an item in an earlier ring position may become available after an later item). Eventual consistency should follow.

func New

func New(n int32) *Ring

func (*Ring) Add

func (r *Ring) Add(e interface{})

func (*Ring) Capacity

func (r *Ring) Capacity() int32

func (*Ring) Do

func (r *Ring) Do(f func(interface{}))

Jump to

Keyboard shortcuts

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