const URL = Npm.require('url'),
  defaultConfig = {
    hitSamples: 1024,
    openedSessions: 64,
    closedSessions: 64,
  },
  config = _.extend(defaultConfig, Meteor.settings.connstats),
  hitSamples = _.range(config.hitSamples).map(() => undefined),
  openedSessions = [],
  closedSessions = [],
  randBucket = () => _.random(config.hitSamples - 1),
  activeSessions = {},
  append = (dest, entry, limit) => {
    dest.push(entry);
    if (dest.length > limit) {
      dest.shift();
    }
  },
  connDescr = (conn) => ({
    ua: conn.httpHeaders['user-agent'],
    ip: conn.httpHeaders['x-forwarded-for'],
    started: new Date(),
  });

WebApp.connectHandlers.use((req, res, next) => {
  if (req.url.indexOf('/.well-known/pcarrier/connstats') === 0) {
    res.setHeader('Content-Type', 'application/json');

    if (typeof config.secret !== 'undefined') {
      const query = URL.parse(req.url, true).query;
      if (config.secret !== query.secret) {
        res.statusCode = 403;
        return res.end(JSON.stringify({error:"secret needed"}));
      }
    }

    res.end(JSON.stringify({
      hitSamples: _.without(hitSamples, undefined),
      activeSessions: _.values(activeSessions),
      openedSessions,
      closedSessions,
    }));
  } else {
    hitSamples[randBucket()] = {
      ua: req.headers['user-agent'],
      ip: req.headers['x-forwarded-for'],
    };
    next();
  }
});

Meteor.onConnection(function(conn) {
  const descr = connDescr(conn);
  activeSessions[conn.id] = descr;
  append(openedSessions, descr, config.openedSessions);
  conn.onClose(function() {
    delete activeSessions[conn.id];
    descr.stopped = new Date();
    append(closedSessions, descr, config.closedSessions);
  });
});

@@ -1,12 +1,802 @@
+class RingArray %7B%0A  constructor(capacity) %7B%0A    this.capacity = capacity;%0A    this._entries = %5B%5D;%0A    this._position = 0;%0A    this.available = 0;%0A    for (let i = 0; i %3C capacity; i++) %7B%0A      this._entries%5Bi%5D = undefined;%0A    %7D%0A  %7D%0A%0A  push(entry) %7B%0A    this._entries%5Bthis._position%5D = entry;%0A    this._position = (this._position + 1) %25 this.capacity;%0A    if (this.available %3C this.capacity) %7B%0A      this.available += 1;%0A    %7D%0A    return entry;%0A  %7D%0A%0A  entries(n) %7B%0A    if (typeof n === 'undefined' %7C%7C n %3E= this.available) %7B%0A      n = this.available;%0A    %7D%0A    const result = %5B%5D;%0A    let pos = (this._position + this.capacity - n) %25 this.capacity;%0A    for (let i = 0; i %3C n; i++) %7B%0A      result%5Bi%5D = this._entries%5Bpos%5D;%0A      pos = (pos + 1) %25 this.capacity;%0A    %7D%0A    return result;%0A  %7D%0A%7D%0A%0A
 const URL = 
@@ -1060,230 +1060,177 @@
 s = 
-%5B%5D,%0A  closedSessions = %5B%5D,%0A  randBucket = () =%3E _.random(config.hitSamples - 1),%0A  activeSessions = %7B%7D,%0A  append = (dest, entry, limit) =%3E %7B%0A    dest.push(entry);%0A    if (dest.length %3E limit) %7B%0A      dest.shift();%0A    %7D%0A  
+new RingArray(config.openedSessions),%0A  closedSessions = new RingArray(config.openedSessions),%0A  randBucket = () =%3E _.random(config.hitSamples - 1),%0A  activeSessions = %7B
 %7D,%0A 
@@ -1950,30 +1950,82 @@
 ions
-,%0A      closedSessions
+: openedSessions.entries(),%0A      closedSessions: closedSessions.entries()
 ,%0A  
@@ -2293,45 +2293,8 @@
 ;%0A  
-append(openedSessions, descr, config.
 open
@@ -2299,24 +2299,35 @@
 enedSessions
+.push(descr
 );%0A  conn.on
@@ -2421,45 +2421,8 @@
     
-append(closedSessions, descr, config.
 clos
@@ -2431,16 +2431,27 @@
 Sessions
+.push(descr
 );%0A  %7D);

Use ring arrays