• Stars
    star
    492
  • Rank 89,476 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

golang worker pool , Concurrency limiting goroutine pool

Build Status Go Report Card codecov GoDoc Mentioned in Awesome Go

golang worker pool

  • Concurrency limiting goroutine pool.
  • Limits the concurrency of task execution, not the number of tasks queued.
  • Never blocks submitting tasks, no matter how many tasks are queued.
  • Support timeout
  • Support through security queues queue

Installation

The simplest way to install the library is to run:

$ go get github.com/xxjwxc/gowp

Support the maximum number of tasks, put them in the workpool and wait for them to be completed

Example

package main

import (
	"fmt"
	"time"

	"github.com/xxjwxc/gowp/workpool"
)

func main() {
	wp := workpool.New(10)     // Set the maximum number of threads
	for i := 0; i < 20; i++ { // Open 20 requests 
		ii := i
		wp.Do(func() error {
			for j := 0; j < 10; j++ { // 0-10 values per print
				fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
				time.Sleep(1 * time.Second)
			}
			//time.Sleep(1 * time.Second)
			return nil
		})
	}

	wp.Wait()
	fmt.Println("down")
}

Support for error return

package main

import (
	"fmt"
	"time"

	"github.com/xxjwxc/gowp/workpool"
)

func main() {
	wp := workpool.New(10)             // Set the maximum number of threads
	for i := 0; i < 20; i++ { 
		ii := i
		wp.Do(func() error {
			for j := 0; j < 10; j++ { // 0-10 values per print
				fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
				if ii == 1 {
					return errors.Cause(errors.New("my test err")) // have err return
				}
				time.Sleep(1 * time.Second)
			}

			return nil
		})
	}

	err := wp.Wait()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("down")
	}

Supporting judgement of completion (non-blocking)

package main

import (
	"fmt"
	"time"

	"github.com/xxjwxc/gowp/workpool"
)

func main() {
	wp := workpool.New(5)              // Set the maximum number of threads
	for i := 0; i < 10; i++ { 
		//	ii := i
		wp.Do(func() error {
			for j := 0; j < 5; j++ { 
				//fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
				time.Sleep(1 * time.Second)
			}
			return nil
		})

		fmt.Println(wp.IsDone())
	}
	wp.Wait()
	fmt.Println(wp.IsDone())
	fmt.Println("down")
}

Support synchronous waiting for results

package main

import (
	"fmt"
	"time"

	"github.com/xxjwxc/gowp/workpool"
)

func main() {
	wp := workpool.New(5) // Set the maximum number of threads
	for i := 0; i < 10; i++ { 
		ii := i
		wp.DoWait(func() error {
			for j := 0; j < 5; j++ { 
				fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
				// if ii == 1 {
				// 	return errors.New("my test err")
				// }
				time.Sleep(1 * time.Second)
			}

			return nil
			//time.Sleep(1 * time.Second)
			//return errors.New("my test err")
		})
	}

	err := wp.Wait()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("down")
}

Support timeout exit

package main

import (
	"fmt"
	"time"
	"time"
	"github.com/xxjwxc/gowp/workpool"
)

func main() {
	wp := workpool.New(5)              // Set the maximum number of threads
		wp.SetTimeout(time.Millisecond) // set max timeout
	for i := 0; i < 10; i++ { 
		ii := i
		wp.DoWait(func() error {
			for j := 0; j < 5; j++ {
				fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
				time.Sleep(1 * time.Second)
			}

			return nil
		})
	}

	err := wp.Wait()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("down")
}

limiter(cache)

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/xxjwxc/gowp/limiter"
)

func main() {
	limiter := limiter.NewLimiter(limiter.WithLimit(10), limiter.WithNamespace("test"), limiter.WithTsTimeout(true) /*, limiter.WithRedis(res)*/)
	var wg sync.WaitGroup
	for i := 0; i < 20; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			token, _ := limiter.Acquire(10) // get
			fmt.Println(token)

			time.Sleep(1 * time.Second)
			limiter.Release(token) 
		}()
	}
	wg.Wait()
	fmt.Println("down")
}

limiter(redis)

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/xxjwxc/gowp/limiter"
	"github.com/xxjwxc/public/myredis"
)

func main() {
	conf := myredis.InitRedis(myredis.WithAddr("127.0.0.1:6379"), myredis.WithPwd("123456"), myredis.WithGroupName("test"))
	res, err := myredis.NewRedis(conf)
	if err != nil {
		fmt.Println(err)
		return
	}

	limiter := limiter.NewLimiter(limiter.WithRedis(res), limiter.WithLimit(10), limiter.WithNamespace("test") /*, limiter.WithRedis(res)*/)
	var wg sync.WaitGroup
	for i := 0; i < 20; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			token, _ := limiter.Acquire(10) 
			fmt.Println(token)

			time.Sleep(1 * time.Second)
			limiter.Release(token) 
		}()
	}
	wg.Wait()
	fmt.Println("down")
}

More Repositories

1

uber_go_guide_cn

Uber Go 语言编码规范中文版. The Uber Go Style Guide .
7,558
star
2

gormt

database to golang struct
Go
2,347
star
3

caoguo

golang,微信小程序,电商系统
Vue
617
star
4

shares

A-share quantitative system. A股量化系统
PLpgSQL
474
star
5

ginrpc

gin auto binding,grpc, and annotated route,gin 注解路由, grpc,自动参数绑定工具
Go
289
star
6

public

util toolkit for go.golang 通用函数包
Go
171
star
7

gohanlp

Golang RESTful Client for HanLP.中文分词 词性标注 命名实体识别 依存句法分析 语义依存分析 新词发现 关键词短语提取 自动摘要 文本分类聚类 拼音简繁转换 自然语言处理
Go
36
star
8

esLog

elasticsearch log golang 的elasticsearch 日志封装,包括搜索,查询,添加等
Go
20
star
9

gofal

fractional api base on golang . golang math tools fractional molecular denominator 分数计算 分子 分母 运算
Go
18
star
10

xxjServer

C++ general tools,Cross-platform Tool Library
C
12
star
11

RTMP

rtmp online, 直播
C
11
star
12

openai

openai interface on golang
Go
11
star
13

PoetryRhyme

Poetry and rhyme,诗古,诗,诗歌
10
star
14

go-service

golang service,golang 服务创建库,跨平台支持,golang 系统服务注册库
Go
10
star
15

consult

golang consul tools
Go
7
star
16

GoSparkApi

golang 讯飞星火大模型 Go SparkApi
Go
6
star
17

xxjwxc.github.io

个人博客地址,欢迎访问
HTML
6
star
18

oauth2

golang oauth2 authorization tools , oauth2通用授权系统
C
6
star
19

SQLserverCppConnection

SQLserver Connection for cpp ,c++ sqlserver 链接库
C++
5
star
20

jump

remote monitoring . 远程监控工具
Go
5
star
21

ginrest

restful api base on go gin
Go
4
star
22

gomp3

mp3 decode mp3编解码器 golang版本 (pcm wav mp3)
C
3
star
23

douyin

golang 抖音弹幕消息
HTML
3
star
24

xxjwxc

1
star
25

gocharts

golang echarts tools
Go
1
star