Skip to content

Commit c80dc40

Browse files
committed
优化代码
1 parent 8640326 commit c80dc40

File tree

7 files changed

+77
-55
lines changed

7 files changed

+77
-55
lines changed

FUTURE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## ✈️ 未来版本的新特性 (Features in future versions)
22

3+
### v0.2.x
4+
5+
* [x] 优化代码
6+
* [ ] 增加生命周期检测,资源检查回调
7+
38
### v0.1.x
49

510
* [x] 实现基础的控制池功能

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 📜 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.2.0
4+
5+
> 此版本发布于 2025-04-12
6+
7+
* 优化代码
8+
39
### v0.1.1
410

511
> 此版本发布于 2024-03-07

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

option.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,36 @@
44

55
package rego
66

7-
import "context"
7+
import (
8+
"context"
9+
"errors"
10+
)
11+
12+
var (
13+
ErrPoolIsFull = errors.New("rego: pool is full")
14+
ErrPoolIsClosed = errors.New("rego: pool is closed")
15+
)
816

917
type config struct {
1018
fastFailed bool
1119

12-
newPoolFullErrFunc func(ctx context.Context) error
13-
newPoolClosedErrFunc func(ctx context.Context) error
20+
newPoolFullErr func(ctx context.Context) error
21+
newPoolClosedErr func(ctx context.Context) error
1422
}
1523

1624
func newDefaultConfig() *config {
25+
newPoolFullErr := func(_ context.Context) error {
26+
return ErrPoolIsFull
27+
}
28+
29+
newPoolClosedErr := func(_ context.Context) error {
30+
return ErrPoolIsClosed
31+
}
32+
1733
conf := &config{
18-
fastFailed: false,
19-
newPoolFullErrFunc: nil,
20-
newPoolClosedErrFunc: nil,
34+
fastFailed: false,
35+
newPoolFullErr: newPoolFullErr,
36+
newPoolClosedErr: newPoolClosedErr,
2137
}
2238

2339
return conf
@@ -39,13 +55,17 @@ func WithFastFailed() Option {
3955
// WithPoolFullErr sets newPoolFullErr to config.
4056
func WithPoolFullErr(newPoolFullErr func(ctx context.Context) error) Option {
4157
return func(conf *config) {
42-
conf.newPoolFullErrFunc = newPoolFullErr
58+
if newPoolFullErr != nil {
59+
conf.newPoolFullErr = newPoolFullErr
60+
}
4361
}
4462
}
4563

4664
// WithPoolClosedErr sets newPoolClosedErr to config.
4765
func WithPoolClosedErr(newPoolClosedErr func(ctx context.Context) error) Option {
4866
return func(conf *config) {
49-
conf.newPoolClosedErrFunc = newPoolClosedErr
67+
if newPoolClosedErr != nil {
68+
conf.newPoolClosedErr = newPoolClosedErr
69+
}
5070
}
5171
}

option_test.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
)
1212

13-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWithFastFailed$
13+
// go test -v -cover -run=^TestWithFastFailed$
1414
func TestWithFastFailed(t *testing.T) {
1515
conf := &config{fastFailed: false}
1616
WithFastFailed()(conf)
@@ -20,30 +20,42 @@ func TestWithFastFailed(t *testing.T) {
2020
}
2121
}
2222

23-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWithPoolFullErr$
23+
// go test -v -cover -run=^TestWithPoolFullErr$
2424
func TestWithPoolFullErr(t *testing.T) {
2525
newPoolFullErr := func(ctx context.Context) error {
2626
return nil
2727
}
2828

29-
conf := &config{newPoolFullErrFunc: nil}
29+
conf := &config{newPoolFullErr: nil}
3030
WithPoolFullErr(newPoolFullErr)(conf)
3131

32-
if fmt.Sprintf("%p", conf.newPoolFullErrFunc) != fmt.Sprintf("%p", newPoolFullErr) {
33-
t.Fatalf("conf.newPoolFullErrFunc %p is wrong", conf.newPoolFullErrFunc)
32+
if fmt.Sprintf("%p", conf.newPoolFullErr) != fmt.Sprintf("%p", newPoolFullErr) {
33+
t.Fatalf("conf.newPoolFullErr %p is wrong", conf.newPoolFullErr)
34+
}
35+
36+
WithPoolFullErr(nil)(conf)
37+
38+
if fmt.Sprintf("%p", conf.newPoolFullErr) != fmt.Sprintf("%p", newPoolFullErr) {
39+
t.Fatalf("conf.newPoolFullErr %p is wrong", conf.newPoolFullErr)
3440
}
3541
}
3642

37-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWithPoolClosedErr$
43+
// go test -v -cover -run=^TestWithPoolClosedErr$
3844
func TestWithPoolClosedErr(t *testing.T) {
3945
newPoolClosedErr := func(ctx context.Context) error {
4046
return nil
4147
}
4248

43-
conf := &config{newPoolClosedErrFunc: nil}
49+
conf := &config{newPoolClosedErr: nil}
4450
WithPoolClosedErr(newPoolClosedErr)(conf)
4551

46-
if fmt.Sprintf("%p", conf.newPoolClosedErrFunc) != fmt.Sprintf("%p", newPoolClosedErr) {
47-
t.Fatalf("conf.newPoolClosedErrFunc %p is wrong", conf.newPoolClosedErrFunc)
52+
if fmt.Sprintf("%p", conf.newPoolClosedErr) != fmt.Sprintf("%p", newPoolClosedErr) {
53+
t.Fatalf("conf.newPoolClosedErr %p is wrong", conf.newPoolClosedErr)
54+
}
55+
56+
WithPoolClosedErr(nil)(conf)
57+
58+
if fmt.Sprintf("%p", conf.newPoolClosedErr) != fmt.Sprintf("%p", newPoolClosedErr) {
59+
t.Fatalf("conf.newPoolClosedErr %p is wrong", conf.newPoolClosedErr)
4860
}
4961
}

pool.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ package rego
66

77
import (
88
"context"
9-
"errors"
109
"sync"
1110
)
1211

13-
var (
14-
ErrPoolIsFull = errors.New("rego: pool is full")
15-
ErrPoolIsClosed = errors.New("rego: pool is closed")
16-
)
17-
1812
type Status struct {
1913
// Limit is the limit of acquired resources.
2014
Limit uint64 `json:"limit"`
@@ -57,6 +51,10 @@ type Pool[T any] struct {
5751
}
5852

5953
func New[T any](limit uint64, acquire AcquireFunc[T], release ReleaseFunc[T], opts ...Option) *Pool[T] {
54+
if limit <= 0 {
55+
panic("rego: limit can't be less than 0")
56+
}
57+
6058
if acquire == nil || release == nil {
6159
panic("rego: acquire or release func can't be nil")
6260
}
@@ -94,22 +92,6 @@ func (p *Pool[T]) Put(resource T) error {
9492
}
9593
}
9694

97-
func (p *Pool[T]) newPoolFullErr(ctx context.Context) error {
98-
if p.conf.newPoolFullErrFunc == nil {
99-
return ErrPoolIsFull
100-
}
101-
102-
return p.conf.newPoolFullErrFunc(ctx)
103-
}
104-
105-
func (p *Pool[T]) newPoolClosedErr(ctx context.Context) error {
106-
if p.conf.newPoolClosedErrFunc == nil {
107-
return ErrPoolIsClosed
108-
}
109-
110-
return p.conf.newPoolClosedErrFunc(ctx)
111-
}
112-
11395
func (p *Pool[T]) tryToTake() (resource T, ok bool) {
11496
select {
11597
case resource = <-p.resources:
@@ -144,7 +126,7 @@ func (p *Pool[T]) Take(ctx context.Context) (resource T, err error) {
144126
if p.closed {
145127
p.lock.Unlock()
146128

147-
return resource, p.newPoolClosedErr(ctx)
129+
return resource, p.conf.newPoolClosedErr(ctx)
148130
}
149131

150132
var ok bool
@@ -174,7 +156,7 @@ func (p *Pool[T]) Take(ctx context.Context) (resource T, err error) {
174156
if p.conf.fastFailed {
175157
p.lock.Unlock()
176158

177-
return resource, p.newPoolFullErr(ctx)
159+
return resource, p.conf.newPoolFullErr(ctx)
178160
}
179161

180162
p.waiting++
@@ -205,19 +187,16 @@ func (p *Pool[T]) Status() Status {
205187
}
206188

207189
func (p *Pool[T]) releaseResources() error {
208-
for i := uint64(0); i < p.acquired; i++ {
209-
resource := <-p.resources
210-
if err := p.release(resource); err != nil {
211-
return err
190+
for {
191+
select {
192+
case resource := <-p.resources:
193+
if err := p.release(resource); err != nil {
194+
return err
195+
}
196+
default:
197+
return nil
212198
}
213199
}
214-
// for resource := range p.resources {
215-
// if err := p.release(resource); err != nil {
216-
// return err
217-
// }
218-
// }
219-
220-
return nil
221200
}
222201

223202
// Close closes pool and releases all resources.

pool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"time"
1313
)
1414

15-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWithFastFailed$
15+
// go test -v -cover -run=^TestWithFastFailed$
1616
func TestPool(t *testing.T) {
1717
limit := int64(16)
1818
acquireLimit := int64(0)

0 commit comments

Comments
 (0)