http example doesn't seem to see the very first request on startup...
See original GitHub issueHi @billywhizz I’ve been messing with the http example to build a sort of runtime web/http server library around Just, but there’s this thing happening where only the first request (since server startup) doesn’t seem to make it to the onConnect handler… It’s not such a huge issue since all subsequent requests seem to work fine, but it seems odd, and wanted to mention it:
In the http example we have:
function onConnect(sock) {
const self = this;
stats.conn++;
const stream = createHTTPStream(sock.buf, maxPipeline);
const { buf, size } = responses[200];
sock.onReadable = () => {
const bytes = sock.pull(stream.offset);
if (bytes <= 0)
return;
let headers = stream.getHeaders().split('\n');
let pathParts = headers[0].split(' ');
print("Method: " + pathParts[0] + ", url: " + pathParts[1]);
const err = stream.parse(bytes, count => {
qps += count;
stats.rps += bytes;
if (sock.write(buf, count * size) <= 0)
return;
stats.wps += (count * size);
})
if (err < 0) print(`error: ${err}`) // Todo: handle app error
}
sock.onWritable = () => {}
sock.onEnd = () => stats.conn--;
return this;
}
function start() {
print("Starting server...");
createServer(onConnect).listen();
return this;
}
You can see I added a print() statement in onConnect. When I hit any url (ie. http://localhost:3000/test)… two requests are sent from the browser. One is for the favicon, and the other for the url endpoints (/test). The onConnect print statement only sees the favicon request, but not /test. Only when I try a second request (and subsequent) will it show that /test is being reached.
I’m not entirely sure why, maybe it’s to do with the socket initialization on first run, or the way createServer is initialized… but it’s only happening on the very first request.
If you might have any ideas, let me know, but I’ll dig in a little.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top GitHub Comments
hmm. yes, it should be possible to do something like this. i’ve been doing some experiments with bundling up an entire directory of code as a module which could then be imported by another application as a library/module. am still in the weeds with it at the moment and just testing ideas out.
i’m currently in middle of a refactor branch that let’s you decide what you want from core in your application before compilation. it’s working pretty good. for example i can just write a config like this and then run a command to bundle up only the c++ modules and JS libs i need:
@rw3iss that’s cool ryan! i will check it out. i have got a basic example working here: https://github.com/just-js/examples/tree/main/http. this uses the http module which is using picohttpparser. i’ll see if i can add some more to this and flesh it out a bit. feel free to hack on it and submit a PR if you have any suggested changes.
i’ve also been experimenting with packet sniffing here. this is useful when debugging http servers - it lets you see the whole tcp conversation. and have also been doing some experiments with building/bundling here. i have basics working now to be able to bundle an app and all it’s libs/modules up into a single executable or a shared library. will write some blog posts about these when i get some time.