http: allow batched rpc calls.
This commit is contained in:
parent
8efbe1f9ee
commit
b025f5c241
@ -203,10 +203,6 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
var i, params, options, censored, output, address;
|
var i, params, options, censored, output, address;
|
||||||
|
|
||||||
if (req.method === 'POST' && req.pathname === '/') {
|
if (req.method === 'POST' && req.pathname === '/') {
|
||||||
enforce(typeof req.body.method === 'string', 'Method must be a string.');
|
|
||||||
if (!req.body.params)
|
|
||||||
req.body.params = [];
|
|
||||||
enforce(Array.isArray(req.body.params), 'Params must be an array.');
|
|
||||||
req.options = {};
|
req.options = {};
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
@ -541,50 +537,93 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
|
|
||||||
// JSON RPC
|
// JSON RPC
|
||||||
this.post('/', con(function* (req, res, send, next) {
|
this.post('/', con(function* (req, res, send, next) {
|
||||||
var json;
|
var out = [];
|
||||||
|
var cmds = req.body;
|
||||||
|
var array = true;
|
||||||
|
var i, cmd, json;
|
||||||
|
|
||||||
if (!this.rpc) {
|
if (!this.rpc) {
|
||||||
RPC = require('./rpc');
|
RPC = require('./rpc');
|
||||||
this.rpc = new RPC(this.node);
|
this.rpc = new RPC(this.node);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.debug('RPC call for %s:', req.pathname);
|
if (!Array.isArray(cmds)) {
|
||||||
this.logger.debug(req.body);
|
cmds = [cmds];
|
||||||
|
array = false;
|
||||||
if (req.body.method === 'getwork') {
|
|
||||||
res.setHeader('X-Long-Polling', '/?longpoll=1');
|
|
||||||
if (req.query.longpoll)
|
|
||||||
req.body.method = 'getworklp';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
for (i = 0; i < cmds.length; i++) {
|
||||||
json = yield this.rpc.execute(req.body);
|
cmd = cmds[i];
|
||||||
} catch (err) {
|
|
||||||
this.logger.error(err);
|
|
||||||
|
|
||||||
if (err.type === 'RPCError') {
|
enforce(cmd && typeof cmd === 'object', 'Command must be an object.');
|
||||||
return send(400, {
|
enforce(typeof cmd.method === 'string', 'Method must be a string.');
|
||||||
result: err.message,
|
|
||||||
error: null,
|
if (!cmd.params)
|
||||||
id: req.body.id
|
cmd.params = [];
|
||||||
});
|
|
||||||
|
enforce(Array.isArray(cmd.params), 'Params must be an array.');
|
||||||
|
|
||||||
|
if (!cmd.id)
|
||||||
|
cmd.id = 0;
|
||||||
|
|
||||||
|
enforce(typeof cmd.id === 'number', 'ID must be a number.');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.debug('Handling %d RPC calls.', cmds.length);
|
||||||
|
|
||||||
|
for (i = 0; i < cmds.length; i++) {
|
||||||
|
cmd = cmds[i];
|
||||||
|
|
||||||
|
if (cmd.method === 'getwork') {
|
||||||
|
res.setHeader('X-Long-Polling', '/?longpoll=1');
|
||||||
|
if (req.query.longpoll)
|
||||||
|
cmd.method = 'getworklp';
|
||||||
}
|
}
|
||||||
|
|
||||||
return send(500, {
|
this.logger.debug(cmd);
|
||||||
result: null,
|
|
||||||
error: {
|
try {
|
||||||
message: err.message,
|
json = yield this.rpc.execute(cmd);
|
||||||
code: 1
|
} catch (err) {
|
||||||
},
|
this.logger.error(err);
|
||||||
id: req.body.id
|
|
||||||
|
if (err.type === 'RPCError') {
|
||||||
|
out.push({
|
||||||
|
result: null,
|
||||||
|
error: {
|
||||||
|
message: err.message,
|
||||||
|
code: -1
|
||||||
|
},
|
||||||
|
id: cmd.id
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
out.push({
|
||||||
|
result: null,
|
||||||
|
error: {
|
||||||
|
message: err.message,
|
||||||
|
code: 1
|
||||||
|
},
|
||||||
|
id: cmd.id
|
||||||
|
});
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
out.push({
|
||||||
|
result: json != null ? json : null,
|
||||||
|
error: null,
|
||||||
|
id: cmd.id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
send(200, {
|
if (!array) {
|
||||||
result: json != null ? json : null,
|
send(200, out[0]);
|
||||||
error: null,
|
return;
|
||||||
id: req.body.id
|
}
|
||||||
});
|
|
||||||
|
send(200, out);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
this.get('/', function(req, res, send, next) {
|
this.get('/', function(req, res, send, next) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user