Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 6d6c6df

Browse files
committed
Tokens have URL for WebTransport connections.
A new property webTransportUrl is added to tokens if a QUIC agent is running.
1 parent 8b4afdb commit 6d6c6df

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

source/agent/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ var joinCluster = function (on_ok) {
9595
joinRetry: config.cluster.worker.join_retry,
9696
info: {
9797
ip: config.cluster.worker.ip,
98+
port: config[myPurpose] ? config[myPurpose].port : undefined,
9899
purpose: myPurpose,
99100
state: 2,
100101
max_load: config.cluster.worker.load.max,

source/data_access/model/tokenModel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var TokenSchema = new Schema({
1515
origin: {},
1616
code: String,
1717
secure: Boolean,
18-
host: String
18+
host: String,
19+
webTransportUrl: String
1920
});
2021

2122
module.exports = mongoose.model('Token', TokenSchema);

source/management_api/requestHandler.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,37 @@ var log = require('./logger').logger.getLogger('RequestHandler');
3333
var cluster_name = ((global.config || {}).cluster || {}).name || 'owt-cluster';
3434
var e = require('./errors');
3535

36-
exports.schedulePortal = function (tokenCode, origin, callback) {
36+
const scheduleAgent = function(agentName, tokenCode, origin, attempts, callback) {
3737
var keepTrying = true;
3838

39-
var tryFetchingPortal = function (attempts) {
39+
var tryFetchingAgent = function(attempts) {
4040
if (attempts <= 0) {
4141
return callback('timeout');
4242
}
4343

44-
rpc.callRpc(cluster_name, 'schedule', ['portal', tokenCode, origin, 60 * 1000], {callback: function (result) {
44+
rpc.callRpc(cluster_name, 'schedule', [ agentName, tokenCode, origin, 60 * 1000 ], { callback : function(result) {
4545
if (result === 'timeout' || result === 'error') {
4646
if (keepTrying) {
47-
log.info('Faild in scheduling portal, tokenCode:', tokenCode, ', keep trying.');
48-
setTimeout(function () {tryFetchingPortal(attempts - (result === 'timeout' ? 4 : 1));}, 1000);
47+
log.info('Faild in scheduling ', agentName, ' tokenCode:', tokenCode, ', keep trying.');
48+
setTimeout(function() { tryFetchingAgent(attempts - (result === 'timeout' ? 4 : 1)); }, 1000);
4949
}
5050
} else {
5151
callback(result.info);
5252
keepTrying = false;
5353
}
54-
}});
54+
} });
5555
};
5656

57-
tryFetchingPortal(60);
57+
tryFetchingAgent(attempts);
58+
};
59+
60+
exports.schedulePortal = function(tokenCode, origin, callback) {
61+
return scheduleAgent('portal', tokenCode, origin, 60, callback);
62+
};
63+
64+
exports.scheduleQuicAgent = async function(tokenCode, origin, callback) {
65+
// QUIC agent may not be enabled in all deployment environment, at least before CI for QUIC SDK is ready. Attempts less times to reduce response time for creating tokens.
66+
return scheduleAgent('quic', tokenCode, origin, 5, callback);
5867
};
5968

6069
const scheduleRoomController = function (roomId) {

source/management_api/resource/v1/tokensResource.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ var log = logger.getLogger('TokensResource');
3939

4040
var getTokenString = function (id, token) {
4141
return dataAccess.token.key().then(function(serverKey) {
42-
var toSign = id + ',' + token.host,
42+
var toSign = id + ',' + token.host + ',' + token.webTransportUrl,
4343
hex = crypto.createHmac('sha256', serverKey).update(toSign).digest('hex'),
4444
signed = (new Buffer(hex)).toString('base64'),
4545

4646
tokenJ = {
4747
tokenId: id,
4848
host: token.host,
4949
secure: token.secure,
50+
webTransportUrl: token.webTransportUrl,
5051
signature: signed
5152
},
5253
tokenS = (new Buffer(JSON.stringify(tokenJ))).toString('base64');
@@ -100,7 +101,7 @@ var generateToken = function (currentRoom, authData, origin, callback) {
100101
return;
101102
}
102103

103-
if(ec.via_host !== '') {
104+
if(ec.via_host !== '') {
104105
if(ec.via_host.indexOf('https') == 0) {
105106
token.secure = true;
106107
token.host = ec.via_host.substr(8);
@@ -120,11 +121,22 @@ var generateToken = function (currentRoom, authData, origin, callback) {
120121
token.host += ':' + ec.port;
121122
}
122123

123-
dataAccess.token.create(token, function(id) {
124-
getTokenString(id, token)
125-
.then((tokenS) => {
126-
callback(tokenS);
127-
});
124+
// TODO: Schedule QUIC agent and portal parallelly.
125+
requestHandler.scheduleQuicAgent(token.code, origin, info => {
126+
if (info !== 'timeout') {
127+
let hostname = info.hostname;
128+
if (!hostname) {
129+
hostname = info.ip;
130+
}
131+
// TODO: Rename "echo".
132+
token.webTransportUrl = 'quic-transport://' + hostname + ':' + info.port + '/echo';
133+
}
134+
dataAccess.token.create(token, function(id) {
135+
getTokenString(id, token)
136+
.then((tokenS) => {
137+
callback(tokenS);
138+
});
139+
});
128140
});
129141
});
130142
};

source/portal/portal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var Portal = function(spec, rpcReq) {
3535
}
3636

3737
var calculateSignature = function (token) {
38-
var toSign = token.tokenId + ',' + token.host,
38+
var toSign = token.tokenId + ',' + token.host + ',' + token.webTransportUrl,
3939
signed = crypto.createHmac('sha256', token_key).update(toSign).digest('hex');
4040
return (new Buffer(signed)).toString('base64');
4141
};

0 commit comments

Comments
 (0)