Skip to content

一个简单的资源池库,用于控制、复用一些特定的资源,比如说网络连接 - A resource pool library which is used for controlling and reusing some resources like network connection

License

FishGoddess/rego

Repository files navigation

🍦 Rego

Go Doc License Coverage Test

Rego 是一个简单的资源池库,用于控制、复用一些特定的资源,比如说网络连接。

Read me in English

🍭 功能特性

  • 简单的资源池,可以控制资源数量

历史版本的特性请查看 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() (net.Conn, error) {
	// Guess this ip is from which websites?
	return net.Dial("tcp", "20.205.243.166:80")
}

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

func main() {
	// Create a resource pool which type is net.Conn and limit is 64.
	pool := rego.New[net.Conn](acquireConn, releaseConn, rego.WithLimit(64))
	defer pool.Close()

	// Take a resource from pool.
	conn, err := pool.Take(context.Background())
	if err != nil {
		panic(err)
	}

	// Remember put the client to pool when your using is done.
	// This is why we call the resource in pool is reusable.
	// We recommend you to do this job in a defer function.
	defer pool.Put(conn)

	// Use the conn
	fmt.Println(conn.RemoteAddr())
}

👥 贡献者

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

About

一个简单的资源池库,用于控制、复用一些特定的资源,比如说网络连接 - A resource pool library which is used for controlling and reusing some resources like network connection

Resources

License

Stars

Watchers

Forks

Packages

No packages published