-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectionpool.go
62 lines (54 loc) · 1.24 KB
/
connectionpool.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
package main
import "sync"
import "log"
type ConnectionBackoff struct {
Server string
Backoff int
}
type ConnectionPool struct {
connections []*IRC
backoff []ConnectionBackoff
c *Config
sync.Mutex
}
func (c *ConnectionPool) GetBackoffAndIncrement(Server string) int {
for _,b := range(c.backoff) {
if b.Server == Server {
b.Backoff++
return b.Backoff
}
}
c.backoff = append(c.backoff, ConnectionBackoff{Server, 1})
return 1
}
func (c *ConnectionPool) GetConnection(Server string) *IRC {
c.Lock()
defer c.Unlock()
for _, irc := range(c.connections) {
if irc.Server == Server {
return irc
}
}
irc := IRC{Server: Server, Proxy: c.c.GetProxyDial()}
if irc.Connect() {
c.connections = append(c.connections, &irc)
return &irc
} else {
return nil
}
}
func (c *ConnectionPool) Quit(server string) {
log.Print("quitting " + server)
c.Lock()
defer c.Unlock()
for indx, irc := range(c.connections) {
if irc.Server == server {
log.Print("quitting connection")
irc.Quit()
log.Print(c.connections)
c.connections = append(c.connections[:indx], c.connections[indx+1:]...)
log.Print(c.connections)
break
}
}
}