Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse sessions with many origins #20

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,11 @@ Default: `1`

Max free sessions per origin.

#### agent.getName(authority, options)

Returns a `string` containing a proper name for sessions created with these options.

#### agent.getSession(authority, options, name)

Returns a Promise giving free `Http2Session` under the provided name. If no free sessions are found, a new one is created.

If the `name` argument is `undefined`, it defaults to `agent.getName(authority, options)`.
If the `name` argument is `undefined`, it defaults to `${authority.hostname}:${authority.port}`.

##### authority

Expand Down
72 changes: 47 additions & 25 deletions source/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ const nameKeys = [
'maxReservedRemoteStreams',
'maxSendHeaderBlockLength',
'paddingStrategy',
'peerMaxConcurrentStreams',
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
'settings',
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

// `tls.connect()` options
'localAddress',
'family',
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
'path',
'rejectUnauthorized',
'minDHSize',
Expand Down Expand Up @@ -67,10 +64,25 @@ class Agent extends EventEmitter {
constructor({timeout = 60000, maxSessions = Infinity, maxFreeSessions = 1} = {}) {
super();

this.busySessions = {};
this.freeSessions = {};
this.queue = {};

this.sessions = {
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
// optionsX: {
// originA: {
// freeSessions: [
// session3,
// ...
// ],
// busySessions: [
// session1,
// ...
// ]
// },
// ...
// },
// ...
};

this.timeout = timeout;
this.maxSessions = maxSessions;
this.maxFreeSessions = maxFreeSessions;
Expand All @@ -80,28 +92,18 @@ class Agent extends EventEmitter {
};
}

getName(authority, options = {}) {
if (typeof authority === 'string') {
authority = new URL(authority);
}

const port = authority.port || 443;
const host = authority.hostname || authority.host || 'localhost';

let name = `${host}:${port}`;
_optionsToString(options) {
let serialized = '';

// TODO: this should ignore defaults too
for (const key of nameKeys) {
if (Reflect.has(options, key)) {
if (typeof options[key] === 'object') {
name += `:${JSON.stringify(options[key])}`;
} else {
name += `:${options[key]}`;
if (options) {
for (const key of nameKeys) {
if (Reflect.has(options, key)) {
serialized += `:${options[key]}`;
}
}
}

return name;
return serialized;
}

_processQueue(name) {
Expand All @@ -118,7 +120,18 @@ class Agent extends EventEmitter {
return new Promise((resolve, reject) => {
const detached = {resolve, reject};

name = name || this.getName(authority, options);
if (!name) {
if (typeof authority === 'string') {
authority = new URL(authority);
}

const port = authority.port || 443;
const host = authority.hostname || authority.host || 'localhost';

name = `${host}:${port}`;
}

const optionsString = this._optionsToString(options);

if (Reflect.has(this.freeSessions, name)) {
resolve(this.freeSessions[name][0]);
Expand Down Expand Up @@ -152,6 +165,10 @@ class Agent extends EventEmitter {
});
session[kCurrentStreamsCount] = 0;

const applyOrigins = () => {

};

session.setTimeout(this.timeout, () => {
// `.close()` would wait until all streams all closed
session.destroy();
Expand All @@ -178,8 +195,14 @@ class Agent extends EventEmitter {
this._processQueue(name);
});

session.on('origin', originSet => {
applyOrigins();
// PlayerTwo.every(val => PlayerOne.includes(val));
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
});

session.once('localSettings', () => {
removeFromQueue();
applyOrigins();

const movedListeners = listeners.splice(session.remoteSettings.maxConcurrentStreams);

Expand Down Expand Up @@ -271,9 +294,8 @@ class Agent extends EventEmitter {

async request(authority, options, headers) {
const session = await this.getSession(authority, options);
const stream = session.request(headers);

return stream;
return session.request(headers);
}

createConnection(authority, options) {
Expand Down