-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrpc_pool.go
More file actions
172 lines (151 loc) · 4.36 KB
/
rpc_pool.go
File metadata and controls
172 lines (151 loc) · 4.36 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package godoo
import (
"fmt"
"sync"
"time"
"github.com/wongpinter/godoo/logr" // Assuming a local logr package
)
// ConnectionPool manages a collection of godoo clients.
type ConnectionPool struct {
clients map[string]*Client
mutex sync.RWMutex
maxRetries int
retryInterval time.Duration
}
// NewConnectionPool creates and initializes a new connection pool.
// It attempts to connect to all servers defined in serverConfigs and retries failed connections in the background.
func NewConnectionPool(retryInterval time.Duration, maxRetries int, serverConfigs map[string]*ClientConfig) (*ConnectionPool, error) {
pool := &ConnectionPool{
clients: make(map[string]*Client),
maxRetries: maxRetries,
retryInterval: retryInterval,
}
var wg sync.WaitGroup
var mu sync.Mutex // Mutex to protect access to failedClients map
failedClients := make(map[string]*ClientConfig)
for key, cfg := range serverConfigs {
client, err := NewClient(cfg)
if err != nil {
logr.Warnf("Connecting to XML-RPC %s... Failed with error: %v", cfg.URL, err)
failedClients[key] = cfg
continue
}
logr.Infof("Connecting to XML-RPC %s... Success", cfg.URL)
pool.clients[key] = client
}
// Retry failed connections in the background.
if len(failedClients) > 0 {
wg.Add(len(failedClients))
for key, cfg := range failedClients {
go func(k string, c *ClientConfig) {
defer wg.Done()
client := pool.retryConnection(c)
if client != nil {
mu.Lock()
pool.clients[k] = client
mu.Unlock()
}
}(key, cfg)
}
}
return pool, nil
}
// retryConnection attempts to establish a connection with retries.
func (p *ConnectionPool) retryConnection(cfg *ClientConfig) *Client {
for i := 1; i <= p.maxRetries; i++ {
client, err := NewClient(cfg)
if err == nil {
logr.Infof("Successfully reconnected to %s", cfg.URL)
return client
}
logr.Warnf("Retry %d/%d for %s failed: %v", i, p.maxRetries, cfg.URL, err)
time.Sleep(p.retryInterval)
}
logr.Warnf("Failed to create client for URL %s after %d retries", cfg.URL, p.maxRetries)
return nil
}
// GetClient retrieves a client from the pool by its key.
func (p *ConnectionPool) GetClient(key string) (*Client, error) {
p.mutex.RLock()
defer p.mutex.RUnlock()
client, ok := p.clients[key]
if !ok {
return nil, fmt.Errorf("client with key %s does not exist", key)
}
return client, nil
}
// NumConnections returns the number of active connections in the pool.
func (p *ConnectionPool) NumConnections() int {
p.mutex.RLock()
defer p.mutex.RUnlock()
return len(p.clients)
}
// Close gracefully closes all client connections in the pool.
func (p *ConnectionPool) Close() {
p.mutex.Lock()
defer p.mutex.Unlock()
for _, client := range p.clients {
client.Close()
}
p.clients = make(map[string]*Client) // Clear the map
}
// RemoveClient closes and removes a client from the pool.
func (p *ConnectionPool) RemoveClient(key string) {
p.mutex.Lock()
defer p.mutex.Unlock()
if client, ok := p.clients[key]; ok {
client.Close()
delete(p.clients, key)
}
}
// AddClient creates a new client and adds it to the pool.
func (p *ConnectionPool) AddClient(key string, cfg ClientConfig) error {
p.mutex.Lock()
defer p.mutex.Unlock()
if _, ok := p.clients[key]; ok {
return fmt.Errorf("client with key %s already exists", key)
}
client, err := NewClient(&cfg)
if err != nil {
return err
}
p.clients[key] = client
return nil
}
// CheckConnections verifies all active connections and attempts to reconnect any that have failed.
func (p *ConnectionPool) CheckConnections() {
p.mutex.RLock()
disconnected := make(map[string]*ClientConfig)
for key, client := range p.clients {
if err := client.Ping(); err != nil {
logr.Warnf("Connection for %s is disconnected: %v", key, err)
disconnected[key] = client.Config()
}
}
p.mutex.RUnlock()
if len(disconnected) == 0 {
return
}
// Remove disconnected clients first
p.mutex.Lock()
for key := range disconnected {
if client, ok := p.clients[key]; ok {
client.Close()
delete(p.clients, key)
}
}
p.mutex.Unlock()
// Retry connections in the background
var wg sync.WaitGroup
wg.Add(len(disconnected))
for key, cfg := range disconnected {
go func(k string, c *ClientConfig) {
defer wg.Done()
if client := p.retryConnection(c); client != nil {
p.mutex.Lock()
p.clients[k] = client
p.mutex.Unlock()
}
}(key, cfg)
}
}