|
1 | 1 | /* Copyright (C) 2016 NooBaa */ |
2 | | -/** |
3 | | - * |
4 | | - * REDIRECTOR |
5 | | - * |
6 | | - */ |
7 | 2 | 'use strict'; |
8 | 3 |
|
9 | | -const _ = require('lodash'); |
10 | 4 | const P = require('../../util/promise'); |
11 | 5 | const dbg = require('../../util/debug_module')(__filename); |
12 | 6 | const server_rpc = require('../server_rpc'); |
| 7 | +const RpcConnSet = require('../../rpc/rpc_conn_set'); |
13 | 8 |
|
14 | | -// dbg.set_level(5); |
15 | | - |
16 | | -const cluster_connections = new Set(); |
17 | | -const alerts_connections = new Set(); |
| 9 | +const cluster_conn_set = new RpcConnSet('redirector cluster_conn_set'); |
| 10 | +const alerts_conn_set = new RpcConnSet('redirector alerts_conn_set'); |
18 | 11 |
|
19 | 12 | function register_to_cluster(req) { |
20 | | - var conn = req.connection; |
21 | | - if (!cluster_connections.has(conn)) { |
22 | | - dbg.log0('register_to_cluster', conn.url.href); |
23 | | - cluster_connections.add(conn); |
24 | | - conn.on('close', function() { |
25 | | - cluster_connections.delete(conn); |
26 | | - }); |
27 | | - } |
| 13 | + cluster_conn_set.add(req.connection); |
28 | 14 | } |
29 | 15 |
|
30 | 16 | function publish_to_cluster(req) { |
31 | | - var api_name = req.rpc_params.method_api.slice(0, -4); // remove _api suffix |
32 | | - var method = req.rpc_params.method_name; |
33 | | - var addresses = ['fcall://fcall']; // also call on myself |
34 | | - cluster_connections.forEach(function(conn) { |
35 | | - addresses.push(conn.url.href); |
36 | | - }); |
37 | | - addresses = _.uniq(addresses); |
38 | | - dbg.log0('publish_to_cluster:', addresses); |
39 | | - return P.map(addresses, function(address) { |
40 | | - return server_rpc.client[api_name][method](req.rpc_params.request_params, { |
41 | | - address: address, |
| 17 | + const api_name = req.rpc_params.method_api.slice(0, -4); // remove _api suffix |
| 18 | + const method = req.rpc_params.method_name; |
| 19 | + const connections = cluster_conn_set.list(); |
| 20 | + dbg.log0('publish_to_cluster:', connections.length); |
| 21 | + return P.map(connections, |
| 22 | + conn => server_rpc.client[api_name][method](req.rpc_params.request_params, { |
| 23 | + connection: conn, |
42 | 24 | auth_token: req.auth_token, |
43 | | - }); |
44 | | - }) |
45 | | - .then(function(res) { |
46 | | - return { |
47 | | - redirect_reply: { |
48 | | - aggregated: res, |
49 | | - } |
50 | | - }; |
51 | | - }); |
| 25 | + }) |
| 26 | + ) |
| 27 | + .then(res => ({ |
| 28 | + redirect_reply: { |
| 29 | + aggregated: res, |
| 30 | + } |
| 31 | + })); |
52 | 32 | } |
53 | 33 |
|
54 | 34 | function register_for_alerts(req) { |
55 | | - var conn = req.connection; |
56 | | - if (!alerts_connections.has(conn)) { |
57 | | - dbg.log0('register_for_alerts', conn.url.href); |
58 | | - alerts_connections.add(conn); |
59 | | - conn.on('close', function() { |
60 | | - alerts_connections.delete(conn); |
61 | | - }); |
62 | | - } |
| 35 | + alerts_conn_set.add(req.connection); |
63 | 36 | } |
64 | 37 |
|
65 | 38 | function unregister_from_alerts(req) { |
66 | | - var conn = req.connection; |
67 | | - if (!alerts_connections.has(conn)) { |
68 | | - return; |
69 | | - } |
70 | | - alerts_connections.delete(conn); |
| 39 | + alerts_conn_set.remove(req.connection); |
71 | 40 | } |
72 | 41 |
|
73 | 42 | function publish_alerts(req) { |
74 | | - var connections = []; |
75 | | - alerts_connections.forEach(function(conn) { |
76 | | - connections.push(conn); |
77 | | - }); |
78 | | - connections = _.uniq(connections); |
79 | | - dbg.log3('publish_alerts:', req.rpc_params.request_params, connections); |
80 | | - return P.map(connections, function(conn) { |
81 | | - return server_rpc.client.frontend_notifications.alert(req.rpc_params.request_params, { |
| 43 | + const connections = alerts_conn_set.list(); |
| 44 | + dbg.log3('publish_alerts:', req.rpc_params.request_params, connections.length); |
| 45 | + return P.map(connections, conn => |
| 46 | + server_rpc.client.frontend_notifications.alert(req.rpc_params.request_params, { |
82 | 47 | connection: conn, |
83 | | - }); |
84 | | - }) |
| 48 | + }) |
| 49 | + ) |
85 | 50 | .then(() => { |
86 | 51 | dbg.log3('published'); |
87 | 52 | }); |
|
0 commit comments