Skip to content

Commit 9fe5b60

Browse files
committed
dont use busy clients for inspection and retry inspection until timeout
1 parent 3a00500 commit 9fe5b60

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

modules/Inspector.js

+50-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const config = require('config');
22
const SteamUser = require('steam-user');
33
const GlobalOffensive = require('globaloffensive');
44
const SteamTotp = require('steam-totp');
5-
const _ = require('lodash');
5+
const async = require('async');
66
const logger = require('./Logger');
77
const Encryption = require('./Encryption');
88

@@ -73,31 +73,70 @@ accounts.forEach((account) => {
7373
clients.push({
7474
client,
7575
csgo,
76+
busySince: null,
7677
});
7778
});
7879

80+
setInterval(() => {
81+
for (let i = 0; i < clients.length; i += 1) {
82+
// Mark clients as not busy if they have been busy for more than 10 seconds.
83+
if (clients[i].busySince && Date.now() > (clients[i].busySince + 10000)) {
84+
clients[i].busySince = null;
85+
logger.info(`Marked client ${i} as not busy after 10 second timeout`);
86+
}
87+
}
88+
}, 1000);
89+
7990
class Inspector {
8091
static getClients() {
8192
return clients;
8293
}
8394

8495
static inspect(url, timeoutMs = 3000) {
8596
return new Promise((resolve, reject) => {
86-
const client = _.sample(_.filter(clients, ((x) => x.csgo.haveGCSession)));
87-
88-
if (_.isUndefined(client)) {
89-
reject(
90-
new Error('There is currently no client available with an active game coordinator connection'),
91-
);
97+
// We'll want a timeout of at least 1500 ms.
98+
if (timeoutMs < 1500) {
99+
throw new Error('The specified timeout must be at least 1500 ms');
92100
}
93101

94-
client.csgo.inspectItem(url, (item) => {
95-
resolve(item);
96-
});
97-
102+
// Make sure that the promise is rejected after the timeout.
98103
setTimeout(() => {
99104
reject(new Error(`Inspection timed out after ${timeoutMs} ms`));
100105
}, timeoutMs);
106+
107+
// Set the retry interval and the number of retries.
108+
// We subtract 1000 ms from the `timeoutMs` because we'll need some time
109+
// for the inspection too.
110+
const retryInterval = 100;
111+
const retryTimes = Math.round((timeoutMs - 1000 / retryInterval));
112+
113+
// Find an index of a client that is currently not busy and has a GC connection.
114+
async
115+
.retry({ times: retryTimes, interval: retryInterval }, async () => {
116+
const index = clients.findIndex(
117+
(client) => client.busySince === null && client.csgo.haveGCSession,
118+
);
119+
120+
if (index === -1) {
121+
throw new Error(
122+
'There is currently no client available with an active game coordinator connection',
123+
);
124+
}
125+
126+
return index;
127+
})
128+
.then((index) => {
129+
// Mark the client as busy so that it won't be used for other inspections.
130+
// If the inspection succeeds, `busySince` will be cleared immediately.
131+
// If the inspection times out, `busySince` will be cleared after within
132+
// 1 second after the timeout.
133+
clients[index].busySince = Date.now();
134+
135+
clients[index].csgo.inspectItem(url, (item) => {
136+
clients[index].busySince = null;
137+
resolve(item);
138+
});
139+
});
101140
});
102141
}
103142
}

util/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.sleep = (ms) => new Promise((res) => setTimeout(() => res(), ms));

0 commit comments

Comments
 (0)