concurrency

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2020 License: MIT Imports: 7 Imported by: 0

README

go-concurrency

A useful tool to limit goroutine numbers, timeout etc.

GoDoc Go Report Card

How to use

Install go-concurrency to your package
go get github.com/major1201/go-concurrency
Go with Simple payload
// create a new job group with 2 concurrent worker and the backlog size is 1
jg := concurrency.NewJobGroup(2, 1)
// start the job group in background
go jg.Start()

// create 5 sub-jobs
for i := 0; i < 5; i++ {
    n := i
    if err := jg.Go(func() {
        fmt.Printf("start job %d\n", n)
        time.Sleep(5 * time.Second)
        fmt.Printf("end job %d\n", n)
    }); err != nil {
        fmt.Printf("add job %d error, %v\n", n, err)
    }
}

// the job group would no longer accept new jobs and would do and wait until all the jobs in backlog is done
jg.StopWait()

output:

add job 3 error, concurrency backlog is full
add job 4 error, concurrency backlog is full
start job 1
start job 0
end job 0
end job 1
start job 2
end job 2
Weighted payload

The job can be weighted, if your job is really important, you can set your job with weight

jg.GoWithWeight(3, func() {
    // do stuff
})
Payload with timeout
// a closable resource
httpConn := ....

// create a custom payload
payload := concurrency.NewPayload(func() {
    // do stuff
})

// set the payload with timeout and cancel function
payload.SetTimeout(3 * time.Second, func() {
    httpConn.Close()
})

// go!
jg.GoWithPayload(payload)

caution: set a job with timeout DOES NOT make sure the job to cancel, the cancel function just be called after timeout

Contributing

Just fork the repository and open a pull request with your changes.

Licence

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBacklogIsNotEnough indicates the backlog is not enough
	ErrBacklogIsNotEnough = errors.New("concurrency backlog is not enough")
	// ErrAlreadyQuit indicates the job group is quit
	ErrAlreadyQuit = errors.New("jobGroup is ready quit")
)

Functions

This section is empty.

Types

type JobGroup

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

JobGroup job group

func NewJobGroup

func NewJobGroup(workerSize, backlogSize int) *JobGroup

NewJobGroup returns a JobGroup

func (*JobGroup) Abort

func (jg *JobGroup) Abort()

Abort the job group and returns at once, however the workers would still do its jobs

func (*JobGroup) AbortWait

func (jg *JobGroup) AbortWait()

AbortWait the job group and wait until the workers is done

func (*JobGroup) Go

func (jg *JobGroup) Go(f func()) error

Go commits a job to the backlog

func (*JobGroup) GoWithPayload

func (jg *JobGroup) GoWithPayload(p *Payload) error

GoWithPayload commits a raw payload

func (*JobGroup) GoWithWeight

func (jg *JobGroup) GoWithWeight(weight int, f func()) error

GoWithWeight commits a job to the backlog with weight

func (*JobGroup) Start

func (jg *JobGroup) Start()

Start to consume the jobs

func (*JobGroup) StopWait

func (jg *JobGroup) StopWait()

StopWait the job group would no longer accept new jobs and would do and wait until all the jobs in backlog is done

type Payload

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

Payload the job exactly be run

func NewPayload

func NewPayload(f func()) *Payload

NewPayload returns a payload with the job function

func (*Payload) SetTimeout

func (p *Payload) SetTimeout(timeout time.Duration, cancel func())

SetTimeout set the payload timeout and che cancel function,

caution: set a job with timeout DOES NOT make sure the job to cancel, the cancel function just be called after timeout

func (*Payload) SetWeight

func (p *Payload) SetWeight(weight int)

SetWeight set the payload weight

Jump to

Keyboard shortcuts

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