-
Notifications
You must be signed in to change notification settings - Fork 0
/
glats.gleam
455 lines (404 loc) · 11.4 KB
/
glats.gleam
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import gleam/io
import gleam/result
import gleam/dynamic.{Dynamic}
import gleam/map.{Map}
import gleam/option.{None, Option, Some}
import gleam/list
import gleam/otp/actor.{StartError}
import gleam/erlang/atom.{Atom}
import gleam/erlang/process.{Pid, Subject}
import glats/settings.{Settings}
import glats/message.{Message}
import glats/protocol.{ServerInfo}
import glats/internal/decoder
pub type Connection =
Subject(ConnectionMessage)
/// Errors that can be returned by the server.
///
pub type ConnectionError {
Timeout
NoResponders
Unexpected
}
// //
// Connection //
// //
/// Message sent to a NATS connection process.
pub opaque type ConnectionMessage {
Subscribe(
from: Subject(Result(Int, String)),
subscriber: Subject(Message),
subject: String,
queue_group: Option(String),
)
Unsubscribe(from: Subject(Result(Nil, String)), sid: Int)
Publish(from: Subject(Result(Nil, String)), message: Message)
Request(
from: Subject(Result(Message, ConnectionError)),
subject: String,
message: String,
)
GetServerInfo(from: Subject(Result(ServerInfo, ConnectionError)))
GetActiveSubscriptions(from: Subject(Result(Int, ConnectionError)))
Exited(process.ExitMessage)
}
type SubscriptionActorMessage {
GetSid(Subject(Int))
ReceivedMessage(Message)
DecodeError(Dynamic)
SubscriberExited
}
type ConnectionState {
ConnectionState(nats: Pid, self: Subject(ConnectionMessage))
}
type SubscriptionActorState {
SubscriptionActorState(
conn: Connection,
sid: Int,
subscriber: Subject(Message),
)
}
// Gnat's GenServer start_link.
external fn gnat_start_link(
settings: Map(String, Dynamic),
) -> actor.ErlangStartResult =
"Elixir.Gnat" "start_link"
// Gnat's publish function.
external fn gnat_pub(Pid, String, String, List(#(Atom, String))) -> Atom =
"Elixir.Gnat" "pub"
// Gnat's request function.
external fn gnat_request(
Pid,
String,
String,
List(#(Atom, String)),
) -> Result(Dynamic, Atom) =
"Elixir.Gnat" "request"
// Gnat's subscribe function.
external fn gnat_sub(
Pid,
Pid,
String,
List(#(Atom, String)),
) -> Result(Int, String) =
"Elixir.Gnat" "sub"
// Gnat's unsubscribe function.
external fn gnat_unsub(Pid, Int, List(#(Atom, String))) -> Atom =
"Elixir.Gnat" "unsub"
// Gnat's server_info function.
external fn gnat_server_info(Pid) -> Dynamic =
"Elixir.Gnat" "server_info"
// Gnat's active_subscriptions function.
external fn gnat_active_subscriptions(Pid) -> Result(Int, Dynamic) =
"Elixir.Gnat" "active_subscriptions"
/// Starts an actor that handles a connection to NATS using the provided
/// settings.
///
pub fn connect(settings: Settings) -> Result(Connection, StartError) {
// Start actor for NATS connection handling.
// This just starts Gnat's GenServer module linked to
// the actor process and translates commands.
actor.start_spec(actor.Spec(
init: fn() {
process.trap_exits(True)
let subject = process.new_subject()
let selector =
process.new_selector()
|> process.selecting_trapped_exits(Exited)
// Start linked process using Gnat's start_link
case
gnat_start_link(
settings
|> build_settings,
)
{
Ok(pid) ->
actor.Ready(ConnectionState(nats: pid, self: subject), selector)
Error(_) -> actor.Failed("starting connection failed")
}
},
init_timeout: 5000,
loop: handle_command,
))
}
// Runs for every command received.
//
fn handle_command(message: ConnectionMessage, state: ConnectionState) {
case message {
Exited(em) -> actor.Stop(em.reason)
Publish(from, msg) -> handle_publish(from, msg, state)
Request(from, subject, msg) -> handle_request(from, subject, msg, state)
Subscribe(from, subscriber, subject, queue_group) ->
handle_subscribe(from, subscriber, subject, queue_group, state)
Unsubscribe(from, sid) -> handle_unsubscribe(from, sid, state)
GetServerInfo(from) -> handle_server_info(from, state)
GetActiveSubscriptions(from) -> handle_active_subscriptions(from, state)
_ -> actor.Continue(state)
}
}
// Handles a single server info command.
//
fn handle_server_info(from, state: ConnectionState) {
gnat_server_info(state.nats)
|> decoder.decode_server_info
|> result.map_error(fn(_) { Unexpected })
|> process.send(from, _)
actor.Continue(state)
}
// Handles a single active subscriptions command.
//
fn handle_active_subscriptions(from, state: ConnectionState) {
gnat_active_subscriptions(state.nats)
|> result.map_error(fn(_) { Unexpected })
|> process.send(from, _)
actor.Continue(state)
}
// Handles a single publish command.
//
fn handle_publish(from, message: Message, state: ConnectionState) {
case
gnat_pub(state.nats, message.subject, message.body, [])
|> atom.to_string
{
"ok" -> process.send(from, Ok(Nil))
_ -> process.send(from, Error("unknown publish error"))
}
actor.Continue(state)
}
// Handles a single request command.
//
fn handle_request(from, subject, message, state: ConnectionState) {
case gnat_request(state.nats, subject, message, []) {
Ok(msg) ->
decoder.decode_msg(msg)
|> result.map_error(fn(_) { Unexpected })
|> process.send(from, _)
Error(err) ->
case atom.to_string(err) {
"timeout" -> Error(Timeout)
"no_responders" -> Error(NoResponders)
_ -> Error(Unexpected)
}
|> process.send(from, _)
}
actor.Continue(state)
}
// Handles a single unsubscribe command.
//
fn handle_unsubscribe(from, sid, state: ConnectionState) {
case
gnat_unsub(state.nats, sid, [])
|> atom.to_string
{
"ok" -> process.send(from, Ok(Nil))
_ -> process.send(from, Error("unknown unsubscribe error"))
}
actor.Continue(state)
}
// Handles a single subscribe command.
//
fn handle_subscribe(
from,
subscriber,
subject: String,
queue_group: Option(String),
state: ConnectionState,
) {
case start_subscription_actor(subscriber, subject, queue_group, state) {
Ok(actor) ->
case process.try_call(actor, GetSid, 1000) {
Ok(sid) -> process.send(from, Ok(sid))
Error(_) -> process.send(from, Error("subscribe failed"))
}
Error(_) -> process.send(from, Error("subscribe failed"))
}
actor.Continue(state)
}
// Since Gnat will send messages from Elixir we need to translate it
// to a type in Gleam _before_ passing it to the user.
// This is done by starting a new actor that will receive the messages
// from Gnat, decode them into `Message` and then send it to the actual
// subscribing subject.
//
fn start_subscription_actor(
subscriber,
subject,
queue_group,
state: ConnectionState,
) {
actor.start_spec(actor.Spec(
init: fn() {
// Monitor subscriber process.
let monitor =
process.monitor_process(
subscriber
|> process.subject_owner,
)
let selector =
process.new_selector()
|> process.selecting_process_down(monitor, fn(_) { SubscriberExited })
|> process.selecting_record2(
atom.create_from_string("msg"),
fn(data) {
data
|> decoder.decode_msg
|> result.map(ReceivedMessage)
|> result.unwrap(DecodeError(data))
},
)
let opts = case queue_group {
Some(group) -> [#(atom.create_from_string("queue_group"), group)]
None -> []
}
case gnat_sub(state.nats, process.self(), subject, opts) {
Ok(sid) ->
actor.Ready(
SubscriptionActorState(state.self, sid, subscriber),
selector,
)
Error(err) -> actor.Failed(err)
}
},
init_timeout: 5000,
loop: subscription_loop,
))
}
fn subscription_loop(
message: SubscriptionActorMessage,
state: SubscriptionActorState,
) {
case message {
GetSid(from) -> {
actor.send(from, state.sid)
actor.Continue(state)
}
ReceivedMessage(msg) -> {
actor.send(state.subscriber, msg)
actor.Continue(state)
}
DecodeError(data) -> {
io.debug(data)
actor.Continue(state)
}
SubscriberExited -> {
io.println("subscriber exited")
// TODO: handle properly
unsubscribe(state.conn, state.sid)
actor.Stop(process.Normal)
}
}
}
// //
// Publish //
// //
/// Publishes a single message to NATS on a provided subject.
///
pub fn publish(conn: Connection, subject: String, message: String) {
publish_message(conn, Message(subject, map.new(), None, message))
}
/// Publishes a single message to NATS using the data from a provided `Message`
/// record.
///
pub fn publish_message(conn: Connection, message: Message) {
process.call(conn, Publish(_, message), 5000)
}
/// Sends a request and listens for a response synchronously.
///
/// See [request-reply pattern docs.](https://docs.nats.io/nats-concepts/core-nats/reqreply)
///
pub fn request(conn: Connection, subject: String, message: String) {
process.call(conn, Request(_, subject, message), 5000)
}
// //
// Subscribe //
// //
/// Subscribes to a NATS subject that can be received on the
/// provided OTP subject.
///
pub fn subscribe(
conn: Connection,
subscriber: Subject(Message),
subject: String,
) {
process.call(conn, Subscribe(_, subscriber, subject, None), 5000)
}
/// Unsubscribe from a subscription by providing the subscription ID.
///
pub fn unsubscribe(conn: Connection, sid: Int) {
process.call(conn, Unsubscribe(_, sid), 5000)
}
/// Subscribes to a NATS subject as part of a queue group.
/// Messages can be received on the provided OTP subject.
///
/// See [Queue Groups docs.](https://docs.nats.io/nats-concepts/core-nats/queue)
///
pub fn queue_subscribe(
conn: Connection,
subscriber: Subject(Message),
subject: String,
group: String,
) {
process.call(conn, Subscribe(_, subscriber, subject, Some(group)), 5000)
}
// //
// Server Info //
// //
/// Returns server info provided by the connected NATS server.
///
pub fn server_info(conn: Connection) {
process.call(conn, GetServerInfo, 5000)
}
// //
// Active Subscriptions //
// //
/// Returns the number of active subscriptions for the connection.
///
pub fn active_subscriptions(conn: Connection) {
process.call(conn, GetActiveSubscriptions, 5000)
}
// Settings mapping helpers to create a map out of the settings
// type, which is expected by Gnat's start_link.
fn build_settings(settings: Settings) {
[]
|> take_host(settings)
|> take_port(settings)
|> take_tls(settings)
|> take_ssl_opts(settings)
|> map.from_list
}
fn take_host(old: List(#(String, Dynamic)), settings: Settings) {
case settings.host {
Some(host) ->
old
|> add_opt_to_list("host", host)
None -> old
}
}
fn take_port(old: List(#(String, Dynamic)), settings: Settings) {
case settings.port {
Some(port) ->
old
|> add_opt_to_list("port", port)
None -> old
}
}
fn take_tls(old: List(#(String, Dynamic)), settings: Settings) {
case settings.tls {
Some(tls) ->
old
|> add_opt_to_list("tls", tls)
None -> old
}
}
fn take_ssl_opts(old: List(#(String, Dynamic)), settings: Settings) {
case settings.ssl_opts {
Some(ssl_opts) ->
old
|> add_opt_to_list("ssl_opts", ssl_opts)
None -> old
}
}
fn add_opt_to_list(old: List(#(String, Dynamic)), key: String, value) {
old
|> list.append([#(key, dynamic.from(value))])
}