Skip to content

Commit

Permalink
agent pool
Browse files Browse the repository at this point in the history
  • Loading branch information
rentallect committed Jan 24, 2024
1 parent 8dfdf11 commit 0ea5f0f
Show file tree
Hide file tree
Showing 74 changed files with 837 additions and 309 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"buffer": "^6.0.3",
"bufferutil": "^4.0.6",
"chnl": "^1.2.0",
"elapsed-time": "^0.0.1",
"es6-object-assign": "^1.1.0",
"events": "^3.3.0",
"fast-memoize": "^2.5.2",
Expand Down
2 changes: 1 addition & 1 deletion src/channel/channel-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
122 changes: 61 additions & 61 deletions src/channel/channel.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/channel/connection-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/connection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/connections.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/header-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/header.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/messages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/protocol.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/tls-connection-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/tls-connection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/wasm-tls-connection-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/wasm-tls-connection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
82 changes: 60 additions & 22 deletions src/context/context.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,7 @@ import { ZitiFormData } from '../http/form-data';
import { BrowserStdout } from '../http/browser-stdout';
import { http } from '../http/http';
import { ZitiWebSocketWrapperCtor } from '../http/ziti-websocket-wrapper-ctor';
import { ZitiAgentPool } from '../http/ziti-agent-pool';
import { ZitiWASMFD } from './wasmFD';


Expand All @@ -48,6 +49,7 @@ import { isUndefined, isEqual, isNull, result, find, filter, has, minBy, forEach
import EventEmitter from 'events';
import {isIP} from 'is-ip';
import jwt_decode from 'jwt-decode';
import ElapsedTime from 'elapsed-time';


// const EXPIRE_WINDOW = 28.0 // TEMP, for debugging
Expand Down Expand Up @@ -138,6 +140,24 @@ class ZitiContext extends EventEmitter {

this._didInitialGetPendingChannelConnects = false;

this._agentPool = new ZitiAgentPool({

// maximum number of ZitiAgent's the pool should contain
max: 25,

// number of milliseconds a client must sit idle in the pool and not be checked out
// before it is disconnected from the backend and discarded
// default is 10000 (10 seconds) - setting to 0 to disables auto-disconnection of idle clients
idleTimeoutMillis: 0,

// number of milliseconds to wait before timing out when connecting a new ZitiAgent - 0 means no timeout
connectionTimeoutMillis: (10 * 1000),

// pass the pool our logger
logger: this.logger,

})

}

get libCrypto () {
Expand Down Expand Up @@ -204,6 +224,14 @@ class ZitiContext extends EventEmitter {
}


/**
*
*/
getZitiAgentPool() {
return this._agentPool;
}


/**
*
*/
Expand Down Expand Up @@ -626,6 +654,7 @@ class ZitiContext extends EventEmitter {
*
*/
async acquireTLSHandshakeLock(fd) {
debugger;
this.logger.trace(`ZitiContext.acquireTLSHandshakeLock() [${fd}] trying to acquire _tlsHandshakeLock`);
this._tlsHandshakeLockRelease = await this._tlsHandshakeLock.acquire();
this._tlsHandshakeLockFD = fd;
Expand Down Expand Up @@ -705,7 +734,7 @@ class ZitiContext extends EventEmitter {

// Use 'ext-jwt' style authentication, but allow for 'password' style (mostly for testing)
let method = (isNull(self.access_token)) ? 'password' : 'ext-jwt';
self.logger.trace('ZitiContext.getFreshAPISession(): method [${method}]');
self.logger.trace(`ZitiContext.getFreshAPISession(): method[${method}]`);

// Get an API session with Controller
let res = await self._zitiBrowzerEdgeClient.authenticate({
Expand Down Expand Up @@ -1578,7 +1607,7 @@ class ZitiContext extends EventEmitter {

let self = this;

this.logger.debug(`connect() entered for conn[${conn.id}] networkSession[${networkSession}]`);
this.logger.debug(`ctx.connect() conn[${conn.id}] socket[${conn.socket._id}] networkSession[${networkSession}]`);

conn.networkSessionToken = networkSession.token;

Expand All @@ -1596,11 +1625,11 @@ class ZitiContext extends EventEmitter {
}

//
this.logger.debug(`trying to acquire _connectMutex for conn[${conn.id}]`);
// this.logger.debug(`trying to acquire _connectMutex for conn[${conn.id}]`);

await this._connectMutexWithTimeout.runExclusive(async () => {
// await this._connectMutexWithTimeout.runExclusive(async () => {

this.logger.debug(`now own _connectMutex for conn[${conn.id}]`);
// this.logger.debug(`now own _connectMutex for conn[${conn.id}]`);

let pendingChannelConnects = await this._getPendingChannelConnects(conn, edgeRouters);

Expand Down Expand Up @@ -1640,25 +1669,29 @@ class ZitiContext extends EventEmitter {
}

let channelWithNearestEdgeRouter = nearestEdgeRouter.channel;
this.logger.debug(`ch[${channelWithNearestEdgeRouter.id}] has nearest wssER[${nearestEdgeRouter.edgeRouterHost}] for conn[${conn.id}]`);
channelWithNearestEdgeRouter._connections._saveConnection(conn);
conn.channel = channelWithNearestEdgeRouter;
this.logger.debug(`ctx.connect() conn[${conn.id}] socket[${conn.socket._id}] ch[${channelWithNearestEdgeRouter.id}] has nearest wssER[${nearestEdgeRouter.edgeRouterHost}]`);

// Initiate connection with Edge Router (creates Fabric session)
await channelWithNearestEdgeRouter.connect(conn);
// if (conn.socket.isNew) {
await channelWithNearestEdgeRouter.connect(conn);
// }

if (conn.state == ZitiEdgeProtocol.conn_state.Connected) {
if (conn.encrypted) { // if connected to a service that has 'encryptionRequired'
// Do not proceed until crypto handshake has completed
this.logger.trace(`ctx.connect() conn[${conn.id}] socket[${conn.socket._id}] awaitConnectionCryptoEstablishComplete start`);
await channelWithNearestEdgeRouter.awaitConnectionCryptoEstablishComplete(conn);
this.logger.trace(`ctx.connect() conn[${conn.id}] socket[${conn.socket._id}] awaitConnectionCryptoEstablishComplete end`);
}
}
this.logger.debug(`releasing _connectMutex for conn[${conn.id}]`);
})
.catch(( err ) => {
this.logger.error(err);
throw new Error(err);
});
// this.logger.debug(`releasing _connectMutex for conn[${conn.id}]`);
// })
// .catch(( err ) => {
// this.logger.error(err);
// throw new Error(err);
// });
}


Expand Down Expand Up @@ -2044,15 +2077,17 @@ class ZitiContext extends EventEmitter {
*/
async httpFetch (url, opts) {

let et = ElapsedTime.new().start();

let self = this;

let value, release;

if (isEqual(opts.serviceScheme, 'https')) {
[value, release] = await self._fetchSemaphoreHTTPS.acquire();
} else {
[value, release] = await self._fetchSemaphoreHTTP.acquire();
}
// if (isEqual(opts.serviceScheme, 'https')) {
// [value, release] = await self._fetchSemaphoreHTTPS.acquire();
// } else {
// [value, release] = await self._fetchSemaphoreHTTP.acquire();
// }

let ret;

Expand All @@ -2069,12 +2104,11 @@ class ZitiContext extends EventEmitter {
url = parsedURL.toString();
}

self.logger.debug(`httpFetch starting for [${url}]`);
self.logger.debug(`httpFetch starting url[${url}]`);

// build HTTP request object
let request = new ZitiHttpRequest(opts.serviceName, url, opts, this);
let options = await request.getRequestOptions();
// options.domProxyHit = domProxyHit;

options.headers.set('Host', await this.getConfigHostByServiceName (opts.serviceName));

Expand All @@ -2083,10 +2117,12 @@ class ZitiContext extends EventEmitter {
if (options.method === 'GET') {

req = http.get(options);
req.agent = await this.getZitiAgentPool().connect(req, options);

} else {

req = http.request(options);
req.agent = await this.getZitiAgentPool().connect(req, options);

if (options.body) {
if (options.body instanceof Promise) {
Expand Down Expand Up @@ -2139,6 +2175,8 @@ class ZitiContext extends EventEmitter {
});

req.on('response', async res => {

self.logger.debug(`httpFetch on.reponse() elapsed[${et.getValue()}] url[${url}]`);

const response_options = {
url: url,
Expand Down Expand Up @@ -2205,7 +2243,7 @@ class ZitiContext extends EventEmitter {
ret = await fetchPromise;

} finally {
release();
// release();
}

return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/context/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/context/wasmFD.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/enroll/enroller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion src/enroll/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright Netfoundry, Inc.
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 0ea5f0f

Please sign in to comment.