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
package httpd
import (
"sync"
"github.com/jumpserver/koko/pkg/logger"
)
var conns = &Connections{container: make(map[string][]string), mu: new(sync.RWMutex)}
var clients = &Clients{container: make(map[string]*Client), mu: new(sync.RWMutex)}
type Clients struct {
container map[string]*Client
mu *sync.RWMutex
}
func (c *Clients) GetClient(cID string) (client *Client) {
c.mu.RLock()
defer c.mu.RUnlock()
client = c.container[cID]
return
}
func (c *Clients) DeleteClient(cID string) {
c.mu.RLock()
client, ok := c.container[cID]
c.mu.RUnlock()
if !ok {
return
}
_ = client.Close()
c.mu.Lock()
defer c.mu.Unlock()
delete(c.container, cID)
logger.Debug("Remain clients count: ", len(c.container))
}
func (c *Clients) AddClient(cID string, conn *Client) {
c.mu.Lock()
defer c.mu.Unlock()
c.container[cID] = conn
logger.Debug("Now clients count: ", len(c.container))
}
type Connections struct {
container map[string][]string
mu *sync.RWMutex
}
func (c *Connections) AddClient(cID, clientID string) {
c.mu.Lock()
defer c.mu.Unlock()
clients, ok := c.container[cID]
if ok {
clients = append(clients, clientID)
} else {
clients = []string{clientID}
}
c.container[cID] = clients
}
func (c *Connections) GetClients(cID string) (clients []string) {
c.mu.Lock()
defer c.mu.Unlock()
return c.container[cID]
}
func (c *Connections) DeleteClients(cID string) {
for _, clientID := range c.GetClients(cID) {
clients.DeleteClient(clientID)
}
c.mu.Lock()
defer c.mu.Unlock()
delete(c.container, cID)
}