File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ type pool struct {
38
38
39
39
func (p * pool ) Acquire () (v wire ) {
40
40
p .cond .L .Lock ()
41
+ retry:
41
42
for len (p .list ) == 0 && p .size == p .cap && ! p .down {
42
43
p .cond .Wait ()
43
44
}
@@ -51,6 +52,11 @@ func (p *pool) Acquire() (v wire) {
51
52
v = p .list [i ]
52
53
p .list [i ] = nil
53
54
p .list = p .list [:i ]
55
+ if v .Error () != nil {
56
+ p .size --
57
+ v .Close ()
58
+ goto retry
59
+ }
54
60
}
55
61
p .cond .L .Unlock ()
56
62
return v
Original file line number Diff line number Diff line change @@ -49,6 +49,34 @@ func TestPool(t *testing.T) {
49
49
}
50
50
})
51
51
52
+ t .Run ("Reuse without broken connections" , func (t * testing.T ) {
53
+ pool , count := setup (100 )
54
+ c1 := pool .Acquire ()
55
+ c2 := pool .Acquire ()
56
+ pool .Store (c1 )
57
+ pool .Store (c2 )
58
+ pool .cond .L .Lock ()
59
+ for _ , p := range pool .list {
60
+ p .Close ()
61
+ }
62
+ pool .cond .L .Unlock ()
63
+ c3 := pool .Acquire ()
64
+ if c3 .Error () != nil {
65
+ t .Fatalf ("c3.Error() is not nil" )
66
+ }
67
+ if atomic .LoadInt32 (count ) != 3 {
68
+ t .Fatalf ("pool does not clean borken connections" )
69
+ }
70
+ pool .cond .L .Lock ()
71
+ defer pool .cond .L .Unlock ()
72
+ if pool .size != 1 {
73
+ t .Fatalf ("pool size is not 1" )
74
+ }
75
+ if len (pool .list ) != 0 {
76
+ t .Fatalf ("pool list is not empty" )
77
+ }
78
+ })
79
+
52
80
t .Run ("NotExceed" , func (t * testing.T ) {
53
81
conn := make ([]wire , 100 )
54
82
pool , count := setup (len (conn ))
You can’t perform that action at this time.
0 commit comments