Categorygithub.com/major1201/go-concurrency
modulepackage
0.1.1
Repository: https://github.com/major1201/go-concurrency.git
Documentation: pkg.go.dev

# 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

# Functions

NewJobGroup returns a JobGroup.
NewPayload returns a payload with the job function.

# Variables

ErrAlreadyQuit indicates the job group is quit.
ErrBacklogIsNotEnough indicates the backlog is not enough.

# Structs

JobGroup job group.
Payload the job exactly be run.