Skip to content

Instantly share code, notes, and snippets.

@billywhizz
Created December 29, 2019 03:40
Show Gist options
  • Save billywhizz/11e8d2784e8f8dacb8d72feb4ea97ecf to your computer and use it in GitHub Desktop.
Save billywhizz/11e8d2784e8f8dacb8d72feb4ea97ecf to your computer and use it in GitHub Desktop.
const { Socket, TCP } = library('socket', {})
const server = new Socket(TCP)
const read = Buffer.alloc(64 * 1024)
const write = Buffer.alloc(64 * 1024)
const bytes = new Uint8Array(read.bytes)
let off = 0, records = 0, resLength = 0
const response = `HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nServer: dv8\r\nDate: ${(new Date()).toUTCString()}\r\nContent-Length: 13\r\n\r\nHello, World!`
while (1) {
resLength = write.write(response, off)
off += resLength
records++
if (off + resLength > write.size) break
}
let rps = 0
global.t1 = setInterval(() => {
print(`mem: ${process.memoryUsage().rss}, rps: ${rps}`)
rps = 0
}, 1000)
server.onConnect(() => {
const client = new Socket(TCP)
let lastByte = 0, lineLength = 0
client.setup(read, write)
client.onClose(() => {})
client.onRead(len => {
let off = 0, byte = 0, requests = 0
while (len--) {
byte = bytes[off++]
lineLength++
if (byte === 10 && lastByte === 13) {
if (lineLength === 2) {
requests++
if (requests === records) {
rps += requests
client.write(requests * resLength)
requests = 0
}
}
lineLength = 0
}
lastByte = byte
}
if (requests > 0) {
rps += requests
client.write(requests * resLength)
requests = 0
}
})
client.onDrain(() => client.resume())
client.onEnd(() => client.close())
return client
})
server.listen('0.0.0.0', 3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment