forked from senecajs/seneca-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.js
238 lines (170 loc) · 5.67 KB
/
mesh.js
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
/*
MIT License,
Copyright (c) 2015-2016, Richard Rodger and other contributors.
*/
'use strict'
var _ = require('lodash')
var Jsonic = require('jsonic')
var Sneeze = require('sneeze')
var Nid = require('nid')
module.exports = function mesh (options) {
var seneca = this
var balance_map = {}
var mid = Nid()
options = seneca.util.deepextend({
auto: true,
make_entry: default_make_entry
}, options)
// options.base is to deprecated
var isbase = !!(options.isbase || options.base)
var pin = options.pin || options.pins
if( isbase ) {
pin = pin || 'role:mesh,base:true'
}
// options.remotes is to deprecated
var bases = options.bases || options.remotes
var tag = options.tag
var sneeze_opts = options.sneeze || {}
sneeze_opts.isbase = sneeze_opts.isbase || isbase
sneeze_opts.bases = sneeze_opts.bases || bases || void 0
sneeze_opts.port = sneeze_opts.port || options.port || void 0
sneeze_opts.host = sneeze_opts.host || options.host || void 0
sneeze_opts.identifier = sneeze_opts.identifier || seneca.id
sneeze_opts.tag
= ( void 0 !== sneeze_opts.tag ? sneeze_opts.tag :
void 0 !== tag ?
(null === tag ? null : 'seneca~'+tag)
: 'seneca~mesh' )
var listen = options.listen || [{pin:pin, model:options.model||'actor'}]
var balance_client_opts = options.balance_client || {}
seneca.use( 'balance-client$mesh~'+mid, balance_client_opts )
seneca.add( 'role:transport,cmd:listen', transport_listen )
// call seneca.listen as a convenience
// subsequent seneca.listen calls will still publish to network
if( options.auto ) {
_.each( listen, function( listen_opts ) {
listen_opts.port = null != listen_opts.port ? listen_opts.port : function() {
return 50000 + Math.floor((10000*Math.random()))
}
listen_opts.model = listen_opts.model || 'actor'
seneca.root.listen( listen_opts )
})
}
function transport_listen (msg, done) {
this.prior( msg, function( err, out ) {
if( err ) return done( err )
join( this, out, function() {
done()
})
})
}
function join( instance, config, done ) {
config = config || {}
if( !config.pin && !config.pins ) {
config.pin = 'null:true'
}
var instance_sneeze_opts = _.clone( sneeze_opts )
instance_sneeze_opts.identifier =
sneeze_opts.identifier + '~' +
seneca.util.pincanon(config.pin||config.pins) + '~' + Date.now()
var sneeze = Sneeze( instance_sneeze_opts )
var meta = {
config: config,
instance: instance.id,
}
sneeze.on('error', function(err) {
seneca.log.warn(err)
})
sneeze.on('add', add_client)
sneeze.on('remove', remove_client)
sneeze.on('ready', done)
seneca.add('role:seneca,cmd:close', function(msg, done) {
if( sneeze ) {
sneeze.leave()
}
this.prior(msg, done)
})
seneca.add( 'role:mesh,get:members', function get_members (msg, done) {
var members = []
_.each( sneeze.members(), function(member) {
var m = options.make_entry(member)
members.push( void 0 === m ? default_make_entry(member) : m )
})
this.prior( msg, function( err, list ) {
list = list || []
var outlist = list.concat(members)
done( null, outlist )
})
})
sneeze.join( meta )
function add_client( meta ) {
var config = meta.config || {}
var pins = config.pins || config.pin || []
pins = _.isArray(pins) ? pins : [pins]
_.each( pins, function( pin ) {
var pin_id = instance.util.pattern(pin)
var has_balance_client = !!balance_map[pin_id]
var target_map = (balance_map[pin_id] = balance_map[pin_id] || {})
var pin_config = _.clone( config )
delete pin_config.pins
delete pin_config.pin
pin_config.pin = pin_id
var id = instance.util.pattern( pin_config )+'~'+meta.identifier$
// this is a duplicate, so ignore
if( target_map[id] ) {
return
}
pin_config.id = id
// TODO: how to handle local override?
var actmeta = instance.find( pin )
var ignore_client = !!(actmeta && !actmeta.client)
if( ignore_client ) {
return
}
if( !has_balance_client ) {
// no balancer for this pin, so add one
instance.root.client( {type:'balance', pin:pin, model:config.model} )
}
target_map[id] = true
instance.act(
'role:transport,type:balance,add:client',
{config:pin_config} )
})
}
function remove_client( meta ) {
var config = meta.config || {}
var pins = config.pins || config.pin || []
pins = _.isArray(pins) ? pins : [pins]
_.each( pins, function( pin ) {
var pin_id = instance.util.pattern(pin)
var pin_config = _.clone( config )
delete pin_config.pins
delete pin_config.pin
pin_config.pin = pin_id
var id = instance.util.pattern( pin_config )+'~'+meta.identifier$
pin_config.id = id
var target_map = balance_map[pin_id]
if( target_map ) {
delete target_map[id]
}
instance.act(
'role:transport,type:balance,remove:client',
{config:pin_config} )
})
}
}
function default_make_entry (member) {
var entry = member
if( member.tag$.match(/^seneca~/) ) {
entry = {
pin: member.config.pin,
port: member.config.port,
host: member.config.host,
type: member.config.type,
model: member.config.model,
instance: member.instance
}
}
return entry
}
}