Reading the javascript websocket code

I was reading through the javascript code and came upon this:

  /**
   * Return the next message ref, accounting for overflows
   * @returns {string}
   */
  makeRef(){
    let newRef = this.ref + 1
    if(newRef === this.ref){ this.ref = 0 } else { this.ref = newRef }

    return this.ref.toString()
  }

(found here:
phoenix/socket.js at e221f88083779a4055bddf3d268f5d23f474bea9 · phoenixframework/phoenix · GitHub)

I was just wondering why the overflow protection is on there. I can see that this a good practice but in this case I cannot find any way of every reaching that overflow, or am I missing something?

Number.MAX_SAFE_INTEGER
// 9007199254740991
Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2
// true
Number.MAX_SAFE_INTEGER / 1000 / 24 / 60 / 60 / 365
// 285616.4147241562

So if you generate 1000s new refs per second you can still get 28 000 years in the future. Seems like a very long time to have 1 client running without ever crashing?

Our software is just that robust that we plan for tens of thousands of years of uptime :smiley:

In all honesty I knew overflows were exceedingly unlikely but I didn’t do the math on just how unlikely so this check really isn’t necessary

6 Likes

Thanks, I was mostly wondering if I’ve maybe missed something :slight_smile:

Another thing to keep in mind is we created the client with ie6 support and I’m not sure what the various IE’s overflow at

1 Like