This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection_pool_factory.go
105 lines (82 loc) · 2.31 KB
/
connection_pool_factory.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package goBolt
import (
"context"
"fmt"
pool "github.com/jolestar/go-commons-pool"
"github.com/mindstand/go-bolt/connection"
"github.com/mindstand/go-bolt/errors"
)
type ConnectionPooledObjectFactory struct {
connectionString string
}
func (c *ConnectionPooledObjectFactory) MakeObject(ctx context.Context) (*pool.PooledObject, error) {
conn, err := connection.CreateBoltConn(c.connectionString)
if err != nil {
return nil, err
}
return pool.NewPooledObject(conn), nil
}
func (c *ConnectionPooledObjectFactory) DestroyObject(ctx context.Context, object *pool.PooledObject) error {
if object == nil {
return errors.New("pooled object wrapper can not be nil")
}
if object.Object == nil {
return errors.New("pooled object can not be nil")
}
conn, ok := object.Object.(connection.IConnection)
if !ok {
return fmt.Errorf("unable to cast [%T] to [connection.IConnection]", object.Object)
}
if conn.ValidateOpen() {
return conn.Close()
} else {
return nil
}
}
func (c *ConnectionPooledObjectFactory) ValidateObject(ctx context.Context, object *pool.PooledObject) bool {
if object == nil {
return false
}
if object.Object == nil {
return false
}
conn, ok := object.Object.(connection.IConnection)
if !ok {
return false
}
return conn.ValidateOpen()
}
func (c *ConnectionPooledObjectFactory) ActivateObject(ctx context.Context, object *pool.PooledObject) error {
if object == nil {
return errors.New("pooled object wrapper can not be nil")
}
if object.Object == nil {
return errors.New("pooled object can not be nil")
}
conn, ok := object.Object.(connection.IConnection)
if !ok {
return fmt.Errorf("unable to cast [%T] to [connection.IConnection]", object.Object)
}
var err error
if !conn.ValidateOpen() {
conn, err = connection.CreateBoltConn(c.connectionString)
if err != nil {
return err
}
object.Object = conn
}
return nil
}
func (c *ConnectionPooledObjectFactory) PassivateObject(ctx context.Context, object *pool.PooledObject) error {
if object == nil {
return errors.New("pooled object wrapper can not be nil")
}
if object.Object == nil {
return errors.New("pooled object can not be nil")
}
conn, ok := object.Object.(connection.IConnection)
if !ok {
return fmt.Errorf("unable to cast [%T] to [connection.IConnection]", object.Object)
}
return conn.MakeIdle()
}