-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.js
71 lines (63 loc) · 2.04 KB
/
proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const EC2Client = require("./lib/ec2");
const PausingProxy = require("./lib/pausingProxy");
const config = {
proxyPort: process.env.LISTEN_PORT ?
parseInt(process.env.LISTEN_PORT) : 25565,
clientTimeout: process.env.CLIENT_TIMEOUT ?
parseInt(process.env.CLIENT_TIMEOUT) : 120000,
forwardServer: {
port: process.env.FORWARD_PORT ?
parseInt(process.env.FORWARD_PORT) : 25565,
host: process.env.FORWARD_HOST
},
instanceId: process.env.INSTANCE_ID,
startupDelay: process.env.STARTUP_DELAY ?
parseInt(process.env.STARTUP_DELAY) : 16000,
inactiveShutdownSecs: process.env.INACTIVE_TIMEOUT ?
parseInt(process.env.INACTIVE_TIMEOUT) : 900000
};
let inactivityTimeout = null;
//proxy begins in paused state
const proxy = new PausingProxy(config.forwardServer, config.clientTimeout);
const instance = new EC2Client(config.instanceId, config.startupDelay);
const scheduleShutdown = async function() {
try {
if (inactivityTimeout == null) {
console.log(`Instance will be shut down in ${config.inactiveShutdownSecs/1000}s if no connection activity`);
inactivityTimeout = setTimeout(async () => {
inactivityTimeout = null;
proxy.pause();
await instance.stop();
}, config.inactiveShutdownSecs);
}
} catch (err) {
console.error("Failed to perform scheduled shutdown", err);
}
};
const ensureAvailable = async function() {
try {
if (inactivityTimeout != null) {
console.log("Cancelling instance shutdown");
clearTimeout(inactivityTimeout);
inactivityTimeout = null;
}
await instance.start();
proxy.resume();
} catch (err) {
console.error("Failed to ensure server availability", err);
}
};
//emitted at startup with 0 connections
proxy.on("connections", (count) => {
console.log(`Client connection count is ${count}`);
if (count > 0) {
ensureAvailable();
} else {
scheduleShutdown();
}
});
proxy.listen(config.proxyPort);
process.on('SIGINT', function() {
console.log( "\nGracefully exiting from SIGINT (Ctrl-C)" );
process.exit(1);
});