-
Notifications
You must be signed in to change notification settings - Fork 24
/
universeHelpers.go
146 lines (119 loc) 路 3.08 KB
/
universeHelpers.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
package libdream
import (
"fmt"
commonIface "github.com/taubyte/go-interfaces/common"
commonSpecs "github.com/taubyte/go-specs/common"
peer "github.com/taubyte/p2p/peer"
"golang.org/x/exp/slices"
)
func (u *Universe) defaultClients() map[string]*commonIface.ClientConfig {
clients := make(map[string]*commonIface.ClientConfig)
for _, name := range commonSpecs.P2PStreamProtocols {
clients[name] = &commonIface.ClientConfig{}
}
return clients
}
func (u *Universe) getServiceByNameId(name, id string) (node commonIface.Service, exist bool) {
u.lock.RLock()
defer u.lock.RUnlock()
serviceInfo, exist := u.service[name]
if !exist {
return
}
if serviceInfo == nil || !exist {
return
}
node, exist = serviceInfo.nodes[id]
return
}
func (u *Universe) getSimpleByNameId(name, id string) (simple *Simple, exist bool) {
u.lock.RLock()
defer u.lock.RUnlock()
simple, exist = u.simples[name]
if !exist {
return
}
if simple == nil || !exist {
return
}
return
}
func (u *Universe) killServiceByNameId(name, id string) error {
node, exist := u.getServiceByNameId(name, id)
if !exist {
return fmt.Errorf("killing %s: %s failed with: does not exist", name, id)
}
serviceInfo, exist := u.service[name]
if !exist {
return fmt.Errorf("killing %s: %s failed with: does not exist", name, id)
}
u.lock.Lock()
defer u.lock.Unlock()
node.Close()
delete(serviceInfo.nodes, id)
delete(u.lookups, id)
u.discardNode(node.Node(), false)
return nil
}
func (u *Universe) killSimpleByNameId(name, id string) error {
simple, exist := u.getSimpleByNameId(name, id)
if !exist {
return fmt.Errorf("killing %s: %s failed with: does not exist", name, id)
}
u.lock.Lock()
defer u.lock.Unlock()
simple.Close()
delete(u.simples, name)
delete(u.lookups, id)
u.discardNode(simple.PeerNode(), false)
return nil
}
func (u *Universe) KillNodeByNameID(name, id string) error {
if slices.Contains(commonSpecs.Protocols, name) {
return u.killServiceByNameId(name, id)
} else {
return u.killSimpleByNameId(name, id)
}
}
func (u *Universe) startService(protocol string, config *commonIface.ServiceConfig) (peer.Node, error) {
creationMethod, err := Registry.service(protocol)
if err != nil {
return nil, err
}
service, err := creationMethod(u, config)
if err != nil {
return nil, err
}
u.registerService(protocol, service)
u.toClose(service)
return service.Node(), nil
}
func (s *Simple) startClient(name string, config *commonIface.ClientConfig) error {
creationMethod, err := Registry.client(name)
if err != nil {
return err
}
client, err := creationMethod(s.Node, config)
if err != nil {
return err
}
s.lock.Lock()
s.clients[name] = client
s.lock.Unlock()
if _, err := s.getClient(name); err != nil {
return err
}
return nil
}
func (u *Universe) discardNode(node peer.Node, lock bool) {
// ref: https://stackoverflow.com/questions/20545743/how-to-remove-items-from-a-slice-while-ranging-over-it
if lock {
u.lock.Lock()
defer u.lock.Unlock()
}
for i := len(u.all) - 1; i >= 0; i-- {
if u.all[i] == node {
u.all = append(u.all[:i], u.all[i+1:]...)
}
}
}