rego

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 4 Imported by: 1

README

🍦 Rego

Go Doc License Coverage Test

Rego 用于复用一些特定的资源,比如说网络连接。

Read me in English

🍭 功能特性
  • 复用资源,支持限制数量
  • 支持自定义错误
  • 支持查询运行指标
  • 支持 context 超时机制

历史版本的特性请查看 HISTORY.md。未来版本的新特性和计划请查看 FUTURE.md

💡 使用方式
$ go get -u github.com/FishGoddess/rego
package main

import (
	"context"
	"fmt"
	"net"

	"github.com/FishGoddess/rego"
)

// acquireConn acquires a new conn, and returns an error if failed.
func acquireConn(ctx context.Context) (net.Conn, error) {
	// Guess this ip is from which websites?
	var dialer net.Dialer
	return dialer.DialContext(ctx, "tcp", "20.205.243.166:80")
}

// releaseConn releases the conn, and returns an error if failed.
func releaseConn(ctx context.Context, conn net.Conn) error {
	return conn.Close()
}

func main() {
	// Create a pool which type is net.Conn and limit is 64.
	ctx := context.Background()

	pool := rego.New(64, acquireConn, releaseConn)
	defer pool.Close(ctx)

	// Acquire a conn from pool.
	conn, err := pool.Acquire(ctx)
	if err != nil {
		panic(err)
	}

    // Remember releasing the conn after using.
	defer pool.Release(ctx, conn)

	// Use the conn
	fmt.Println(conn.RemoteAddr())
}
🚄 性能测试
$ make bench
goos: linux
goarch: amd64
cpu: Intel(R) Xeon(R) CPU E5-26xx v4

BenchmarkPool-2          6231790               186.8 ns/op             0 B/op          0 allocs/op

测试文件:_examples/pool_test.go

👥 贡献者

如果您觉得 rego 缺少您需要的功能,请不要犹豫,马上参与进来,发起一个 issue

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AcquireFunc

type AcquireFunc[Resource any] func(ctx context.Context) (Resource, error)

AcquireFunc is a function acquires a new resource and returns error if failed.

type AvailableFunc added in v0.4.1

type AvailableFunc[Resource any] func(ctx context.Context, resource Resource) bool

AvailableFunc is a function checks if a resource is available.

type Pool

type Pool[Resource any] struct {
	// contains filtered or unexported fields
}

Pool stores some resources and you can reuse them.

func New

func New[Resource any](limit uint64, acquire AcquireFunc[Resource], release ReleaseFunc[Resource]) *Pool[Resource]

New returns a new pool with limit resources. All resources are acquired by acquire function and released by release function.

func (*Pool[Resource]) Acquire added in v0.4.1

func (p *Pool[Resource]) Acquire(ctx context.Context) (resource Resource, err error)

Acquire acquires a resource from pool and returns an error if failed. You should call Pool.Release to return the resource back to the pool.

func (*Pool[Resource]) Close

func (p *Pool[Resource]) Close(ctx context.Context) error

Close closes pool and releases all resources.

func (*Pool[Resource]) Release added in v0.4.1

func (p *Pool[Resource]) Release(ctx context.Context, resource Resource) error

Release releases a resource to pool so we can reuse it next time.

func (*Pool[Resource]) Status

func (p *Pool[Resource]) Status() Status

Status returns the statistics of the pool.

func (*Pool[Resource]) WithAvailableFunc added in v0.4.1

func (p *Pool[Resource]) WithAvailableFunc(available AvailableFunc[Resource]) *Pool[Resource]

func (*Pool[Resource]) WithPoolClosedErrFunc added in v0.4.1

func (p *Pool[Resource]) WithPoolClosedErrFunc(newClosedErr PoolClosedErrFunc) *Pool[Resource]

type PoolClosedErrFunc added in v0.4.1

type PoolClosedErrFunc func(ctx context.Context) error

PoolClosedErrFunc is a function returns a pool closed error.

type ReleaseFunc

type ReleaseFunc[Resource any] func(ctx context.Context, resource Resource) error

ReleaseFunc is a function releases a resource and returns error if failed.

type Status

type Status struct {
	// Limit is the maximum quantity of resources in pool.
	Limit uint64 `json:"limit"`

	// Using is the quantity of using resources in pool.
	Using uint64 `json:"using"`

	// Idle is the quantity of idle resources in pool.
	Idle uint64 `json:"idle"`

	// Waiting is the quantity of caller waiting for a resource.
	Waiting uint64 `json:"waiting"`

	// WaitDuration is the average duration waiting a resource.
	WaitDuration time.Duration `json:"wait_duration"`
}

Status includes some statistics of the pool.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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