-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpool.go
152 lines (115 loc) · 2.48 KB
/
pool.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2021 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package garment
import (
"fmt"
"sync"
)
var (
// pool var
pool *Pool
// once var
once sync.Once
)
// PingCallback type
type PingCallback func(interface{}) error
// CloseCallback type
type CloseCallback func(interface{}) error
// ReconnectCallback type
type ReconnectCallback func(interface{}) error
// Connection type
type Connection struct {
Value interface{}
Ping PingCallback
Close CloseCallback
Reconnect ReconnectCallback
}
// Pool type
type Pool struct {
sync.RWMutex
items map[string]Connection
}
// NewPool creates a new instance of Pool
func NewPool() *Pool {
once.Do(func() {
pool = &Pool{
items: make(map[string]Connection),
}
})
return pool
}
// Get gets a connection with a key
func (c *Pool) Get(key string) interface{} {
c.Lock()
defer c.Unlock()
connection, ok := c.items[key]
if !ok {
return nil
}
return connection.Value
}
// Has checks if connection exists
func (c *Pool) Has(key string) bool {
if val := c.Get(key); val != nil {
return true
}
return false
}
// Set sets a connection with a key
func (c *Pool) Set(key string, value interface{}, ping PingCallback, close CloseCallback, reconnect ReconnectCallback) {
c.Lock()
defer c.Unlock()
c.items[key] = Connection{
Value: value,
Ping: ping,
Close: close,
Reconnect: reconnect,
}
}
// Close close the connection and removes the key
func (c *Pool) Close(key string) error {
c.Lock()
defer c.Unlock()
connection, ok := c.items[key]
if !ok {
return fmt.Errorf("Unable to find %s", key)
}
return connection.Close(connection.Value)
}
// Ping checks the connection status
func (c *Pool) Ping(key string) error {
c.Lock()
defer c.Unlock()
connection, ok := c.items[key]
if !ok {
return fmt.Errorf("Unable to find %s", key)
}
return connection.Ping(connection.Value)
}
// Reconnect reconnects again
func (c *Pool) Reconnect(key string) error {
c.Lock()
defer c.Unlock()
connection, ok := c.items[key]
if !ok {
return fmt.Errorf("Unable to find %s", key)
}
return connection.Reconnect(connection.Value)
}
// Remove removes a connection
func (c *Pool) Remove(key string) {
c.Lock()
defer c.Unlock()
_, ok := c.items[key]
if !ok {
return
}
delete(c.items, key)
}
// Count counts the number of managed connections
func (c *Pool) Count() int {
c.Lock()
defer c.Unlock()
return len(c.items)
}