Skip to content

Commit

Permalink
add options to limit simultaneous connections (rethinkdb#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
nphyatt authored and Tryneus committed Sep 15, 2016
1 parent 2e1a31d commit 4b72b87
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/src/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ const parseArguments = (args) => {
{ type: 'string', metavar: 'yes|no', constant: 'yes', nargs: '?',
help: 'Whether to allow anonymous Horizon connections.' });

parser.addArgument([ '--max-connections' ],
{ type: 'int', metavar: 'MAX_CONNECTIONS',
help: 'Maximum number of simultaneous connections server will accept.' });

parser.addArgument([ '--debug' ],
{ type: 'string', metavar: 'yes|no', constant: 'yes', nargs: '?',
help: 'Enable debug logging.' });
Expand Down Expand Up @@ -347,6 +351,7 @@ const start_horizon_server = (http_servers, opts) =>
rdb_user: opts.rdb_user || null,
rdb_password: opts.rdb_password || null,
rdb_timeout: opts.rdb_timeout || null,
max_connections: opts.max_connections || null,
});

// `interruptor` is meant for use by tests to stop the server without relying on SIGINT
Expand Down
1 change: 1 addition & 0 deletions cli/src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const make_default_options = () => ({
allow_unauthenticated: false,
auth_redirect: '/',
access_control_allow_origin: '',
max_connections: null,

auth: { },
});
Expand Down
4 changes: 4 additions & 0 deletions server/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Client {
// The first message should always be the handshake
this._socket.once('message', (data) =>
this.error_wrap_socket(() => this.handle_handshake(data)));

if (server._max_connections !== null && server._reql_conn._clients.size >= server._max_connections) {
this.close({ request_id: null, error: 'Max connections limit reached.', error_code: 0 });
}
}

handle_websocket_close() {
Expand Down
1 change: 1 addition & 0 deletions server/src/schema/server_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const server = Joi.object({
rdb_user: Joi.string().allow(null),
rdb_password: Joi.string().allow(null),
rdb_timeout: Joi.number().allow(null),
max_connections: Joi.number().allow(null),
}).unknown(false);

const auth = Joi.object({
Expand Down
1 change: 1 addition & 0 deletions server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Server {
const opts = Joi.attempt(user_opts || { }, options_schema);
this._path = opts.path;
this._name = opts.project_name;
this._max_connections = opts.max_connections;
this._permissions_enabled = opts.permissions;
this._auth_methods = { };
this._request_handlers = new Map();
Expand Down

0 comments on commit 4b72b87

Please sign in to comment.