Skip to content

用于复用一些特定的资源,比如说网络连接 - for reusing some resources like network connections

License

Notifications You must be signed in to change notification settings

FishGoddess/rego

Repository files navigation

🍦 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

About

用于复用一些特定的资源,比如说网络连接 - for reusing some resources like network connections

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published