@weave-js/core 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -1
- package/jest.config.js +1 -1
- package/lib/ExtendableError.js +7 -7
- package/lib/broker/context.js +49 -49
- package/lib/broker/defaultOptions.js +8 -9
- package/lib/broker/index.js +160 -164
- package/lib/buildRuntime.js +34 -34
- package/lib/cache/adapters/base.js +50 -74
- package/lib/cache/adapters/inMemory.js +58 -58
- package/lib/cache/adapters/inMemoryLru.js +57 -57
- package/lib/cache/adapters/index.js +3 -39
- package/lib/cache/getCacheKeyByObject.js +31 -0
- package/lib/cache/getPropertyFromDataOrMetadata.js +20 -0
- package/lib/cache/lock.js +30 -24
- package/lib/constants.js +5 -5
- package/lib/errorHandler.js +8 -8
- package/lib/errors.js +35 -35
- package/lib/helper/defineAction.js +2 -2
- package/lib/helper/defineBrokerOptions.js +2 -2
- package/lib/helper/defineService.js +2 -2
- package/lib/index.js +21 -21
- package/lib/logger/base.js +38 -29
- package/lib/logger/format/asHumanReadable.js +15 -15
- package/lib/logger/format/asJson.js +10 -10
- package/lib/logger/format/index.js +3 -3
- package/lib/logger/index.js +24 -24
- package/lib/logger/levels.js +21 -21
- package/lib/logger/tools.js +15 -15
- package/lib/logger/utils/colorize.js +23 -23
- package/lib/logger/utils/format.js +75 -65
- package/lib/metrics/common.js +47 -47
- package/lib/metrics/constants.js +44 -44
- package/lib/metrics/exporter/base.js +8 -8
- package/lib/metrics/exporter/event.js +20 -20
- package/lib/metrics/exporter/index.js +15 -15
- package/lib/metrics/index.js +2 -2
- package/lib/metrics/types/base.js +20 -20
- package/lib/metrics/types/counter.js +6 -6
- package/lib/metrics/types/gauge.js +24 -24
- package/lib/metrics/types/histogram.js +26 -26
- package/lib/metrics/types/index.js +6 -6
- package/lib/metrics/types/info.js +17 -17
- package/lib/middlewares/action-hooks/index.js +38 -38
- package/lib/middlewares/bulkhead/index.js +34 -34
- package/lib/middlewares/cache/index.js +37 -37
- package/lib/middlewares/circuit-breaker/index.js +62 -62
- package/lib/middlewares/context-tracker/index.js +39 -39
- package/lib/middlewares/error-handler/index.js +14 -14
- package/lib/middlewares/index.js +2 -2
- package/lib/middlewares/metrics/getMiddlewareWrapper.js +17 -17
- package/lib/middlewares/metrics/index.js +29 -29
- package/lib/middlewares/retry/index.js +15 -15
- package/lib/middlewares/timeout/index.js +16 -16
- package/lib/middlewares/tracing/index.js +37 -37
- package/lib/middlewares/tracing/tags.js +6 -6
- package/lib/middlewares/validator/index.js +31 -31
- package/lib/registry/actionEndpoint.js +8 -8
- package/lib/registry/collections/actionCollection.js +39 -35
- package/lib/registry/collections/endpointCollection.js +63 -63
- package/lib/registry/collections/eventCollection.js +62 -62
- package/lib/registry/collections/nodeCollection.js +45 -45
- package/lib/registry/collections/serviceCollection.js +61 -55
- package/lib/registry/eventEndpoint.js +14 -14
- package/lib/registry/load-balancer/base.js +3 -3
- package/lib/registry/load-balancer/index.js +7 -7
- package/lib/registry/node.js +27 -27
- package/lib/registry/registry.js +139 -136
- package/lib/registry/service/parseAction.js +16 -16
- package/lib/registry/service/parseEvent.js +18 -18
- package/lib/registry/service/reduceMixins.js +10 -10
- package/lib/registry/service/service.js +72 -72
- package/lib/registry/serviceItem.js +21 -21
- package/lib/runtime/initActionInvoker.js +21 -21
- package/lib/runtime/initCache.js +7 -7
- package/lib/runtime/initContextFactory.js +22 -22
- package/lib/runtime/initEventbus.js +49 -49
- package/lib/runtime/initLogger.js +11 -11
- package/lib/runtime/initMetrics.js +54 -54
- package/lib/runtime/initMiddlewareManager.js +21 -21
- package/lib/runtime/initRegistry.js +6 -6
- package/lib/runtime/initServiceManager.js +85 -68
- package/lib/runtime/initTracing.js +23 -23
- package/lib/runtime/initTransport.js +5 -5
- package/lib/runtime/initUuidFactory.js +4 -4
- package/lib/runtime/initValidator.js +5 -5
- package/lib/tracing/collectors/base.js +22 -22
- package/lib/tracing/collectors/event.js +33 -33
- package/lib/tracing/collectors/index.js +15 -15
- package/lib/tracing/span.js +26 -26
- package/lib/tracing/time.js +8 -8
- package/lib/transport/InboundTransformStream.js +26 -26
- package/lib/transport/adapters/adapteBase.js +30 -30
- package/lib/transport/adapters/adapters.js +4 -4
- package/lib/transport/adapters/dummy/index.js +18 -18
- package/lib/transport/adapters/fromURI.js +13 -13
- package/lib/transport/adapters/getAdapterByName.js +5 -5
- package/lib/transport/adapters/index.js +12 -12
- package/lib/transport/adapters/tcp/discovery/codec.js +4 -4
- package/lib/transport/adapters/tcp/discovery/index.js +62 -62
- package/lib/transport/adapters/tcp/index.js +169 -171
- package/lib/transport/adapters/tcp/tcp-messagetypes.js +7 -7
- package/lib/transport/adapters/tcp/tcpReader.js +39 -39
- package/lib/transport/adapters/tcp/tcpWriteStream.js +26 -26
- package/lib/transport/adapters/tcp/tcpWriter.js +57 -57
- package/lib/transport/adapters/tcp/utils.js +12 -12
- package/lib/transport/createMessage.js +4 -4
- package/lib/transport/createTransport.js +254 -254
- package/lib/transport/errorPayloadFactory.js +14 -17
- package/lib/transport/messageHandlers.js +200 -200
- package/lib/transport/messageTypes.js +19 -19
- package/lib/types.js +28 -15
- package/lib/utils/index.js +1 -1
- package/lib/utils/options.js +44 -44
- package/lib/utils/restoreError.js +4 -4
- package/package.json +4 -4
- package/lib/cache/base.js +0 -136
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Author: Kevin Ries (kevin@
|
|
2
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
3
3
|
* -----
|
|
4
4
|
* Copyright 2021 Fachwerk
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const { WeaveParameterValidationError } = require('../../errors')
|
|
8
|
-
const { capitalize } = require('@weave-js/utils')
|
|
7
|
+
const { WeaveParameterValidationError } = require('../../errors');
|
|
8
|
+
const { capitalize } = require('@weave-js/utils');
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* @typedef {import('../../types').Runtime} Runtime
|
|
@@ -18,77 +18,77 @@ const { capitalize } = require('@weave-js/utils')
|
|
|
18
18
|
* @returns {Middleware} - Validator middleware
|
|
19
19
|
*/
|
|
20
20
|
module.exports = (runtime) => {
|
|
21
|
-
const validator = runtime.validator
|
|
21
|
+
const validator = runtime.validator;
|
|
22
22
|
|
|
23
23
|
const processErrors = (context, type, results) => {
|
|
24
|
-
const errors = results.map(data => Object.assign(data, { nodeId: context.nodeId, action: context.action.name }))
|
|
25
|
-
return Promise.reject(new WeaveParameterValidationError(`${capitalize(type)} parameter validation error`, errors))
|
|
26
|
-
}
|
|
24
|
+
const errors = results.map(data => Object.assign(data, { nodeId: context.nodeId, action: context.action.name }));
|
|
25
|
+
return Promise.reject(new WeaveParameterValidationError(`${capitalize(type)} parameter validation error`, errors));
|
|
26
|
+
};
|
|
27
27
|
|
|
28
28
|
return {
|
|
29
29
|
localAction (handler, action) {
|
|
30
30
|
const parameterOptions = Object.assign(
|
|
31
31
|
runtime.options.validatorOptions,
|
|
32
32
|
action.validatorOptions
|
|
33
|
-
)
|
|
33
|
+
);
|
|
34
34
|
|
|
35
35
|
// validate request schema
|
|
36
|
-
let validateRequestSchema
|
|
37
|
-
let validateResponseSchema
|
|
36
|
+
let validateRequestSchema;
|
|
37
|
+
let validateResponseSchema;
|
|
38
38
|
if (action.params && typeof action.params === 'object') {
|
|
39
|
-
validateRequestSchema = validator.compile(action.params, parameterOptions)
|
|
39
|
+
validateRequestSchema = validator.compile(action.params, parameterOptions);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
if (action.responseSchema && typeof action.responseSchema === 'object') {
|
|
43
|
-
validateResponseSchema = validator.compile(action.responseSchema, parameterOptions)
|
|
43
|
+
validateResponseSchema = validator.compile(action.responseSchema, parameterOptions);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
if (!validateRequestSchema && !validateRequestSchema) {
|
|
47
|
-
return handler
|
|
47
|
+
return handler;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
return (context, serviceInjections) => {
|
|
51
|
-
const requestSchemaResult = validateRequestSchema ? validateRequestSchema(context.data) : true
|
|
51
|
+
const requestSchemaResult = validateRequestSchema ? validateRequestSchema(context.data) : true;
|
|
52
52
|
|
|
53
53
|
if (requestSchemaResult === true) {
|
|
54
54
|
return handler(context, serviceInjections)
|
|
55
55
|
.then((result) => {
|
|
56
56
|
if (validateResponseSchema) {
|
|
57
|
-
const responseSchemaResult = validateResponseSchema(result)
|
|
57
|
+
const responseSchemaResult = validateResponseSchema(result);
|
|
58
58
|
if (responseSchemaResult === true) {
|
|
59
|
-
return result
|
|
59
|
+
return result;
|
|
60
60
|
}
|
|
61
|
-
return processErrors(context, 'response', responseSchemaResult)
|
|
61
|
+
return processErrors(context, 'response', responseSchemaResult);
|
|
62
62
|
}
|
|
63
|
-
return result
|
|
64
|
-
})
|
|
63
|
+
return result;
|
|
64
|
+
});
|
|
65
65
|
} else {
|
|
66
66
|
// Enriching the validator errors with some useful information
|
|
67
|
-
return processErrors(context, 'request', requestSchemaResult)
|
|
67
|
+
return processErrors(context, 'request', requestSchemaResult);
|
|
68
68
|
}
|
|
69
|
-
}
|
|
69
|
+
};
|
|
70
70
|
},
|
|
71
71
|
localEvent (handler, event) {
|
|
72
72
|
if (event.params && typeof event.params === 'object') {
|
|
73
73
|
const parameterOptions = Object.assign(
|
|
74
74
|
runtime.options.validatorOptions,
|
|
75
75
|
event.validatorOptions
|
|
76
|
-
)
|
|
76
|
+
);
|
|
77
77
|
|
|
78
|
-
const validate = validator.compile(event.params, parameterOptions)
|
|
78
|
+
const validate = validator.compile(event.params, parameterOptions);
|
|
79
79
|
|
|
80
80
|
return (context, serviceInjections) => {
|
|
81
|
-
let result = validate(context.data)
|
|
81
|
+
let result = validate(context.data);
|
|
82
82
|
|
|
83
83
|
if (result === true) {
|
|
84
|
-
return handler(context, serviceInjections)
|
|
84
|
+
return handler(context, serviceInjections);
|
|
85
85
|
} else {
|
|
86
|
-
result = result.map(data => Object.assign(data, { nodeId: context.nodeId, event: context.eventName }))
|
|
87
|
-
return Promise.reject(new WeaveParameterValidationError('Parameter validation error', result))
|
|
86
|
+
result = result.map(data => Object.assign(data, { nodeId: context.nodeId, event: context.eventName }));
|
|
87
|
+
return Promise.reject(new WeaveParameterValidationError('Parameter validation error', result));
|
|
88
88
|
}
|
|
89
|
-
}
|
|
89
|
+
};
|
|
90
90
|
}
|
|
91
|
-
return handler
|
|
91
|
+
return handler;
|
|
92
92
|
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/*
|
|
10
|
-
* Author: Kevin Ries (kevin@
|
|
10
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
11
11
|
* -----
|
|
12
12
|
* Copyright 2021 Fachwerk
|
|
13
13
|
*/
|
|
@@ -31,15 +31,15 @@ exports.createActionEndpoint = (runtime, node, service, action) => {
|
|
|
31
31
|
isLocal: node.id === runtime.nodeId,
|
|
32
32
|
state: true,
|
|
33
33
|
name: `${node.id}:${action.name}`
|
|
34
|
-
}
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
endpoint.updateAction = (newAction) => {
|
|
37
|
-
endpoint.action = newAction
|
|
38
|
-
}
|
|
37
|
+
endpoint.action = newAction;
|
|
38
|
+
};
|
|
39
39
|
|
|
40
40
|
endpoint.isAvailable = () => {
|
|
41
|
-
return endpoint.state === true
|
|
42
|
-
}
|
|
41
|
+
return endpoint.state === true;
|
|
42
|
+
};
|
|
43
43
|
|
|
44
|
-
return endpoint
|
|
45
|
-
}
|
|
44
|
+
return endpoint;
|
|
45
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
|
-
* Author: Kevin Ries (kevin@
|
|
4
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
5
5
|
* -----
|
|
6
6
|
* Copyright 2021 Fachwerk
|
|
7
7
|
*/
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* @typedef {import('../../types.js').Registry} Registry
|
|
11
11
|
* @typedef {import('../../types.js').ServiceActionCollection} ServiceActionCollection
|
|
12
12
|
*/
|
|
13
|
-
const { omit } = require('@weave-js/utils')
|
|
14
|
-
const { createEndpointList } = require('./endpointCollection')
|
|
13
|
+
const { omit } = require('@weave-js/utils');
|
|
14
|
+
const { createEndpointList } = require('./endpointCollection');
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Configuration object for weave service broker.
|
|
@@ -29,47 +29,51 @@ exports.createActionCollection = (registry) => {
|
|
|
29
29
|
/**
|
|
30
30
|
* @type {ServiceActionCollection}
|
|
31
31
|
*/
|
|
32
|
-
const actionCollection = Object.create(null)
|
|
33
|
-
const { runtime } = registry
|
|
34
|
-
const actions = new Map()
|
|
32
|
+
const actionCollection = Object.create(null);
|
|
33
|
+
const { runtime } = registry;
|
|
34
|
+
const actions = new Map();
|
|
35
35
|
|
|
36
36
|
actionCollection.add = (node, service, action) => {
|
|
37
|
-
let endPointList = actions.get(action.name)
|
|
37
|
+
let endPointList = actions.get(action.name);
|
|
38
38
|
if (!endPointList) {
|
|
39
|
-
endPointList = createEndpointList(runtime, action.name)
|
|
40
|
-
actions.set(action.name, endPointList)
|
|
39
|
+
endPointList = createEndpointList(runtime, action.name);
|
|
40
|
+
actions.set(action.name, endPointList);
|
|
41
41
|
}
|
|
42
|
-
return endPointList.add(node, service, action)
|
|
43
|
-
}
|
|
42
|
+
return endPointList.add(node, service, action);
|
|
43
|
+
};
|
|
44
44
|
|
|
45
45
|
actionCollection.get = (actionName) => {
|
|
46
|
-
return actions.get(actionName)
|
|
47
|
-
}
|
|
46
|
+
return actions.get(actionName);
|
|
47
|
+
};
|
|
48
48
|
|
|
49
49
|
actionCollection.removeByService = (service) => {
|
|
50
50
|
actions.forEach(list => {
|
|
51
|
-
list.removeByService(service)
|
|
52
|
-
})
|
|
53
|
-
}
|
|
51
|
+
list.removeByService(service);
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
54
|
|
|
55
55
|
actionCollection.remove = (actionName, node) => {
|
|
56
56
|
// todo: switch property order
|
|
57
|
-
const endpoints = actions.get(actionName)
|
|
57
|
+
const endpoints = actions.get(actionName);
|
|
58
58
|
if (endpoints) {
|
|
59
|
-
endpoints.removeByNodeId(node.id)
|
|
59
|
+
endpoints.removeByNodeId(node.id);
|
|
60
60
|
}
|
|
61
|
-
}
|
|
61
|
+
};
|
|
62
62
|
|
|
63
|
-
actionCollection.list = ({
|
|
64
|
-
|
|
63
|
+
actionCollection.list = ({
|
|
64
|
+
onlyLocals = false,
|
|
65
|
+
skipInternals = false,
|
|
66
|
+
withEndpoints = false
|
|
67
|
+
} = {}) => {
|
|
68
|
+
const result = [];
|
|
65
69
|
|
|
66
70
|
actions.forEach(action => {
|
|
67
71
|
if (skipInternals && /^\$node/.test(action.name)) {
|
|
68
|
-
return
|
|
72
|
+
return;
|
|
69
73
|
}
|
|
70
74
|
|
|
71
75
|
if (onlyLocals && !action.hasLocal()) {
|
|
72
|
-
return
|
|
76
|
+
return;
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
// todo: don't create an new object
|
|
@@ -78,17 +82,17 @@ exports.createActionCollection = (registry) => {
|
|
|
78
82
|
hasAvailable: action.hasAvailable(),
|
|
79
83
|
hasLocal: action.hasLocal(),
|
|
80
84
|
count: action.count()
|
|
81
|
-
}
|
|
85
|
+
};
|
|
82
86
|
|
|
83
87
|
if (item.count > 0) {
|
|
84
|
-
const endpoint = action.endpoints[0]
|
|
88
|
+
const endpoint = action.endpoints[0];
|
|
85
89
|
if (endpoint) {
|
|
86
|
-
item.action = omit(endpoint.action, ['handler', 'service'])
|
|
90
|
+
item.action = omit(endpoint.action, ['handler', 'service']);
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
if (item.action == null || item.action.protected) {
|
|
91
|
-
return
|
|
95
|
+
return;
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
if (withEndpoints) {
|
|
@@ -96,14 +100,14 @@ exports.createActionCollection = (registry) => {
|
|
|
96
100
|
return {
|
|
97
101
|
nodeId: endpoint.node.id,
|
|
98
102
|
state: endpoint.state
|
|
99
|
-
}
|
|
100
|
-
})
|
|
103
|
+
};
|
|
104
|
+
});
|
|
101
105
|
}
|
|
102
106
|
|
|
103
|
-
result.push(item)
|
|
104
|
-
})
|
|
105
|
-
return result
|
|
106
|
-
}
|
|
107
|
+
result.push(item);
|
|
108
|
+
});
|
|
109
|
+
return result;
|
|
110
|
+
};
|
|
107
111
|
|
|
108
|
-
return actionCollection
|
|
109
|
-
}
|
|
112
|
+
return actionCollection;
|
|
113
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
|
-
* Author: Kevin Ries (kevin@
|
|
4
|
+
* Author: Kevin Ries (kevin.ries@fachwerk.io)
|
|
5
5
|
* -----
|
|
6
6
|
* Copyright 2021 Fachwerk
|
|
7
7
|
*/
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
* @typedef {import('../../types.js').EndpointCollection} EndpointCollection
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
const { createActionEndpoint } = require('../actionEndpoint')
|
|
18
|
-
const { loadBalancingStrategy } = require('../../constants')
|
|
17
|
+
const { createActionEndpoint } = require('../actionEndpoint');
|
|
18
|
+
const { loadBalancingStrategy } = require('../../constants');
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
*
|
|
@@ -26,133 +26,133 @@ const { loadBalancingStrategy } = require('../../constants')
|
|
|
26
26
|
*/
|
|
27
27
|
exports.createEndpointList = (runtime, name, groupName) => {
|
|
28
28
|
/** @type {EndpointCollection} */
|
|
29
|
-
const endpointList = Object.create(null)
|
|
30
|
-
const options = runtime.options
|
|
29
|
+
const endpointList = Object.create(null);
|
|
30
|
+
const options = runtime.options;
|
|
31
31
|
/** @type {Array} */
|
|
32
|
-
const list = endpointList.endpoints = []
|
|
32
|
+
const list = endpointList.endpoints = [];
|
|
33
33
|
|
|
34
|
-
let counter = 0
|
|
34
|
+
let counter = 0;
|
|
35
35
|
|
|
36
|
-
endpointList.name = name
|
|
37
|
-
endpointList.groupName = groupName
|
|
38
|
-
endpointList.isInternal = name.startsWith('$')
|
|
39
|
-
endpointList.localEndpoints = []
|
|
36
|
+
endpointList.name = name;
|
|
37
|
+
endpointList.groupName = groupName;
|
|
38
|
+
endpointList.isInternal = name.startsWith('$');
|
|
39
|
+
endpointList.localEndpoints = [];
|
|
40
40
|
|
|
41
41
|
const setLocalEndpoints = () => {
|
|
42
|
-
endpointList.localEndpoints = list.filter(endpoint => endpoint.isLocal)
|
|
43
|
-
}
|
|
42
|
+
endpointList.localEndpoints = list.filter(endpoint => endpoint.isLocal);
|
|
43
|
+
};
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Select an Entpoint with the selected Load-Balancing-Strategy
|
|
47
47
|
* @param {*} endpointList List of all available Endpoints
|
|
48
48
|
* @returns {any} Endpoint
|
|
49
|
-
|
|
49
|
+
*/
|
|
50
50
|
const select = (endpointList) => {
|
|
51
|
-
//
|
|
51
|
+
// Round robin
|
|
52
52
|
if (options.registry.loadBalancingStrategy === loadBalancingStrategy.ROUND_ROBIN) {
|
|
53
53
|
if (counter >= endpointList.length) {
|
|
54
|
-
counter = 0
|
|
54
|
+
counter = 0;
|
|
55
55
|
}
|
|
56
|
-
const res = endpointList[counter++]
|
|
57
|
-
return res
|
|
56
|
+
const res = endpointList[counter++];
|
|
57
|
+
return res;
|
|
58
58
|
} else {
|
|
59
|
-
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
|
|
60
|
-
return endpointList[randomInt(0, endpointList.length - 1)]
|
|
59
|
+
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
|
|
60
|
+
return endpointList[randomInt(0, endpointList.length - 1)];
|
|
61
61
|
// todo: implement random load balancer
|
|
62
62
|
}
|
|
63
|
-
}
|
|
63
|
+
};
|
|
64
64
|
|
|
65
65
|
endpointList.add = (node, service, action) => { // todo: addaction
|
|
66
|
-
const foundEndpoint = list.find(endpoint => endpoint.node.id === node.id && endpoint.service.name === service.name)
|
|
66
|
+
const foundEndpoint = list.find(endpoint => endpoint.node.id === node.id && endpoint.service.name === service.name);
|
|
67
67
|
|
|
68
68
|
if (foundEndpoint) {
|
|
69
|
-
foundEndpoint.updateAction(action)
|
|
70
|
-
return false
|
|
69
|
+
foundEndpoint.updateAction(action);
|
|
70
|
+
return false;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
const newEndpoint = createActionEndpoint(runtime, node, service, action)
|
|
73
|
+
const newEndpoint = createActionEndpoint(runtime, node, service, action);
|
|
74
74
|
|
|
75
|
-
list.push(newEndpoint)
|
|
76
|
-
setLocalEndpoints()
|
|
77
|
-
return true
|
|
78
|
-
}
|
|
75
|
+
list.push(newEndpoint);
|
|
76
|
+
setLocalEndpoints();
|
|
77
|
+
return true;
|
|
78
|
+
};
|
|
79
79
|
|
|
80
|
-
endpointList.hasAvailable = () => list.find(endpoint => endpoint.isAvailable()) != null
|
|
80
|
+
endpointList.hasAvailable = () => list.find(endpoint => endpoint.isAvailable()) != null;
|
|
81
81
|
|
|
82
|
-
endpointList.hasLocal = () => endpointList.localEndpoints.length > 0
|
|
82
|
+
endpointList.hasLocal = () => endpointList.localEndpoints.length > 0;
|
|
83
83
|
|
|
84
84
|
endpointList.getNextAvailableEndpoint = () => {
|
|
85
85
|
if (list.length === 0) {
|
|
86
|
-
return null
|
|
86
|
+
return null;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
// If there is a local service, get a local endpoint
|
|
90
90
|
if (endpointList.isInternal && endpointList.hasLocal()) {
|
|
91
|
-
return endpointList.getNextLocalEndpoint()
|
|
91
|
+
return endpointList.getNextLocalEndpoint();
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
// If only one endpoint is available return this.
|
|
95
95
|
if (list.length === 1) {
|
|
96
|
-
const endpoint = list[0]
|
|
96
|
+
const endpoint = list[0];
|
|
97
97
|
if (endpoint.isAvailable()) {
|
|
98
|
-
return endpoint
|
|
98
|
+
return endpoint;
|
|
99
99
|
}
|
|
100
|
-
return null
|
|
100
|
+
return null;
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
if (options.registry.preferLocalActions && endpointList.hasLocal()) {
|
|
104
|
-
const endpoint = endpointList.getNextLocalEndpoint()
|
|
104
|
+
const endpoint = endpointList.getNextLocalEndpoint();
|
|
105
105
|
if (endpoint && endpoint.isAvailable()) {
|
|
106
|
-
return endpoint
|
|
106
|
+
return endpoint;
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
const availableEndpoints = list.filter(endpoint => endpoint.isAvailable())
|
|
110
|
+
const availableEndpoints = list.filter(endpoint => endpoint.isAvailable());
|
|
111
111
|
if (availableEndpoints.length === 0) {
|
|
112
|
-
return null
|
|
112
|
+
return null;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
return select(availableEndpoints)
|
|
116
|
-
}
|
|
115
|
+
return select(availableEndpoints);
|
|
116
|
+
};
|
|
117
117
|
|
|
118
118
|
endpointList.getNextLocalEndpoint = () => {
|
|
119
119
|
if (endpointList.localEndpoints.length === 0) {
|
|
120
|
-
return null
|
|
120
|
+
return null;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
if (list.length === 1) {
|
|
124
|
-
const endpoint = endpointList.localEndpoints[0]
|
|
124
|
+
const endpoint = endpointList.localEndpoints[0];
|
|
125
125
|
if (endpoint.isAvailable()) {
|
|
126
|
-
return endpoint
|
|
126
|
+
return endpoint;
|
|
127
127
|
}
|
|
128
|
-
return null
|
|
128
|
+
return null;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
const availableEndpoints = endpointList.localEndpoints.filter(endpoint => endpoint.isAvailable())
|
|
131
|
+
const availableEndpoints = endpointList.localEndpoints.filter(endpoint => endpoint.isAvailable());
|
|
132
132
|
if (availableEndpoints.length === 0) {
|
|
133
|
-
return null
|
|
133
|
+
return null;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
return select(availableEndpoints)
|
|
137
|
-
}
|
|
136
|
+
return select(availableEndpoints);
|
|
137
|
+
};
|
|
138
138
|
|
|
139
|
-
endpointList.count = () => list.length
|
|
139
|
+
endpointList.count = () => list.length;
|
|
140
140
|
|
|
141
|
-
endpointList.getByNodeId = (nodeId) => list.find(endpoint => endpoint.node.id === nodeId)
|
|
141
|
+
endpointList.getByNodeId = (nodeId) => list.find(endpoint => endpoint.node.id === nodeId);
|
|
142
142
|
|
|
143
143
|
endpointList.removeByNodeId = (nodeId) => {
|
|
144
|
-
const endpointToRemove = list.find(item => item.node.id === nodeId)
|
|
145
|
-
list.splice(list.indexOf(endpointToRemove), 1)
|
|
146
|
-
setLocalEndpoints()
|
|
147
|
-
}
|
|
144
|
+
const endpointToRemove = list.find(item => item.node.id === nodeId);
|
|
145
|
+
list.splice(list.indexOf(endpointToRemove), 1);
|
|
146
|
+
setLocalEndpoints();
|
|
147
|
+
};
|
|
148
148
|
|
|
149
|
-
endpointList.removeByService = service => {
|
|
150
|
-
const endpointToRemove = list.find(endpoint => endpoint.service === service)
|
|
149
|
+
endpointList.removeByService = (service) => {
|
|
150
|
+
const endpointToRemove = list.find(endpoint => endpoint.service === service);
|
|
151
151
|
if (endpointToRemove) {
|
|
152
|
-
list.splice(list.indexOf(endpointToRemove), 1)
|
|
152
|
+
list.splice(list.indexOf(endpointToRemove), 1);
|
|
153
153
|
}
|
|
154
|
-
setLocalEndpoints()
|
|
155
|
-
}
|
|
154
|
+
setLocalEndpoints();
|
|
155
|
+
};
|
|
156
156
|
|
|
157
|
-
return endpointList
|
|
158
|
-
}
|
|
157
|
+
return endpointList;
|
|
158
|
+
};
|