Hacker News new | ask | show | jobs
by jesusprubio 2577 days ago
Obviously, you're right. And I can imagine any developer can imagine the same thing :).

But I couldn't find a better way to check if I am online than making a request to any server. In fact, I'm using two over different protocols just in case.

1 comments

One area for improvement: Default to a set of domains to check rather than just one, for example google.com, facebook.com, baidu.com. Check them all in parallel, and return success as soon as you get a good answer from 2 out of 3. This avoids falsely reporting "offline" when one host happens to be down, and it avoids slowing down the check when one host happens to be slow. Probably use `mio` to orchestrate this, to avoid the overhead of threads. (Note that this is similar to how musl libc implements DNS.)

Another area: Think about what should be reported in a country like China. For example, facebook.com is blocked. If China blocks google.com tomorrow, does that mean that every computer in China is offline? Probably not. On the one hand, this isn't something that a library can give a one-size-fits all answer for. On the other hand, since China has the most internet users of any country in the world, this is probably something that the design of the library should consider and offer documentation and best practices for.

Another area: Many GUI apps expect to make ongoing connections, e.g. chat apps that are always listening for messages. These apps need to persistently check the status of the network in the background, and many would be willing to dedicate a background thread to this library if it helped with some of the details of that, like 1) detecting hardware-level state changes, like WiFi coming and going, to prompt an immediate re-check, 2) checking in a rapid loop after a network change, but backing off to a lower background rate when the network is stable, 3) "de-bouncing" network events in case the network seems to be coming and going multiple times a second, so that UIs responding to those events don't flicker.

Higher level: This is a common problem. Find other libraries that have solved this problem before, compare the different ways they've solved it, and document the tradeoffs. I appreciate that I'm suggesting that you do a lot of work, but if you intend for production applications to use this code then it's very important work.

Thank you for the feedback. It's only my first crate I wrote to learn about the environment. It emulates more or less this one in Node.js a lot of people uses: https://github.com/sindresorhus/is-online

I wanted to implement it in this way. But I'm waiting for async/await, it should be trivial with a map function or something similar. But now it already tries a fallback connection if the first one fails.

Good point about the countries, I need to add an issue to cover it.