Skip to content

Commit

Permalink
Merge 092a9a8 into fd48013
Browse files Browse the repository at this point in the history
  • Loading branch information
wmurphyrd committed Feb 12, 2024
2 parents fd48013 + 092a9a8 commit 41dd4bb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,10 @@
## Unreleased

### Changed
* Nodeinfo will only count users from database up to once per day, using a cached count for subsequent requests within 24 hours.
This can help limit query targeting warnings from mongo

### Ops
* Add [nock fetch work around](https://github.com/nock/nock/issues/2397) to fix tests in node 18.
* Adjust workflow to run tests using node 18 and 16. Don't run tests against 14.

Expand Down
16 changes: 15 additions & 1 deletion pub/nodeinfo.js
@@ -1,4 +1,18 @@
'use strict'

const USER_COUNT_FREQ = 24 * 60 * 60 * 1000
let lastUserCountTime = 0
let lastUserCount = Promise.resolve(0)

function getUserCountWithCache (store) {
const now = Date.now()
if (lastUserCountTime + USER_COUNT_FREQ < now) {
lastUserCountTime = now
lastUserCount = store.getUserCount()
}
return lastUserCount
}

module.exports = {
async generateNodeInfo (version) {
return {
Expand All @@ -10,7 +24,7 @@ module.exports = {
protocols: ['activitypub'],
services: { inbound: [], outbound: [] },
openRegistrations: !!this.settings.openRegistrations,
usage: { users: { total: await this.store.getUserCount() } },
usage: { users: { total: await getUserCountWithCache(this.store) } },
metadata: this.settings.nodeInfoMetadata || {}
}
}
Expand Down
23 changes: 22 additions & 1 deletion spec/functional/nodeinfo.spec.js
@@ -1,4 +1,4 @@
/* global describe, beforeAll, beforeEach, it */
/* global jasmine, describe, beforeAll, beforeEach, afterEach, it, spyOn, expect */
const request = require('supertest')

describe('nodeinfo', function () {
Expand Down Expand Up @@ -39,6 +39,12 @@ describe('nodeinfo', function () {
})
})
describe('document get', function () {
beforeEach(function () {
jasmine.clock().install()
})
afterEach(function () {
jasmine.clock().uninstall()
})
it('returns nodeinfo document', function (done) {
request(app)
.get('/nodeinfo/2.1')
Expand All @@ -57,6 +63,21 @@ describe('nodeinfo', function () {
}
}, err => global.failOrDone(err, done))
})
it('caches response for 1 day', async function () {
jasmine.clock().mockDate(new Date())
const count = (await apex.generateNodeInfo('2.0')).usage.users.total
const user = await apex.createActor('newuser', 'New user')
await apex.store.saveObject(user)
const querySpy = spyOn(apex.store, 'getUserCount').and.callThrough()
expect((await apex.generateNodeInfo('2.0')).usage.users.total).toBe(count)
expect(querySpy).toHaveBeenCalledTimes(0)
jasmine.clock().mockDate(new Date(Date.now() + 23 * 60 * 60 * 1000))
expect((await apex.generateNodeInfo('2.0')).usage.users.total).toBe(count)
expect(querySpy).toHaveBeenCalledTimes(0)
jasmine.clock().mockDate(new Date(Date.now() + 1 * 60 * 60 * 1000 + 1))
expect((await apex.generateNodeInfo('2.0')).usage.users.total).toBe(count + 1)
expect(querySpy).toHaveBeenCalledTimes(1)
})
it('404s on 1.x nodeinfo requests', function (done) {
request(app)
.get('/nodeinfo/1.0')
Expand Down

0 comments on commit 41dd4bb

Please sign in to comment.