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
456
457
458
459
460
461
462
463
|
/*
* Copyright (C) Max Romanov
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
#include <nxt_port_rpc.h>
static nxt_atomic_t nxt_stream_ident = 1;
typedef struct nxt_port_rpc_reg_s nxt_port_rpc_reg_t;
struct nxt_port_rpc_reg_s {
uint32_t stream;
nxt_pid_t peer;
nxt_queue_link_t link;
nxt_bool_t link_first;
nxt_port_rpc_handler_t ready_handler;
nxt_port_rpc_handler_t error_handler;
void *data;
};
static void
nxt_port_rpc_remove_from_peers(nxt_task_t *task, nxt_port_t *port,
nxt_port_rpc_reg_t *reg);
static nxt_int_t
nxt_rpc_reg_test(nxt_lvlhsh_query_t *lhq, void *data)
{
return NXT_OK;
}
static const nxt_lvlhsh_proto_t lvlhsh_rpc_reg_proto nxt_aligned(64) = {
NXT_LVLHSH_DEFAULT,
nxt_rpc_reg_test,
nxt_lvlhsh_alloc,
nxt_lvlhsh_free,
};
nxt_inline void
nxt_port_rpc_lhq_stream(nxt_lvlhsh_query_t *lhq, uint32_t *stream)
{
lhq->key_hash = nxt_murmur_hash2(stream, sizeof(*stream));
lhq->key.length = sizeof(*stream);
lhq->key.start = (u_char *) stream;
lhq->proto = &lvlhsh_rpc_reg_proto;
}
nxt_inline void
nxt_port_rpc_lhq_peer(nxt_lvlhsh_query_t *lhq, nxt_pid_t *peer)
{
lhq->key_hash = nxt_murmur_hash2(peer, sizeof(*peer));
lhq->key.length = sizeof(*peer);
lhq->key.start = (u_char *) peer;
lhq->proto = &lvlhsh_rpc_reg_proto;
}
uint32_t
nxt_port_rpc_register_handler(nxt_task_t *task, nxt_port_t *port,
nxt_port_rpc_handler_t ready_handler, nxt_port_rpc_handler_t error_handler,
nxt_pid_t peer, void *data)
{
void *ex;
nxt_port_rpc_reg_t *reg;
ex = nxt_port_rpc_register_handler_ex(task, port, ready_handler,
error_handler, 0);
if (ex == NULL) {
return 0;
}
if (peer != -1) {
nxt_port_rpc_ex_set_peer(task, port, ex, peer);
}
reg = nxt_pointer_to(ex, -sizeof(nxt_port_rpc_reg_t));
nxt_assert(reg->data == ex);
reg->data = data;
return reg->stream;
}
void *
nxt_port_rpc_register_handler_ex(nxt_task_t *task, nxt_port_t *port,
nxt_port_rpc_handler_t ready_handler, nxt_port_rpc_handler_t error_handler,
size_t ex_size)
{
uint32_t stream;
nxt_port_rpc_reg_t *reg;
nxt_lvlhsh_query_t lhq;
nxt_assert(port->pair[0] != -1);
stream =
(uint32_t) nxt_atomic_fetch_add(&nxt_stream_ident, 1) & 0x3fffffff;
reg = nxt_mp_zalloc(port->mem_pool, sizeof(nxt_port_rpc_reg_t) + ex_size);
if (nxt_slow_path(reg == NULL)) {
nxt_debug(task, "rpc: stream #%uD failed to allocate reg", stream);
return NULL;
}
reg->stream = stream;
reg->peer = -1;
reg->ready_handler = ready_handler;
reg->error_handler = error_handler;
reg->data = reg + 1;
nxt_port_rpc_lhq_stream(&lhq, &stream);
lhq.replace = 0;
lhq.value = reg;
lhq.pool = port->mem_pool;
switch (nxt_lvlhsh_insert(&port->rpc_streams, &lhq)) {
case NXT_OK:
break;
default:
nxt_log_error(NXT_LOG_ERR, task->log, "rpc: stream #%uD failed to add "
"reg ", stream);
nxt_mp_free(port->mem_pool, reg);
return NULL;
}
nxt_debug(task, "rpc: stream #%uD registered", stream);
nxt_port_inc_use(port);
return reg->data;
}
uint32_t
nxt_port_rpc_ex_stream(void *ex)
{
nxt_port_rpc_reg_t *reg;
reg = nxt_pointer_to(ex, -sizeof(nxt_port_rpc_reg_t));
nxt_assert(reg->data == ex);
return reg->stream;
}
void
nxt_port_rpc_ex_set_peer(nxt_task_t *task, nxt_port_t *port,
void *ex, nxt_pid_t peer)
{
nxt_int_t ret;
nxt_queue_link_t *peer_link;
nxt_port_rpc_reg_t *reg;
nxt_lvlhsh_query_t lhq;
reg = nxt_pointer_to(ex, -sizeof(nxt_port_rpc_reg_t));
nxt_assert(reg->data == ex);
if (nxt_slow_path(peer == reg->peer)) {
return;
}
if (reg->peer != -1) {
nxt_port_rpc_remove_from_peers(task, port, reg);
reg->peer = -1;
}
if (peer == -1) {
return;
}
reg->peer = peer;
nxt_port_rpc_lhq_peer(&lhq, &peer);
lhq.replace = 0;
lhq.value = ®->link;
lhq.pool = port->mem_pool;
ret = nxt_lvlhsh_insert(&port->rpc_peers, &lhq);
switch (ret) {
case NXT_OK:
reg->link_first = 1;
nxt_queue_self(®->link);
nxt_debug(task, "rpc: stream #%uD assigned uniq pid %PI (%p)",
reg->stream, reg->peer, reg->link.next);
break;
case NXT_DECLINED:
reg->link_first = 0;
peer_link = lhq.value;
nxt_queue_insert_after(peer_link, ®->link);
nxt_debug(task, "rpc: stream #%uD assigned duplicate pid %PI (%p)",
reg->stream, reg->peer, reg->link.next);
break;
default:
nxt_log_error(NXT_LOG_ERR, task->log, "rpc: failed to add "
"peer for stream #%uD (%d)", reg->stream, ret);
reg->peer = -1;
break;
}
}
static void
nxt_port_rpc_remove_from_peers(nxt_task_t *task, nxt_port_t *port,
nxt_port_rpc_reg_t *reg)
{
uint32_t stream;
nxt_int_t ret;
nxt_lvlhsh_query_t lhq;
nxt_port_rpc_reg_t *r;
stream = reg->stream;
if (reg->link_first != 0) {
nxt_port_rpc_lhq_peer(&lhq, ®->peer);
lhq.pool = port->mem_pool;
if (reg->link.next == ®->link) {
nxt_assert(reg->link.prev == ®->link);
nxt_debug(task, "rpc: stream #%uD remove first and last pid %PI "
"registration (%p)", stream, reg->peer, reg->link.next);
ret = nxt_lvlhsh_delete(&port->rpc_peers, &lhq);
} else {
nxt_debug(task, "rpc: stream #%uD remove first pid %PI "
"registration (%p)", stream, reg->peer, reg->link.next);
lhq.replace = 1;
lhq.value = reg->link.next;
r = nxt_queue_link_data(reg->link.next, nxt_port_rpc_reg_t, link);
r->link_first = 1;
nxt_queue_remove(®->link);
ret = nxt_lvlhsh_insert(&port->rpc_peers, &lhq);
}
} else {
nxt_debug(task, "rpc: stream #%uD remove pid %PI "
"registration (%p)", stream, reg->peer, reg->link.next);
nxt_queue_remove(®->link);
ret = NXT_OK;
}
if (nxt_slow_path(ret != NXT_OK)) {
nxt_log_error(NXT_LOG_ERR, task->log, "rpc: stream #%uD failed"
" to delete peer %PI (%d)", stream, reg->peer, ret);
}
}
void
nxt_port_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
{
uint8_t last;
uint32_t stream;
nxt_int_t ret;
nxt_port_t *port;
nxt_port_rpc_reg_t *reg;
nxt_lvlhsh_query_t lhq;
nxt_port_msg_type_t type;
stream = msg->port_msg.stream;
port = msg->port;
last = msg->port_msg.last;
type = msg->port_msg.type;
nxt_port_rpc_lhq_stream(&lhq, &stream);
lhq.pool = port->mem_pool;
if (last != 0) {
ret = nxt_lvlhsh_delete(&port->rpc_streams, &lhq);
} else {
ret = nxt_lvlhsh_find(&port->rpc_streams, &lhq);
}
if (ret != NXT_OK) {
nxt_debug(task, "rpc: stream #%uD no handler found", stream);
return;
}
nxt_debug(task, "rpc: stream #%uD %shandler, type %d", stream,
(last ? "last " : ""), type);
reg = lhq.value;
if (type == _NXT_PORT_MSG_RPC_ERROR) {
reg->error_handler(task, msg, reg->data);
} else {
reg->ready_handler(task, msg, reg->data);
}
if (last == 0) {
return;
}
if (reg->peer != -1) {
nxt_port_rpc_remove_from_peers(task, port, reg);
}
nxt_debug(task, "rpc: stream #%uD free registration", stream);
nxt_mp_free(port->mem_pool, reg);
nxt_port_use(task, port, -1);
}
void
nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, nxt_pid_t peer)
{
uint8_t last;
uint32_t stream;
nxt_int_t ret;
nxt_buf_t buf;
nxt_queue_link_t *peer_link, *next_link;
nxt_port_rpc_reg_t *reg;
nxt_lvlhsh_query_t lhq;
nxt_port_recv_msg_t msg;
nxt_port_rpc_lhq_peer(&lhq, &peer);
lhq.pool = port->mem_pool;
ret = nxt_lvlhsh_delete(&port->rpc_peers, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
nxt_debug(task, "rpc: no reg found for peer %PI", peer);
return;
}
nxt_memzero(&msg, sizeof(msg));
nxt_memzero(&buf, sizeof(buf));
msg.fd = -1;
msg.buf = &buf;
msg.port = port;
msg.port_msg.pid = peer;
msg.port_msg.type = _NXT_PORT_MSG_REMOVE_PID;
peer_link = lhq.value;
last = 0;
while (last == 0) {
reg = nxt_queue_link_data(peer_link, nxt_port_rpc_reg_t, link);
nxt_assert(reg->peer == peer);
stream = reg->stream;
nxt_debug(task, "rpc: stream #%uD trigger error", stream);
msg.port_msg.stream = stream;
msg.port_msg.last = 1;
if (peer_link == peer_link->next) {
nxt_assert(peer_link->prev == peer_link);
last = 1;
} else {
nxt_assert(peer_link->next->prev == peer_link);
nxt_assert(peer_link->prev->next == peer_link);
next_link = peer_link->next;
nxt_queue_remove(peer_link);
peer_link = next_link;
}
reg->peer = -1;
reg->error_handler(task, &msg, reg->data);
/* Reset 'last' flag to preserve rpc handler. */
if (msg.port_msg.last == 0) {
continue;
}
nxt_port_rpc_lhq_stream(&lhq, &stream);
lhq.pool = port->mem_pool;
ret = nxt_lvlhsh_delete(&port->rpc_streams, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
nxt_log_error(NXT_LOG_ERR, task->log,
"rpc: stream #%uD failed to delete handler", stream);
return;
}
nxt_mp_free(port->mem_pool, reg);
nxt_port_use(task, port, -1);
}
}
void
nxt_port_rpc_cancel(nxt_task_t *task, nxt_port_t *port, uint32_t stream)
{
nxt_int_t ret;
nxt_port_rpc_reg_t *reg;
nxt_lvlhsh_query_t lhq;
nxt_port_rpc_lhq_stream(&lhq, &stream);
lhq.pool = port->mem_pool;
ret = nxt_lvlhsh_delete(&port->rpc_streams, &lhq);
if (ret != NXT_OK) {
nxt_debug(task, "rpc: stream #%uD no handler found", stream);
return;
}
reg = lhq.value;
if (reg->peer != -1) {
nxt_port_rpc_remove_from_peers(task, port, reg);
}
nxt_debug(task, "rpc: stream #%uD cancel registration", stream);
nxt_mp_free(port->mem_pool, reg);
nxt_port_use(task, port, -1);
}
|