-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.go
More file actions
48 lines (44 loc) · 1.79 KB
/
hooks.go
File metadata and controls
48 lines (44 loc) · 1.79 KB
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
package tcppool
import (
"net"
"github.com/meliadamian17/tcppool/internal"
)
// PoolHooks defines callback functions that can be triggered
// during various connection pool events. These hooks allow custom
// logic to be executed during connection creation, acquisition, release,
// closure, errors, or during pool creation.
type PoolHooks struct {
// OnConnectionCreate is triggered when a new connection is created.
OnConnectionCreate func(conn net.Conn)
// OnConnectionAcquire is triggered when a connection is acquired from the pool.
OnConnectionAcquire func(conn net.Conn)
// OnConnectionRelease is triggered when a connection is released back into the pool.
OnConnectionRelease func(conn net.Conn)
// OnConnectionClose is triggered when a connection is closed.
OnConnectionClose func(conn net.Conn)
// OnConnectionError is triggered when an error occurs during connection operations.
OnConnectionError func(err error)
// OnPoolCreate is triggered when the connection pool is successfully created.
OnPoolCreate func(c Config)
// OnPoolCreateError is triggered when there is an error during pool creation.
OnPoolCreateError func(err error)
}
// ToInternal converts a public PoolHooks object to the corresponding internal representation.
// This is used to pass hooks from the public API to the internal pool implementation.
func (h PoolHooks) ToInternal() internal.PoolHooks {
return internal.PoolHooks{
OnConnectionCreate: h.OnConnectionCreate,
OnConnectionAcquire: h.OnConnectionAcquire,
OnConnectionRelease: h.OnConnectionRelease,
OnConnectionClose: h.OnConnectionClose,
OnConnectionError: h.OnConnectionError,
OnPoolCreate: func(c internal.ConfigImpl) {
if h.OnPoolCreate != nil {
h.OnPoolCreate(Config{
impl: &c,
})
}
},
OnPoolCreateError: h.OnPoolCreateError,
}
}