Skip to content

Commit 0d94f76

Browse files
authored
Merge pull request #4 from FishGoddess/develop
v0.2.2
2 parents 43a0708 + c3701e1 commit 0d94f76

File tree

12 files changed

+153
-112
lines changed

12 files changed

+153
-112
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ on:
77

88
jobs:
99
test-project:
10-
runs-on: ubuntu-20.04
10+
runs-on: ubuntu-latest
1111
steps:
12+
- run: uname -a
13+
- run: lsb_release -a
14+
1215
- name: Setup
1316
uses: actions/setup-go@v4
1417
with:
1518
go-version: "1.23"
19+
1620
- run: go version
1721

1822
- name: Checkout

FUTURE.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
## ✈️ 未来版本的新特性 (Features in future versions)
1+
## ✈️ 未来版本的新特性
2+
3+
### v0.3.x
4+
5+
* [ ] 增加生命周期检测,资源检查回调
26

37
### v0.2.x
48

59
* [x] 优化代码
6-
* [ ] 增加生命周期检测,资源检查回调
10+
* [x] Acquire 函数支持 context 的透传
11+
* [x] Release 函数支持 context 的透传
12+
* [x] 调整错误类型,调整错误处理选项函数
713

814
### v0.1.x
915

HISTORY.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
## 📜 历史版本的特性介绍 (Features in old versions)
1+
## 📜 历史版本的特性介绍
2+
3+
### v0.2.2
4+
5+
> 此版本发布于 2025-04-29
6+
7+
* Acquire 函数支持 context 的透传
8+
* Release 函数支持 context 的透传
9+
* 调整错误类型,调整错误处理选项函数
210

311
### v0.2.1
412

README.en.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
[![Coverage](_icons/coverage.svg)](_icons/coverage.svg)
66
![Test](https://github.com/FishGoddess/rego/actions/workflows/test.yml/badge.svg)
77

8-
**Rego** is a resource pool library which is used for controlling and reusing some resources like network connection.
8+
**Rego** is a resource pool used for reusing some resources like network connections.
99

1010
[阅读中文版的文档](./README.md)
1111

1212
### 🍭 Features
1313

14-
* Based resource pool, which can limit the count of resources.
14+
* Reuse resources by limiting the quantity of resources
15+
* Error handling callback for different errors
16+
* Check pool status like acquired and idle quantity
17+
* Passing context to callbacks
1518

1619
_Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about more information._
1720

@@ -33,31 +36,34 @@ import (
3336
)
3437

3538
// acquireConn acquires a new conn, and returns an error if failed.
36-
func acquireConn() (net.Conn, error) {
39+
func acquireConn(ctx context.Context) (net.Conn, error) {
3740
// Guess this ip is from which websites?
38-
return net.Dial("tcp", "20.205.243.166:80")
41+
var dialer net.Dialer
42+
return dialer.DialContext(ctx, "tcp", "20.205.243.166:80")
3943
}
4044

4145
// releaseConn releases the given conn, and returns an error if failed.
42-
func releaseConn(conn net.Conn) error {
46+
func releaseConn(ctx context.Context, conn net.Conn) error {
4347
return conn.Close()
4448
}
4549

4650
func main() {
4751
// Create a resource pool which type is net.Conn and limit is 64.
52+
ctx := context.Background()
53+
4854
pool := rego.New(64, acquireConn, releaseConn)
49-
defer pool.Close()
55+
defer pool.Close(ctx)
5056

5157
// Take a resource from pool.
52-
conn, err := pool.Take(context.Background())
58+
conn, err := pool.Take(ctx)
5359
if err != nil {
5460
panic(err)
5561
}
5662

5763
// Remember put the client to pool when your using is done.
5864
// This is why we call the resource in pool is reusable.
5965
// We recommend you to do this job in a defer function.
60-
defer pool.Put(conn)
66+
defer pool.Put(ctx, conn)
6167

6268
// Use the conn
6369
fmt.Println(conn.RemoteAddr())

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
[![Coverage](_icons/coverage.svg)](_icons/coverage.svg)
66
![Test](https://github.com/FishGoddess/rego/actions/workflows/test.yml/badge.svg)
77

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

1010
[Read me in English](./README.en.md)
1111

1212
### 🍭 功能特性
1313

14-
* 简单的资源池,可以控制资源数量
14+
* 简单复用资源,支持限制数量
15+
* 支持错误处理回调,用于自定义业务方的特定错误
16+
* 支持查询资源池的运行情况,比如已获取和空闲的资源数量
17+
* 回调函数支持 context 的透传
1518

1619
_历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新特性和计划请查看 [FUTURE.md](./FUTURE.md)_
1720

@@ -33,31 +36,34 @@ import (
3336
)
3437

3538
// acquireConn acquires a new conn, and returns an error if failed.
36-
func acquireConn() (net.Conn, error) {
39+
func acquireConn(ctx context.Context) (net.Conn, error) {
3740
// Guess this ip is from which websites?
38-
return net.Dial("tcp", "20.205.243.166:80")
41+
var dialer net.Dialer
42+
return dialer.DialContext(ctx, "tcp", "20.205.243.166:80")
3943
}
4044

4145
// releaseConn releases the given conn, and returns an error if failed.
42-
func releaseConn(conn net.Conn) error {
46+
func releaseConn(ctx context.Context, conn net.Conn) error {
4347
return conn.Close()
4448
}
4549

4650
func main() {
4751
// Create a resource pool which type is net.Conn and limit is 64.
52+
ctx := context.Background()
53+
4854
pool := rego.New(64, acquireConn, releaseConn)
49-
defer pool.Close()
55+
defer pool.Close(ctx)
5056

5157
// Take a resource from pool.
52-
conn, err := pool.Take(context.Background())
58+
conn, err := pool.Take(ctx)
5359
if err != nil {
5460
panic(err)
5561
}
5662

5763
// Remember put the client to pool when your using is done.
5864
// This is why we call the resource in pool is reusable.
5965
// We recommend you to do this job in a defer function.
60-
defer pool.Put(conn)
66+
defer pool.Put(ctx, conn)
6167

6268
// Use the conn
6369
fmt.Println(conn.RemoteAddr())

_examples/basic.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,34 @@ import (
1313
)
1414

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

2122
// releaseConn releases the given conn, and returns an error if failed.
22-
func releaseConn(conn net.Conn) error {
23+
func releaseConn(ctx context.Context, conn net.Conn) error {
2324
return conn.Close()
2425
}
2526

2627
func main() {
2728
// Create a resource pool which type is net.Conn and limit is 64.
29+
ctx := context.Background()
30+
2831
pool := rego.New(64, acquireConn, releaseConn)
29-
defer pool.Close()
32+
defer pool.Close(ctx)
3033

3134
// Take a resource from pool.
32-
conn, err := pool.Take(context.Background())
35+
conn, err := pool.Take(ctx)
3336
if err != nil {
3437
panic(err)
3538
}
3639

3740
// Remember put the client to pool when your using is done.
3841
// This is why we call the resource in pool is reusable.
3942
// We recommend you to do this job in a defer function.
40-
defer pool.Put(conn)
43+
defer pool.Put(ctx, conn)
4144

4245
// Use the conn
4346
fmt.Println(conn.RemoteAddr())

_examples/errors.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ import (
1313
)
1414

1515
var (
16-
errPoolIsFull = errors.New("_examples: pool is full")
17-
errPoolIsClosed = errors.New("_examples: pool is closed")
16+
errPoolExhausted = errors.New("_examples: pool is exhausted")
17+
errPoolClosed = errors.New("_examples: pool is closed")
1818
)
1919

20-
func acquire() (int, error) {
20+
func acquire(ctx context.Context) (int, error) {
2121
return 0, nil
2222
}
2323

24-
func release(resource int) error {
24+
func release(ctx context.Context, resource int) error {
2525
return nil
2626
}
2727

28-
func newPoolFullErr(ctx context.Context) error {
29-
return errPoolIsFull
28+
func newPoolExhaustedErr(ctx context.Context) error {
29+
return errPoolExhausted
3030
}
3131

3232
func newPoolClosedErr(ctx context.Context) error {
33-
return errPoolIsClosed
33+
return errPoolClosed
3434
}
3535

3636
func main() {
@@ -42,36 +42,36 @@ func main() {
4242
resource, err := pool.Take(ctx)
4343
fmt.Println(resource, err)
4444

45-
// However, the pool is full after taking one resource without putting.
46-
// It will return a full error.
45+
// However, the pool is exhausted after taking one resource without putting.
46+
// It will return an exhausted error.
4747
resource, err = pool.Take(ctx)
48-
fmt.Println(resource, err, err == rego.ErrPoolIsFull)
48+
fmt.Println(resource, err, err == rego.ErrPoolExhausted)
4949

5050
// Put the resource back to the pool.
51-
pool.Put(resource)
52-
pool.Close()
51+
pool.Put(ctx, resource)
52+
pool.Close(ctx)
5353

5454
// Now, the pool is closed so any taking from the pool will return a closed error.
5555
resource, err = pool.Take(ctx)
56-
fmt.Println(resource, err, err == rego.ErrPoolIsClosed)
56+
fmt.Println(resource, err, err == rego.ErrPoolClosed)
5757

5858
// Create a pool with limit and fast-failed and new error funcs.
59-
pool = rego.New(1, acquire, release, rego.WithFastFailed(), rego.WithPoolFullErr(newPoolFullErr), rego.WithPoolClosedErr(newPoolClosedErr))
59+
pool = rego.New(1, acquire, release, rego.WithFastFailed(), rego.WithPoolExhaustedErr(newPoolExhaustedErr), rego.WithPoolClosedErr(newPoolClosedErr))
6060

6161
// Take one resource from pool which is ok.
6262
resource, err = pool.Take(ctx)
6363
fmt.Println(resource, err)
6464

65-
// However, the pool is full after taking one resource without putting.
66-
// It will return a customizing full error.
65+
// However, the pool is exhausted after taking one resource without putting.
66+
// It will return a customizing exhausted error.
6767
resource, err = pool.Take(ctx)
68-
fmt.Println(resource, err, err == errPoolIsFull)
68+
fmt.Println(resource, err, err == errPoolExhausted)
6969

7070
// Put the resource back to the pool.
71-
pool.Put(resource)
72-
pool.Close()
71+
pool.Put(ctx, resource)
72+
pool.Close(ctx)
7373

7474
// Now, the pool is closed so any taking from the pool will return a customizing closed error.
7575
resource, err = pool.Take(ctx)
76-
fmt.Println(resource, err, err == errPoolIsClosed)
76+
fmt.Println(resource, err, err == errPoolClosed)
7777
}

_examples/pool.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func runServer() {
2626
}
2727

2828
fmt.Println("server:", string(bs))
29+
time.Sleep(time.Second)
2930
})
3031

3132
if err := http.ListenAndServe("127.0.0.1:9876", nil); err != nil {
@@ -34,20 +35,21 @@ func runServer() {
3435
}
3536

3637
// acquireClient acquires a new http client, and returns an error if failed.
37-
func acquireClient() (*http.Client, error) {
38+
func acquireClient(ctx context.Context) (*http.Client, error) {
3839
fmt.Println("acquire client...")
3940
return &http.Client{}, nil
4041
}
4142

4243
// releaseClient releases the given client, and returns an error if failed.
43-
func releaseClient(client *http.Client) error {
44+
func releaseClient(ctx context.Context, client *http.Client) error {
4445
fmt.Println("release client...")
4546
return nil
4647
}
4748

4849
func main() {
4950
// Prepare some backend resources.
5051
ctx := context.Background()
52+
5153
go runServer()
5254
time.Sleep(time.Second)
5355

@@ -57,7 +59,7 @@ func main() {
5759
// The release function is for releasing the given resource, and you can destroy everything of your resource.
5860
// Also, you can specify some options to change the default settings.
5961
pool := rego.New(4, acquireClient, releaseClient)
60-
defer pool.Close()
62+
defer pool.Close(ctx)
6163

6264
var wg sync.WaitGroup
6365
for i := 0; i < 20; i++ {
@@ -75,10 +77,11 @@ func main() {
7577
// Remember put the client to pool when your using is done.
7678
// This is why we call the resource in pool is reusable.
7779
// We recommend you to do this job in a defer function.
78-
defer pool.Put(client)
80+
defer pool.Put(ctx, client)
7981

8082
// Use the client whatever you want.
8183
body := strings.NewReader(strconv.Itoa(ii))
84+
8285
_, err = client.Post("http://127.0.0.1:9876", "", body)
8386
if err != nil {
8487
panic(err)
@@ -87,5 +90,4 @@ func main() {
8790
}
8891

8992
wg.Wait()
90-
time.Sleep(time.Second)
9193
}

0 commit comments

Comments
 (0)