goqueue

package module
v0.0.0-...-b40e496 Latest Latest
Warning

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

Go to latest
Published: May 9, 2018 License: MIT Imports: 4 Imported by: 0

README

A GoRoutine safe queue for Golang Build Status GoDoc

It is similar to the Queue of Python.

Installation

go get github.com/damnever/goqueue

Example

Just for example, I use Queue.Get(0) and Queue.PutNoWait(value) more and often, but channel is not a right way do that...

package main

import (
    "fmt"
    "sync"

    "github.com/Damnever/goqueue"
)

func main() {
    queue := goqueue.New(0)
    wg := &sync.WaitGroup{}

    worker := func(queue *goqueue.Queue) {
        defer wg.Done()
        for !queue.IsEmpty() {
            val, err := queue.Get(0)
            if err != nil {
                fmt.Println("Unexpect Error: %v\n", err)
            }
            num := val.(int)
            fmt.Printf("-> %v\n", num)
            if num%3 == 0 {
                for i := num + 1; i < num+3; i++ {
                    queue.PutNoWait(i)
                }
            }
        }
    }

    go func() {
        defer wg.Done()
        for i := 0; i <= 27; i += 3 {
            queue.PutNoWait(i)
        }
    }()

    for i := 0; i < 5; i++ {
        go worker(queue)
    }

    wg.Add(6)
    wg.Wait()

    fmt.Println("All task done!!!")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyQueue is returned when queue is empty.
	ErrEmptyQueue = errors.New("queue is empty")
	// ErrFullQueue is returned when queue is full.
	ErrFullQueue = errors.New("queue is full")
)

Functions

This section is empty.

Types

type Queue

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

Queue is data structure, which has much similar behavior with channel.

func New

func New(maxSize int) *Queue

New create a new Queue, The maxSize variable sets the max Queue size. If maxSize is zero, Queue will be infinite size, and Put always no wait.

func (*Queue) Get

func (q *Queue) Get(timeout time.Duration) (interface{}, error)

Get gets an element from Queue. If timeout less than 0, If Queue is empty, return (nil, ErrEmptyQueue); If timeout equals to 0, block until get a value from Queue; If timeout greater than 0, wait timeout seconds until get a value from Queue, if timeout passed, return (nil, ErrEmptyQueue).

func (*Queue) GetNoWait

func (q *Queue) GetNoWait() (interface{}, error)

GetNoWait returns immediately, same as Get(-1).

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

IsEmpty returns true if Queue is empty.

func (*Queue) IsFull

func (q *Queue) IsFull() bool

IsFull returns true if Queue is full, always returns false if maxSize is 0.

func (*Queue) Put

func (q *Queue) Put(val interface{}, timeout time.Duration) error

Put puts an element into Queue. If timeout less than 0, If Queue is full, return (nil, ErrFullQueue); If timeout equals to 0, block until put a value into Queue; If timeout greater than 0, wait timeout seconds until put a value into Queue, if timeout passed, return (nil, ErrFullQueue).

func (*Queue) PutNoWait

func (q *Queue) PutNoWait(val interface{}) error

PutNoWait puts an element into Queue immediately, same as Put(-1).

func (*Queue) Size

func (q *Queue) Size() int

Size returns the size of Queue.

Jump to

Keyboard shortcuts

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