Logo

0x3d.Site

is designed for aggregating information.
Welcome
check repository here

Build Status Go Report Card codecov GoDoc Mentioned in Awesome Go Featured|HelloGitHub

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")
}

Golang
Golang
Golang, or Go, is a statically typed programming language developed by Google. It’s known for its simplicity, performance, and support for concurrent programming. Go is widely used in cloud computing, server-side applications, and microservices.
GitHub - goyek/goyek: Task automation Go library
GitHub - goyek/goyek: Task automation Go library
gommon/color at master · labstack/gommon
gommon/color at master · labstack/gommon
GitHub - blevesearch/bleve: A modern text/numeric/geo-spatial/vector indexing library for go
GitHub - blevesearch/bleve: A modern text/numeric/geo-spatial/vector indexing library for go
GitHub - schollz/progressbar: A really basic thread-safe progress bar for Golang applications
GitHub - schollz/progressbar: A really basic thread-safe progress bar for Golang applications
GitHub - free/concurrent-writer: Highly concurrent drop-in replacement for bufio.Writer
GitHub - free/concurrent-writer: Highly concurrent drop-in replacement for bufio.Writer
GitHub - mattn/go-colorable
GitHub - mattn/go-colorable
GitHub - StudioSol/set: A simple Set data structure implementation in Go (Golang) using LinkedHashMap.
GitHub - StudioSol/set: A simple Set data structure implementation in Go (Golang) using LinkedHashMap.
GitHub - essentialkaos/branca: Authenticated encrypted API tokens (IETF XChaCha20-Poly1305 AEAD) for Golang
GitHub - essentialkaos/branca: Authenticated encrypted API tokens (IETF XChaCha20-Poly1305 AEAD) for Golang
Cloud Native Computing Foundation
Cloud Native Computing Foundation
GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.
GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.
GitHub - heetch/confita: Load configuration in cascade from multiple backends into a struct
GitHub - heetch/confita: Load configuration in cascade from multiple backends into a struct
GitHub - brianvoe/sjwt: Simple JWT Golang
GitHub - brianvoe/sjwt: Simple JWT Golang
GitHub - subpop/go-ini: Go package that encodes and decodes INI-files
GitHub - subpop/go-ini: Go package that encodes and decodes INI-files
GitHub - DylanMeeus/GoAudio: Go tools for audio processing & creation 🎶
GitHub - DylanMeeus/GoAudio: Go tools for audio processing & creation 🎶
GitHub - daviddengcn/go-colortext: Change the color of console text.
GitHub - daviddengcn/go-colortext: Change the color of console text.
GitHub - andOTP/andOTP: [Unmaintained] Open source two-factor authentication for Android
GitHub - andOTP/andOTP: [Unmaintained] Open source two-factor authentication for Android
GitHub - alfiankan/crab-config-files-templating: Dynamic configuration file templating tool for kubernetes manifest or general configuration files
GitHub - alfiankan/crab-config-files-templating: Dynamic configuration file templating tool for kubernetes manifest or general configuration files
GitHub - viney-shih/go-cache: A flexible multi-layer Go caching library to deal with in-memory and shared cache by adopting Cache-Aside pattern.
GitHub - viney-shih/go-cache: A flexible multi-layer Go caching library to deal with in-memory and shared cache by adopting Cache-Aside pattern.
GitHub - TreyBastian/colourize: An ANSI colour terminal package for Go
GitHub - TreyBastian/colourize: An ANSI colour terminal package for Go
GitHub - Evertras/bubble-table: A customizable, interactive table component for the Bubble Tea framework
GitHub - Evertras/bubble-table: A customizable, interactive table component for the Bubble Tea framework
GitHub - deatil/go-array: A Go package that read or set data from map, slice or json
GitHub - deatil/go-array: A Go package that read or set data from map, slice or json
GitHub - cometbft/cometbft: CometBFT (fork of Tendermint Core): A distributed, Byzantine fault-tolerant, deterministic state machine replication engine
GitHub - cometbft/cometbft: CometBFT (fork of Tendermint Core): A distributed, Byzantine fault-tolerant, deterministic state machine replication engine
GitHub - icza/session: Go session management for web servers (including support for Google App Engine - GAE).
GitHub - icza/session: Go session management for web servers (including support for Google App Engine - GAE).
GitHub - cristalhq/jwt: Safe, simple and fast JSON Web Tokens for Go
GitHub - cristalhq/jwt: Safe, simple and fast JSON Web Tokens for Go
GitHub - lrita/cmap: a thread-safe concurrent map for go
GitHub - lrita/cmap: a thread-safe concurrent map for go
GitHub - wzshiming/ctc: Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method
GitHub - wzshiming/ctc: Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method
GitHub - goradd/maps: map library using Go generics that offers a standard interface, go routine synchronization, and sorting
GitHub - goradd/maps: map library using Go generics that offers a standard interface, go routine synchronization, and sorting
GitHub - alfiankan/teleterm: Telegram Bot Exec Terminal Command
GitHub - alfiankan/teleterm: Telegram Bot Exec Terminal Command
GitHub - JeremyLoy/config: 12 factor configuration as a typesafe struct in as little as two function calls
GitHub - JeremyLoy/config: 12 factor configuration as a typesafe struct in as little as two function calls
GitHub - goraz/onion: Layer based configuration for golang
GitHub - goraz/onion: Layer based configuration for golang
Golang
More on Golang

Programming Tips & Tricks

Code smarter, not harder—insider tips and tricks for developers.

Error Solutions

Turn frustration into progress—fix errors faster than ever.

Shortcuts

The art of speed—shortcuts to supercharge your workflow.
  1. Collections 😎
  2. Frequently Asked Question's 🤯

Tools

available to use.

Made with ❤️

to provide resources in various ares.