@olane/o-client-limited 0.7.50 → 0.7.51
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/dist/src/connection/o-limited-connection.d.ts +32 -1
- package/dist/src/connection/o-limited-connection.d.ts.map +1 -1
- package/dist/src/connection/o-limited-connection.js +81 -2
- package/dist/src/o-limited.tool.d.ts +0 -7
- package/dist/src/o-limited.tool.d.ts.map +1 -1
- package/dist/src/o-limited.tool.js +1 -39
- package/package.json +7 -7
|
@@ -1,10 +1,41 @@
|
|
|
1
1
|
import { oNodeConnection } from '@olane/o-node';
|
|
2
2
|
import type { oNodeConnectionConfig } from '@olane/o-node';
|
|
3
3
|
/**
|
|
4
|
-
* oLimitedConnection extends oNodeConnection with stream reuse
|
|
4
|
+
* oLimitedConnection extends oNodeConnection with stream reuse and pool management.
|
|
5
|
+
*
|
|
5
6
|
* This is optimized for limited connections where creating new streams is expensive
|
|
7
|
+
* (mobile clients, browsers, resource-constrained environments).
|
|
8
|
+
*
|
|
9
|
+
* Stream Pool Architecture (10 streams total):
|
|
10
|
+
* - Stream 0: Dedicated background reader for incoming requests
|
|
11
|
+
* - Streams 1-9: Round-robin pool for outgoing request-response cycles
|
|
12
|
+
*
|
|
13
|
+
* Features:
|
|
14
|
+
* - Automatic recovery when dedicated reader fails
|
|
15
|
+
* - Health monitoring of all pooled streams
|
|
16
|
+
* - Automatic stream replacement on failure
|
|
17
|
+
* - Event emission for monitoring (reader-failed, stream-replaced, etc.)
|
|
18
|
+
*
|
|
19
|
+
* Default Behavior:
|
|
20
|
+
* - Uses 'reuse' stream policy by default
|
|
21
|
+
* - Automatically initializes pool manager on first stream request
|
|
6
22
|
*/
|
|
7
23
|
export declare class oLimitedConnection extends oNodeConnection {
|
|
24
|
+
private streamPoolManager?;
|
|
25
|
+
private poolInitialized;
|
|
8
26
|
constructor(config: oNodeConnectionConfig);
|
|
27
|
+
/**
|
|
28
|
+
* Initialize the stream pool manager
|
|
29
|
+
*/
|
|
30
|
+
private initializePoolManager;
|
|
31
|
+
getOrCreateStream(): Promise<any>;
|
|
32
|
+
/**
|
|
33
|
+
* Get stream pool statistics
|
|
34
|
+
*/
|
|
35
|
+
getPoolStats(): import("@olane/o-node").StreamPoolStats | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Override close to cleanup pool manager
|
|
38
|
+
*/
|
|
39
|
+
close(): Promise<void>;
|
|
9
40
|
}
|
|
10
41
|
//# sourceMappingURL=o-limited-connection.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-limited-connection.d.ts","sourceRoot":"","sources":["../../../src/connection/o-limited-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"o-limited-connection.d.ts","sourceRoot":"","sources":["../../../src/connection/o-limited-connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAqB,MAAM,eAAe,CAAC;AACnE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,kBAAmB,SAAQ,eAAe;IACrD,OAAO,CAAC,iBAAiB,CAAC,CAAoB;IAC9C,OAAO,CAAC,eAAe,CAAS;gBAEpB,MAAM,EAAE,qBAAqB;IASzC;;OAEG;YACW,qBAAqB;IAqC7B,iBAAiB,IAAI,OAAO,CAAC,GAAG,CAAC;IAevC;;OAEG;IACH,YAAY;IAIZ;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAY7B"}
|
|
@@ -1,7 +1,23 @@
|
|
|
1
|
-
import { oNodeConnection } from '@olane/o-node';
|
|
1
|
+
import { oNodeConnection, StreamPoolManager } from '@olane/o-node';
|
|
2
2
|
/**
|
|
3
|
-
* oLimitedConnection extends oNodeConnection with stream reuse
|
|
3
|
+
* oLimitedConnection extends oNodeConnection with stream reuse and pool management.
|
|
4
|
+
*
|
|
4
5
|
* This is optimized for limited connections where creating new streams is expensive
|
|
6
|
+
* (mobile clients, browsers, resource-constrained environments).
|
|
7
|
+
*
|
|
8
|
+
* Stream Pool Architecture (10 streams total):
|
|
9
|
+
* - Stream 0: Dedicated background reader for incoming requests
|
|
10
|
+
* - Streams 1-9: Round-robin pool for outgoing request-response cycles
|
|
11
|
+
*
|
|
12
|
+
* Features:
|
|
13
|
+
* - Automatic recovery when dedicated reader fails
|
|
14
|
+
* - Health monitoring of all pooled streams
|
|
15
|
+
* - Automatic stream replacement on failure
|
|
16
|
+
* - Event emission for monitoring (reader-failed, stream-replaced, etc.)
|
|
17
|
+
*
|
|
18
|
+
* Default Behavior:
|
|
19
|
+
* - Uses 'reuse' stream policy by default
|
|
20
|
+
* - Automatically initializes pool manager on first stream request
|
|
5
21
|
*/
|
|
6
22
|
export class oLimitedConnection extends oNodeConnection {
|
|
7
23
|
constructor(config) {
|
|
@@ -10,6 +26,69 @@ export class oLimitedConnection extends oNodeConnection {
|
|
|
10
26
|
...config,
|
|
11
27
|
reusePolicy: reusePolicy,
|
|
12
28
|
});
|
|
29
|
+
this.poolInitialized = false;
|
|
13
30
|
this.reusePolicy = reusePolicy;
|
|
14
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Initialize the stream pool manager
|
|
34
|
+
*/
|
|
35
|
+
async initializePoolManager() {
|
|
36
|
+
if (this.poolInitialized || this.streamPoolManager) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
this.logger.debug('Initializing stream pool manager');
|
|
40
|
+
this.streamPoolManager = new StreamPoolManager({
|
|
41
|
+
poolSize: 10,
|
|
42
|
+
readerStreamIndex: 0,
|
|
43
|
+
streamHandler: this.streamHandler,
|
|
44
|
+
p2pConnection: this.p2pConnection,
|
|
45
|
+
requestHandler: this.config.requestHandler,
|
|
46
|
+
createStream: async () => {
|
|
47
|
+
return await super.getOrCreateStream();
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
// Set up event listeners for monitoring
|
|
51
|
+
this.streamPoolManager.on('reader-failed', (data) => {
|
|
52
|
+
this.logger.warn('Stream pool reader failed', data);
|
|
53
|
+
});
|
|
54
|
+
this.streamPoolManager.on('reader-recovered', (data) => {
|
|
55
|
+
this.logger.info('Stream pool reader recovered', data);
|
|
56
|
+
});
|
|
57
|
+
this.streamPoolManager.on('stream-replaced', (data) => {
|
|
58
|
+
this.logger.info('Stream replaced in pool', data);
|
|
59
|
+
});
|
|
60
|
+
await this.streamPoolManager.initialize();
|
|
61
|
+
this.poolInitialized = true;
|
|
62
|
+
this.logger.info('Stream pool manager initialized successfully');
|
|
63
|
+
}
|
|
64
|
+
async getOrCreateStream() {
|
|
65
|
+
if (this.reusePolicy === 'reuse') {
|
|
66
|
+
// Initialize pool manager on first call
|
|
67
|
+
if (!this.poolInitialized) {
|
|
68
|
+
await this.initializePoolManager();
|
|
69
|
+
}
|
|
70
|
+
// Delegate to pool manager for stream selection
|
|
71
|
+
return await this.streamPoolManager.getStream();
|
|
72
|
+
}
|
|
73
|
+
// Fallback to default behavior if reuse is not enabled
|
|
74
|
+
return super.getOrCreateStream();
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get stream pool statistics
|
|
78
|
+
*/
|
|
79
|
+
getPoolStats() {
|
|
80
|
+
return this.streamPoolManager?.getStats();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Override close to cleanup pool manager
|
|
84
|
+
*/
|
|
85
|
+
async close() {
|
|
86
|
+
this.logger.debug('Closing connection and stream pool manager');
|
|
87
|
+
if (this.streamPoolManager) {
|
|
88
|
+
await this.streamPoolManager.close();
|
|
89
|
+
this.streamPoolManager = undefined;
|
|
90
|
+
}
|
|
91
|
+
this.poolInitialized = false;
|
|
92
|
+
await super.close();
|
|
93
|
+
}
|
|
15
94
|
}
|
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
import { oNodeConnection, oNodeConnectionConfig, oNodeTool } from '@olane/o-node';
|
|
2
2
|
import { oNodeConfig } from '@olane/o-node';
|
|
3
3
|
export declare class oLimitedTool extends oNodeTool {
|
|
4
|
-
protected backgroundReaders: Map<string, boolean>;
|
|
5
4
|
constructor(config: oNodeConfig);
|
|
6
5
|
connect(config: oNodeConnectionConfig): Promise<oNodeConnection>;
|
|
7
|
-
/**
|
|
8
|
-
* Start a background reader on the stream to handle incoming requests.
|
|
9
|
-
* Uses handleIncomingStream which runs a persistent while loop,
|
|
10
|
-
* allowing the stream to receive multiple messages over its lifetime.
|
|
11
|
-
*/
|
|
12
|
-
private startBackgroundReader;
|
|
13
6
|
initConnectionManager(): Promise<void>;
|
|
14
7
|
}
|
|
15
8
|
//# sourceMappingURL=o-limited.tool.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"o-limited.tool.d.ts","sourceRoot":"","sources":["../../src/o-limited.tool.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,SAAS,EACV,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"o-limited.tool.d.ts","sourceRoot":"","sources":["../../src/o-limited.tool.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,SAAS,EACV,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,qBAAa,YAAa,SAAQ,SAAS;gBAC7B,MAAM,EAAE,WAAW;IAUzB,OAAO,CAAC,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAAC,eAAe,CAAC;IAkBhE,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;CAQ7C"}
|
|
@@ -9,7 +9,6 @@ export class oLimitedTool extends oNodeTool {
|
|
|
9
9
|
listeners: config.network?.listeners || [], // default to no listeners
|
|
10
10
|
},
|
|
11
11
|
});
|
|
12
|
-
this.backgroundReaders = new Map();
|
|
13
12
|
}
|
|
14
13
|
async connect(config) {
|
|
15
14
|
this.handleProtocol(config.nextHopAddress).catch((error) => {
|
|
@@ -17,51 +16,14 @@ export class oLimitedTool extends oNodeTool {
|
|
|
17
16
|
});
|
|
18
17
|
// Inject requestHandler to enable bidirectional stream processing
|
|
19
18
|
// This allows incoming router requests to be processed through the tool's execute method
|
|
19
|
+
// The StreamPoolManager (in oLimitedConnection) will use this handler for the dedicated reader
|
|
20
20
|
const configWithHandler = {
|
|
21
21
|
...config,
|
|
22
22
|
requestHandler: this.execute.bind(this),
|
|
23
23
|
};
|
|
24
24
|
const connection = await super.connect(configWithHandler);
|
|
25
|
-
// Hook postTransmit to start background reader for reuse streams
|
|
26
|
-
const originalPostTransmit = connection.postTransmit.bind(connection);
|
|
27
|
-
connection.postTransmit = async (stream) => {
|
|
28
|
-
// Start background reader for bidirectional communication on reuse streams
|
|
29
|
-
if (connection.reusePolicy === 'reuse') {
|
|
30
|
-
this.startBackgroundReader(stream, connection);
|
|
31
|
-
}
|
|
32
|
-
await originalPostTransmit(stream);
|
|
33
|
-
};
|
|
34
25
|
return connection;
|
|
35
26
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Start a background reader on the stream to handle incoming requests.
|
|
38
|
-
* Uses handleIncomingStream which runs a persistent while loop,
|
|
39
|
-
* allowing the stream to receive multiple messages over its lifetime.
|
|
40
|
-
*/
|
|
41
|
-
startBackgroundReader(stream, connection) {
|
|
42
|
-
const streamId = stream.p2pStream?.id || stream.id;
|
|
43
|
-
// Don't start duplicate readers for the same stream
|
|
44
|
-
if (this.backgroundReaders.get(streamId)) {
|
|
45
|
-
this.logger.debug('Background reader already running for stream:', streamId);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
this.backgroundReaders.set(streamId, true);
|
|
49
|
-
this.logger.debug('Starting background reader for stream:', streamId);
|
|
50
|
-
// Get the raw p2p stream
|
|
51
|
-
const p2pStream = stream.p2pStream || stream;
|
|
52
|
-
// Use the connection's streamHandler to handle incoming requests
|
|
53
|
-
const streamHandler = connection.streamHandler;
|
|
54
|
-
// Start the persistent read loop in the background
|
|
55
|
-
streamHandler
|
|
56
|
-
.handleIncomingStream(p2pStream, connection.p2pConnection, async (request, s) => {
|
|
57
|
-
this.logger.debug('Background reader received request:', request.method);
|
|
58
|
-
return this.execute(request, s);
|
|
59
|
-
})
|
|
60
|
-
.catch((error) => {
|
|
61
|
-
this.logger.debug('Background reader exited:', error?.message || 'stream closed');
|
|
62
|
-
this.backgroundReaders.delete(streamId);
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
27
|
async initConnectionManager() {
|
|
66
28
|
this.connectionManager = new oLimitedConnectionManager({
|
|
67
29
|
p2pNode: this.p2pNode,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olane/o-client-limited",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.51",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"types": "dist/src/index.d.ts",
|
|
@@ -53,13 +53,13 @@
|
|
|
53
53
|
"typescript": "5.4.5"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@olane/o-config": "0.7.
|
|
57
|
-
"@olane/o-core": "0.7.
|
|
58
|
-
"@olane/o-node": "0.7.
|
|
59
|
-
"@olane/o-protocol": "0.7.
|
|
60
|
-
"@olane/o-tool": "0.7.
|
|
56
|
+
"@olane/o-config": "0.7.51",
|
|
57
|
+
"@olane/o-core": "0.7.51",
|
|
58
|
+
"@olane/o-node": "0.7.51",
|
|
59
|
+
"@olane/o-protocol": "0.7.51",
|
|
60
|
+
"@olane/o-tool": "0.7.51",
|
|
61
61
|
"debug": "^4.4.1",
|
|
62
62
|
"dotenv": "^16.5.0"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "f770a110369cb9791d9e9208c7a084feecbb9b2c"
|
|
65
65
|
}
|