queue

package
v0.0.0-...-4e8bfbf Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2020 License: MIT Imports: 4 Imported by: 0

README

queue

A task queue for mitigating server pressure in high concurrency situations and improving task processing.

Build Codecov ReportCard GoDoc License

Get

go get -u -v github.com/LyricTian/queue

Usage

package main

import (
	"fmt"

	"github.com/LyricTian/queue"
)

func main() {
	q := queue.NewQueue(10, 100)
	q.Run()

	defer q.Terminate()

	job := queue.NewJob("hello", func(v interface{}) {
		fmt.Printf("%s,world \n", v)
	})
	q.Push(job)

	// output: hello,world
}

MIT License

    Copyright (c) 2017 Lyric

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Push

func Push(job Jober)

Push put the executable task into the queue

func Run

func Run(maxCapacity, maxThread int)

Run start running queues, specify the number of buffers, and the number of worker threads

func RunListQueue

func RunListQueue(maxThread int)

RunListQueue start running list queues ,specify the number of worker threads

func Terminate

func Terminate()

Terminate terminate the queue to receive the task and release the resource

Types

type Jober

type Jober interface {
	Job()
}

Jober an asynchronous task that can be executed

func NewJob

func NewJob(v interface{}, fn func(interface{})) Jober

NewJob create an asynchronous task

type ListQueue

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

ListQueue a list task queue for mitigating server pressure in high concurrency situations and improving task processing

Example
q := NewListQueue(10)
q.Run()

var count int64
for i := 0; i < 10; i++ {
	job := NewJob("foo", func(v interface{}) {
		atomic.AddInt64(&count, 1)
	})
	q.Push(job)
}

q.Terminate()
fmt.Println(count)
Output:

10

func NewListQueue

func NewListQueue(maxThread int) *ListQueue

NewListQueue create a list queue that specifies the number of worker threads

func NewListQueueWithMaxLen

func NewListQueueWithMaxLen(maxThread, maxLen int) *ListQueue

NewListQueueWithMaxLen create a list queue that specifies the number of worker threads and the maximum number of elements

func (*ListQueue) Push

func (q *ListQueue) Push(job Jober)

Push put the executable task into the queue

func (*ListQueue) Run

func (q *ListQueue) Run()

Run start running queues

func (*ListQueue) Terminate

func (q *ListQueue) Terminate()

Terminate terminate the queue to receive the task and release the resource

type Queue

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

Queue a task queue for mitigating server pressure in high concurrency situations and improving task processing

Example
q := NewQueue(1, 10)
q.Run()

var count int64

for i := 0; i < 10; i++ {
	job := NewJob("foo", func(v interface{}) {
		atomic.AddInt64(&count, 1)
	})
	q.Push(job)
}

q.Terminate()
fmt.Println(count)
Output:

10

func NewQueue

func NewQueue(maxCapacity, maxThread int) *Queue

NewQueue create a queue that specifies the number of buffers and the number of worker threads

func (*Queue) Push

func (q *Queue) Push(job Jober)

Push put the executable task into the queue

func (*Queue) Run

func (q *Queue) Run()

Run start running queues

func (*Queue) Terminate

func (q *Queue) Terminate()

Terminate terminate the queue to receive the task and release the resource

type Queuer

type Queuer interface {
	Run()
	Push(job Jober)
	Terminate()
}

Queuer a task queue for mitigating server pressure in high concurrency situations and improving task processing

type SyncJober

type SyncJober interface {
	Jober
	Wait() <-chan interface{}
	Error() error
}

SyncJober a synchronization task that can be executed

func NewSyncJob

func NewSyncJob(v interface{}, fn func(interface{}) (interface{}, error)) SyncJober

NewSyncJob create a synchronization task

Jump to

Keyboard shortcuts

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