Node client: Sniffing does not wait for return routing requests potentially via discovery nodes

Hey,

I've been using the typescript client and wondered about the sniffing behaviour using the following code:

    const client = new Client({
        nodes: [ "http://localhost:9200" ],
        sniffOnStart: true,
        sniffOnConnectionFault: true,
    });

    client.on('sniff', (err, result) => {
        console.log("### SNIFF", err, result)
    })

    client.on('response', (err, result) => {
        console.log("### REQUEST", err, result)
    })

    const health = await client.cluster.health();
    console.log("### HEALTH FINISHED")

    // wait a little more until sniffing is logged...
   await new Promise(f => setTimeout(f, 2000));

The health will be finished, before the sniffing is done. I suppose that this is expected, but wanted to ensure.

If the master nodes are used as initial nodes to connect to a cluster, then for a few possibly hundred milliseconds (until the sniffing is done and the connection pool is updated) those will be hit with a search request.

Is there any possibility to wait for the sniffing to finish and only then be ready or is everyone good with putting a little load on the initial discovery nodes that are very likely be master nodes?

Thanks!

Answering myself a little here. One could alleviate the issue by waiting for the first sniff to finish before becoming ready - as I don't know if that sniff will always finish (or I maybe attached to it too late as there is no lifecycle, I also added a dumb two second timeout for this)

    const client = new Client({
        nodes: [ "http://localhost:9200" ],
        sniffOnStart: true,
        sniffOnConnectionFault: true,
    });

    const sniffingPromise = new Promise<void>((resolve, reject) => {
        client.once('sniff', (err, result) => {
            resolve();
        })
    })
    const twoSecondWait = new Promise<void>(f => setTimeout(f, 2000));
    await Promise.race([sniffingPromise, twoSecondWait]);
1 Like

I think it'd be best not to do this: ideally if you have dedicated master nodes then they're not even visible to clients. Instead I'd suggest starting with (a reasonable guess at) the nodes you actually want to use for client traffic. If it's tricky to make such a guess in your environment then consider some other layer of abstraction (e.g. round-robin DNS or a reverse proxy load balancer) to support you through the sniffing phase.

Having your application wait until sniffing is complete also seems ok, but also consider whether it's worth worrying about. You shouldn't be starting up clients very often, so a little misdirected load during the initial sniffing phase shouldn't be more than a tiny fraction of the total.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.