Skip to content

Commit 2633f27

Browse files
authored
Merge pull request #34 from dcormier/master
Updated documentation so examples show up properly
2 parents 3f5d5f8 + d42c09a commit 2633f27

File tree

5 files changed

+192
-171
lines changed

5 files changed

+192
-171
lines changed

example__simple_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package pool_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
"sync/atomic"
8+
9+
"github.com/jolestar/go-commons-pool"
10+
)
11+
12+
func Example_simple() {
13+
type myPoolObject struct {
14+
s string
15+
}
16+
17+
v := uint64(0)
18+
factory := pool.NewPooledObjectFactorySimple(
19+
func(context.Context) (interface{}, error) {
20+
return &myPoolObject{
21+
s: strconv.FormatUint(atomic.AddUint64(&v, 1), 10),
22+
},
23+
nil
24+
})
25+
26+
ctx := context.Background()
27+
p := pool.NewObjectPoolWithDefaultConfig(ctx, factory)
28+
29+
obj, err := p.BorrowObject(ctx)
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
o := obj.(*myPoolObject)
35+
fmt.Println(o.s)
36+
37+
err = p.ReturnObject(ctx, obj)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
// Output: 1
43+
}

example_customFactory_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package pool_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
"sync/atomic"
8+
9+
"github.com/jolestar/go-commons-pool"
10+
)
11+
12+
type MyPoolObject struct {
13+
s string
14+
}
15+
16+
type MyCustomFactory struct {
17+
v uint64
18+
}
19+
20+
func (f *MyCustomFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) {
21+
return pool.NewPooledObject(
22+
&MyPoolObject{
23+
s: strconv.FormatUint(atomic.AddUint64(&f.v, 1), 10),
24+
}),
25+
nil
26+
}
27+
28+
func (f *MyCustomFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error {
29+
// do destroy
30+
return nil
31+
}
32+
33+
func (f *MyCustomFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool {
34+
// do validate
35+
return true
36+
}
37+
38+
func (f *MyCustomFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error {
39+
// do activate
40+
return nil
41+
}
42+
43+
func (f *MyCustomFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error {
44+
// do passivate
45+
return nil
46+
}
47+
48+
func Example_customFactory() {
49+
ctx := context.Background()
50+
p := pool.NewObjectPoolWithDefaultConfig(ctx, &MyCustomFactory{})
51+
52+
obj1, err := p.BorrowObject(ctx)
53+
if err != nil {
54+
panic(err)
55+
}
56+
57+
o := obj1.(*MyPoolObject)
58+
fmt.Println(o.s)
59+
60+
err = p.ReturnObject(ctx, obj1)
61+
if err != nil {
62+
panic(err)
63+
}
64+
65+
// Output: 1
66+
}

example_multipleBorrowers_test.go

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

example_test.go

Lines changed: 0 additions & 170 deletions
This file was deleted.

pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (pool *ObjectPool) addIdleObject(ctx context.Context, p *PooledObject) erro
133133
// PooledObjectFactory.ValidateObject.
134134
// If the pool is full (based on the number of objects in the pool and the
135135
// value of the MaxTotal configuration field), this method will block until
136-
// an object is returned to the pool or the context is cancelled.
136+
// an object is returned to the pool or the context is done.
137137
//
138138
// By contract, clients must return the borrowed instance
139139
// using ReturnObject, InvalidateObject

0 commit comments

Comments
 (0)