-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
220 lines (175 loc) · 5.25 KB
/
api.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package quacktors
import (
"errors"
"github.com/Azer0s/quacktors/typeregister"
"github.com/opentracing/opentracing-go"
"go.uber.org/atomic"
"reflect"
"regexp"
"strings"
"sync"
)
var initCalled = atomic.NewBool(false)
func callInitIfNotCalled() {
if !initCalled.Load() {
initCalled.Store(true)
initQuacktorSystems()
}
}
//RegisterType registers a Message to the type store so it can
//be sent to remote machines (which, of course, need a Message
//with the same Message.Type registered).
func RegisterType(message Message) {
callInitIfNotCalled()
t := reflect.ValueOf(message).Type().Kind()
if t == reflect.Ptr {
panic("RegisterType cannot be called with a pointer to a Message")
}
if message.Type() == "" {
panic("message.Type() can not return an empty string")
}
typeregister.Store(message.Type(), message)
logger.Info("registered type",
"type", message.Type(),
)
}
//RootContext returns a context that can be used outside an Actor.
//It is not associated with a real PID and therefore, one should
//not call anything with the RootContext that requires it to be
//able to receive or the like (e.g. no Context.Quit, Context.Monitor, etc.).
func RootContext() Context {
callInitIfNotCalled()
return Context{
self: &Pid{Id: "root", MachineId: machineId},
Logger: contextLogger{pid: "root"},
sendLock: &sync.Mutex{},
deferred: make([]func(), 0),
}
}
//RootContextWithSpan is the same as RootContext but with a tracing
//span attached to it so it will do distributed tracing.
func RootContextWithSpan(span opentracing.Span) Context {
callInitIfNotCalled()
return Context{
self: &Pid{Id: "root", MachineId: machineId},
Logger: contextLogger{pid: "root"},
sendLock: &sync.Mutex{},
deferred: make([]func(), 0),
span: span,
traceFork: opentracing.FollowsFrom,
}
}
//VectorContext creates a context with a custom name and
//opentracing.Span. This allows for integrating applications
//with quacktors.
func VectorContext(name string, span opentracing.Span) Context {
callInitIfNotCalled()
return Context{
self: &Pid{Id: name, MachineId: machineId},
Logger: contextLogger{pid: name},
sendLock: &sync.Mutex{},
deferred: make([]func(), 0),
span: span,
traceFork: opentracing.FollowsFrom,
}
}
//Spawn spawns an Actor from an anonymous receive function and
//returns the *Pid of the Actor.
func Spawn(action func(ctx *Context, message Message)) *Pid {
callInitIfNotCalled()
return startActor(&StatelessActor{
InitFunction: func(ctx *Context) {},
ReceiveFunction: action,
})
}
//SpawnWithInit spawns an Actor from an anonymous receive function
//and an anonymous init function and returns the *Pid of the Actor.
func SpawnWithInit(init func(ctx *Context), action func(ctx *Context, message Message)) *Pid {
callInitIfNotCalled()
return startActor(&StatelessActor{
InitFunction: init,
ReceiveFunction: action,
})
}
//SpawnStateful spawns an Actor.
func SpawnStateful(actor Actor) *Pid {
callInitIfNotCalled()
return startActor(actor)
}
//NewSystem creates a new system server, connects to
//qpmd, starts the qpmd heartbeat for the new system and
//returns a *System and an error (nil if everything
//went fine).
func NewSystem(name string) (*System, error) {
callInitIfNotCalled()
logger.Info("initializing new system",
"system_name", name)
s := &System{
name: name,
handlers: make(map[string]*Pid),
handlersMu: &sync.RWMutex{},
quitChan: make(chan bool),
heartbeatQuitChan: make(chan bool),
}
p, err := s.startServer()
if err != nil {
logger.Warn("there was an error while starting the system server",
"system_name", s.name,
"error", err)
return &System{}, err
}
conn, err := qpmdRegister(s, p)
if err != nil {
logger.Warn("there was an error while registering system to qpmd",
"system_name", s.name,
"error", err)
return &System{}, err
}
qpmdHeartbeat(conn, s)
return s, nil
}
//Connect connects to a remote system and returns a
//*RemoteSystem and an error (nil if everything went
//fine). The connection string format should be
//"system@remote" where "system" is the name of the
//remote system and "remote" is either an IP or a
//domain name.
func Connect(name string) (*RemoteSystem, error) {
callInitIfNotCalled()
matched, err := regexp.MatchString("(\\w+)@(.+)", name)
if !matched || err != nil {
return &RemoteSystem{}, errors.New("invalid connection string format")
}
s := strings.SplitN(name, "@", 2)
logger.Info("connecting to remote system",
"system_name", s[0],
"remote_address", s[1])
r, err := qpmdLookup(s[0], s[1])
if err != nil {
logger.Warn("there was an error while looking up remote system",
"system_name", s[0],
"remote_address", s[1],
"error", err)
return &RemoteSystem{}, err
}
if r.MachineId == machineId {
panic("can't connect to system, system is on the same quacktor instance")
}
if m, ok := getMachine(r.MachineId); ok {
r.Machine = m
} else {
//start connections to remote machine
logger.Warn("remote machine is not yet connected",
"machine_id", r.MachineId)
err := r.Machine.connect()
if err != nil {
return &RemoteSystem{}, err
}
registerMachine(r.Machine)
}
err = r.sayHello()
if err != nil {
return &RemoteSystem{}, err
}
return r, nil
}