Skip to content

rrgmc/nbchanlist

Repository files navigation

nbchanlist - Non-blocking unbounded lock-free channel-based list for Golang

GoDoc

nbchanlist is a non-blocking unbounded lock-free channel-based list for Golang.

For getting items, a channel is provided, so it can be used with select, and allows for context cancellation, timeouts, and plays nice with other code using channels.

As it is unbounded, care must be taken to avoid memory exhaustion if adding faster than reading.

It comes with a "queue" implementation, but other types of lists can be used by implementing the ListType interface.

Install

go get github.com/rrgmc/nbchanlist

Example

import (
    "fmt"
    "time"

    "github.com/rrgmc/nbchanlist"
)

func ExampleNewQueue() {
    q := nbchanlist.NewQueue[int]()
    q.Put(12) // never blocks
    q.Put(13)
    select {
    case v := <-q.Get():
        fmt.Println(v)
    case <-time.After(time.Second):
        fmt.Println("timeout")
    }
    q.Shutdown() // stops goroutine and close channels
    select {
    case _, ok := <-q.Get():
        if !ok {
            fmt.Println("queue is closed")
        } else {
            fmt.Println("should never happen")
        }
    }

    // Output:
    // 12
    // queue is closed
}

License

MIT

Author

Rangel Reale (rangelreale@gmail.com)

About

Non-blocking unbounded lock-free channel-based list for Golang

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages