-
Notifications
You must be signed in to change notification settings - Fork 150
/
example_multipleBorrowers_test.go
82 lines (67 loc) · 1.54 KB
/
example_multipleBorrowers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package pool_test
import (
"context"
"fmt"
"strconv"
"sync/atomic"
"github.com/jolestar/go-commons-pool/v2"
)
func Example_multipleBorrowers() {
type myPoolObject struct {
s string
}
var v uint64
factory := pool.NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) {
return &myPoolObject{
s: strconv.FormatUint(atomic.AddUint64(&v, 1), 10),
},
nil
})
ctx := context.Background()
p := pool.NewObjectPoolWithDefaultConfig(ctx, factory)
// Borrows #1
obj1, err := p.BorrowObject(ctx)
if err != nil {
panic(err)
}
o := obj1.(*myPoolObject)
fmt.Println(o.s)
// Borrowing again while the first object is borrowed will cause a new object to be made, if
// the pool configuration allows it. If the pull is full, this will block until the context
// is cancelled or an object is returned to the pool.
//
// Borrows #2
obj2, err := p.BorrowObject(ctx)
if err != nil {
panic(err)
}
// Returning the object to the pool makes it available to another borrower.
err = p.ReturnObject(ctx, obj1)
if err != nil {
panic(err)
}
// Since there's an object available in the pool, this gets that rather than creating a new one.
//
// Borrows #1 again (since it was returned earlier)
obj3, err := p.BorrowObject(ctx)
if err != nil {
panic(err)
}
o = obj2.(*myPoolObject)
fmt.Println(o.s)
err = p.ReturnObject(ctx, obj2)
if err != nil {
panic(err)
}
o = obj3.(*myPoolObject)
fmt.Println(o.s)
err = p.ReturnObject(ctx, obj3)
if err != nil {
panic(err)
}
// Output:
// 1
// 2
// 1
}