question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

http example doesn't seem to see the very first request on startup...

See original GitHub issue

Hi @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:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
billywhizzcommented, Nov 13, 2020

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:

const libs = [
  'lib/fs.js',
  'lib/loop.js',
  'lib/path.js',
  'lib/process.js'
]

const version = '0.0.5'

const capabilities = [] // list of allowed internal modules, api calls etc. TBD

const modules = [{
  name: 'sys',
  obj: [
    'modules/sys/sys.o'
  ]
}, {
  name: 'fs',
  obj: [
    'modules/fs/fs.o'
  ]
}, {
  name: 'net',
  obj: [
    'modules/net/net.o'
  ]
}, {
  name: 'vm',
  obj: [
    'modules/vm/vm.o'
  ]
}, {
  name: 'epoll',
  obj: [
    'modules/epoll/epoll.o'
  ]
}]

const target = 'just'
const main = 'just.js'

module.exports = { version, libs, modules, capabilities, target, main }
1reaction
billywhizzcommented, Nov 12, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are Spring Rest services slow on the first request?
The "problem" is: the first request after startup takes way longer to get an answer from the server than all others.
Read more >
Reducing initial request latency by pre-building services in a ...
In this post I show a startup task that pre-builds all the services registered in the DI container to reduce the duration of...
Read more >
ASP.NET Core Middleware | Microsoft Learn
This case doesn't include an actual request pipeline. ... must appear before any middleware that might check the request culture (for ...
Read more >
10 Error Status Codes When Building APIs For The First Time ...
This status code means you haven't yet authenticated against the API. The API doesn't know who you are and it won't serve you....
Read more >
Making ASP.NET Application Always Running
Right-click on the same application pool and select “Advanced Settings”. Update the following values: Set start mode to “Always Running”. Setting the start ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found