Code Monkey home page Code Monkey logo

go-ringbuf's Introduction

go-ringbuf [V2]

Go GitHub tag (latest SemVer) GoDoc FOSSA Status Go Report Card Coverage Status

go-ringbuf provides a high-performance, lock-free circular queue (ring buffer) implementation in golang.

MPMC (multiple-producers and multiple consumers) enabled.

History

v2.1.0

  • remove extras deps
    • use 'log/slog' instead 'hedzr/log', 'errors' instead 'hedzr/errors.v3'
  • remove WithLogger()

v2.0.+

security updates

v2.0.0 @20220408 - go 1.18+

generic version for MPMC Ring Buffer.

  • rewritten with go generics

v1.0.+

security updates

v1.0.0 @20220408

Last release for classical version.

Next release (v2) will move to go 1.18+ with generic enabled.

v0.9.1 @2022

  • review all codes
  • updated deps
  • review and solve uncertain misreport failed licenses
    • we have two deps: hedzr/errors and hedzr/log, and both them have no 3rd-party deps.
      • since 2.1.0, any deps removed.
    • we have no more 3rd-party deps.
    • we assumed a free license under MIT (unified).

Getting Start

go get -v github.com/hedzr/go-ringbuf/v2

Samples

package main

import (
	"fmt"
	"github.com/hedzr/go-ringbuf/v2"
	"log"
)

func main() {
	testIntRB()
	testStringRB()
}

func testStringRB() {
	var err error
	var rb = ringbuf.New[string](80)
	err = rb.Enqueue("abcde")
	errChk(err)

	var item string
	item, err = rb.Dequeue()
	errChk(err)
	fmt.Printf("dequeue ok: %v\n", item)
}

func testIntRB() {
	var err error
	var rb = ringbuf.New[int](80)
	err = rb.Enqueue(3)
	errChk(err)

	var item int
	item, err = rb.Dequeue()
	errChk(err)
	fmt.Printf("dequeue ok: %v\n", item)
}

func errChk(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

Using Ring-Buffer as a fixed resource pool

The following codes is for v1, needed for rewriting

func newRes() *Res{...}

var rb fast.RingBuffer

func initFunc() (err error) {
  const maxSize = 16
  
  if rb = fast.New(uint32(maxSize)); rb == nil {
		err = errors.New("cannot create fast.RingBuffer")
		return
	}

    // CapReal() will be available since v0.8.8, or replace it with Cap() - 1
	for i := uint32(0); i < rb.CapReal(); i++ {
		if err = rb.Enqueue(newRes()); err != nil {
			return
		}
	}
}

func loopFor() {
  var err error
  for {
    it, err := rb.Dequeue()
    checkErr(err)
    if res, ok := it.(*Res); ok {
      // do stuff with `res`, and put it back into ring-buffer
      err = rb.Enqueue(it)
    }
  }
}

Contrib

Welcome

LICENSE

Apache 2.0

go-ringbuf's People

Contributors

dependabot[bot] avatar fossabot avatar hedzr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-ringbuf's Issues

ringBuffer卡住

func TestRbQueue(t *testing.T) {
	size := roundUpToPower2(8)
	q := &ringBuf{
		data:       make([]rbItem, size),
		head:       0,
		tail:       0,
		cap:        size,
		capModMask: size - 1,
	}
	
	var (
		w sync.WaitGroup
		get uint32
		put uint32
		num = 4
		jump = 100000
	)
	
	w.Add(num)
	go func() {
		for i := 0; i < num; i++ {
			go func() {
				defer w.Done()
				
				var val int
				for {
					e := q.Put(val)
					for e != nil {
						time.Sleep(time.Millisecond)
						e = q.Put(val)
					}
					atomic.AddUint32(&put, 1)
					val++
					if val == jump {
						break
					}
				}
			}()
		}
	}()
	
	w.Add(num)
	go func() {
		for i := 0; i < num; i++ {
			go func() {
				defer w.Done()
				
				var val int
				for {
					_, e := q.Get()
					for e != nil {
						time.Sleep(time.Millisecond)
						_,e = q.Get()
					}
					atomic.AddUint32(&get, 1)
					val++
					if val == jump {
						break
					}
				}
			}()
		}
	}()
	
	go func() {
		ticker := time.NewTicker(3 * time.Second)
		defer ticker.Stop()
		
		for {
			<-ticker.C
			
			fmt.Printf("put: %d; get: %d; full: %t; empty: %t; size: %d; head: %d; tail: %d \n", 
				atomic.LoadUint32(&put), atomic.LoadUint32(&get), q.IsFull(), q.IsEmpty(),
				q.Size(), q.head, q.tail)
			
			for i := range q.data {
				fmt.Printf("status: %d; value: %v \n", q.data[i].readWrite, q.data[i].value)
			}
		}
	}()
	
	w.Wait()
}
put: 2818; get: 2817; full: false; empty: true; size: 0; head: 1; tail: 1 
status: 0; value: 789 
status: 1; value: 788 
status: 0; value: 709 
status: 0; value: 710 
status: 0; value: 711 
status: 0; value: 712 
status: 0; value: 713 
status: 0; value: 714 
put: 2818; get: 2817; full: false; empty: true; size: 0; head: 1; tail: 1 
status: 0; value: 789 
status: 1; value: 788 
status: 0; value: 709 
status: 0; value: 710 
status: 0; value: 711 
status: 0; value: 712 
status: 0; value: 713 
status: 0; value: 714

tail对应元素,写入成功的,但tail未向下移动,导致一直卡死。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.