rego

package module
v0.0.0-...-1bfb366 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

rego means recycle goroutines and reduce goroutines

Introduction

Library rego is a lightweight goroutine pool implementation that controls concurrent resources by reusing goroutines. Developers can significantly reduce memory growth in concurrent programs with minimal performance trade-offs.

Install

go get -u github.com/alilestera/rego

Quick Start

Instantiate with customize capacity

You need to call rego.New to instantiate a Rego with a given capacity:

r := rego.New(100)

Submit tasks

You can call r.Submit() submit tasks:

r.Submit(func() {})

Release

If you need to close Rego:

r.Release() // close Rego and ignore all waiting tasks

or

r.ReleaseWait() // close Rego and wait all tasks done

Example

package main

import (
	"fmt"

	"github.com/alilestera/rego"
)

func main() {
	r := rego.New(100)
	r.Submit(func() {
		fmt.Println("hello world")
	})
	r.ReleaseWait()
}

Configuration options

Option Default Description
WithMinWorkers 0 Set the minimum number of active workers
WithIdleTimeout 2 * time.Second Set the idle timeout of active worker

Example

r := rego.New(100, rego.WithMinWorkers(10), rego.WithIdleTimeout(5*time.Second))

Acknowledgments

This project was inspired by or benefited from the following repositories:

License

The source code in rego is licensed under Apache License 2.0.

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultMinWorkers is the default minimum number of workers to keep alive.
	DefaultMinWorkers = 0
	// DefaultIdleTimeout is the default timeout for idle workers.
	DefaultIdleTimeout = 2 * time.Second
)
View Source
const (
	// Opened represents that the Rego is opened.
	Opened = iota
	// Closed represents that the Rego is closed.
	Closed
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option represents the optional function.

func WithIdleTimeout

func WithIdleTimeout(idleTimeout time.Duration) Option

WithIdleTimeout sets the idle timeout.

func WithMinWorkers

func WithMinWorkers(minWorkers int) Option

WithMinWorkers sets the minimum number of workers.

type Rego

type Rego struct {
	Stop int
	// contains filtered or unexported fields
}

Rego is a goroutine pool.

func New

func New(size int, opts ...Option) *Rego

New instantiates a Rego with customized options. The size parameter specifies the capacity of Rego, which is the maximum number of workers that can execute tasks concurrently.

func (*Rego) Cap

func (r *Rego) Cap() int

Cap returns the capacity of Rego.

func (*Rego) IsClosed

func (r *Rego) IsClosed() bool

IsClosed returns whether the Rego is closed.

func (*Rego) MinWorkers

func (r *Rego) MinWorkers() int

MinWorkers returns the minimum number of workers that can keep alive.

func (*Rego) Release

func (r *Rego) Release()

Release closes the Rego but ignores all waiting tasks. Tasks should not be submitted after closing the Rego.

func (*Rego) ReleaseWait

func (r *Rego) ReleaseWait()

ReleaseWait closes the Rego and waits for all tasks to complete. Tasks should not be submitted after closing the Rego.

func (*Rego) Running

func (r *Rego) Running() int

Running returns the number of currently running workers.

func (*Rego) Submit

func (r *Rego) Submit(task func())

Submit submits a task to the Rego.

Example
package main

import (
	"fmt"

	"github.com/alilestera/rego"
)

func main() {
	r := rego.New(3)
	defer r.ReleaseWait()
	requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}
	for _, v := range requests {
		r.Submit(func() {
			fmt.Println(v)
		})
	}
}
Output:
alpha
beta
gamma
delta
epsilon

func (*Rego) SubmitWait

func (r *Rego) SubmitWait(task func())

SubmitWait submits a task to the Rego and waits for it to complete.

Example
package main

import (
	"fmt"

	"github.com/alilestera/rego"
)

func main() {
	r := rego.New(3)
	defer r.ReleaseWait()
	requests := []string{"foo", "bar", "foobar", "anything", "else"}
	for _, v := range requests {
		r.SubmitWait(func() {
			fmt.Println(v)
		})
	}
}
Output:
foo
bar
foobar
anything
else

func (*Rego) Waiting

func (r *Rego) Waiting() int

Waiting returns the number of tasks waiting to be executed.

Jump to

Keyboard shortcuts

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