Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server/core): generalize caching mechanism, and update getServerInfoFactory to rely on getServerConfig dependency #3791

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/server/healthchecks/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Knex } from 'knex'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import { getServerConfigFactory } from '@/modules/core/repositories/server'
import { getAllRegisteredDbClients } from '@/modules/multiregion/utils/dbSelector'
import type { CheckResponse, MultiDBCheck } from '@/healthchecks/types'
import { ensureErrorOrWrapAsCause } from '@/modules/shared/errors/ensureError'
Expand All @@ -9,10 +9,10 @@ type DBCheck = (params: { db: Knex }) => Promise<CheckResponse>

export const isPostgresAlive: DBCheck = async (params) => {
const { db } = params
const getServerInfo = getServerInfoFactory({ db })
const getServerConfig = getServerConfigFactory({ db })

try {
await getServerInfo()
await getServerConfig({ bustCache: true }) // we always want this to hit the database, so it should not be cached
} catch (err) {
return { isAlive: false, err: ensureError(err, 'Unknown Postgres error.') }
}
Expand Down
9 changes: 7 additions & 2 deletions packages/server/modules/activitystream/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import { dispatchStreamEventFactory } from '@/modules/webhooks/services/webhooks
import { Knex } from 'knex'
import { getStreamFactory } from '@/modules/core/repositories/streams'
import { getUserFactory } from '@/modules/core/repositories/users'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import {
getServerConfigFactory,
getServerInfoFactory
} from '@/modules/core/repositories/server'
import { getProjectDbClient } from '@/modules/multiregion/utils/dbSelector'

const tables = {
Expand Down Expand Up @@ -259,7 +262,9 @@ export const saveActivityFactory =
// the whole activity module will need to be refactored to use the eventBus
await dispatchStreamEventFactory({
getStreamWebhooks: getStreamWebhooksFactory({ db: projectDb }),
getServerInfo: getServerInfoFactory({ db }),
getServerInfo: getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
}),
getStream: getStreamFactory({ db: projectDb }),
createWebhookEvent: createWebhookEventFactory({ db: projectDb }),
getUser: getUserFactory({ db })
Expand Down
13 changes: 10 additions & 3 deletions packages/server/modules/activitystream/tests/activity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ const {
storeTokenScopesFactory,
storeTokenResourceAccessDefinitionsFactory
} = require('@/modules/core/repositories/tokens')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')
const {
getServerInfoFactory,
getServerConfigFactory
} = require('@/modules/core/repositories/server')
const { createObjectFactory } = require('@/modules/core/services/objects/management')
const {
storeSingleObjectIfNotFoundFactory,
Expand Down Expand Up @@ -88,13 +91,17 @@ const findEmail = findEmailFactory({ db })
const requestNewEmailVerification = requestNewEmailVerificationFactory({
findEmail,
getUser: getUserFactory({ db }),
getServerInfo: getServerInfoFactory({ db }),
getServerInfo: getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
}),
deleteOldAndInsertNewVerification: deleteOldAndInsertNewVerificationFactory({ db }),
renderEmail,
sendEmail
})
const createUser = createUserFactory({
getServerInfo: getServerInfoFactory({ db }),
getServerInfo: getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
}),
findEmail,
storeUser: storeUserFactory({ db }),
countAdminUsers: countAdminUsersFactory({ db }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ import { buildCoreInviteEmailContentsFactory } from '@/modules/serverinvites/ser
import { getEventBus } from '@/modules/shared/services/eventBus'
import { createBranchFactory } from '@/modules/core/repositories/branches'
import { getUserFactory, getUsersFactory } from '@/modules/core/repositories/users'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import {
getServerConfigFactory,
getServerInfoFactory
} from '@/modules/core/repositories/server'

const cleanup = async () => {
await truncateTables([StreamActivity.name, Users.name])
}

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const getUser = getUserFactory({ db })
const getUsers = getUsersFactory({ db })
const getStream = getStreamFactory({ db })
Expand Down
17 changes: 13 additions & 4 deletions packages/server/modules/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,28 @@ import { requestNewEmailVerificationFactory } from '@/modules/emails/services/ve
import { deleteOldAndInsertNewVerificationFactory } from '@/modules/emails/repositories'
import { renderEmail } from '@/modules/emails/services/emailRendering'
import { sendEmail } from '@/modules/emails/services/sending'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import {
getServerConfigFactory,
getServerInfoFactory
} from '@/modules/core/repositories/server'
import { initializeEventListenerFactory } from '@/modules/auth/services/postAuth'
import { getEventBus } from '@/modules/shared/services/eventBus'

const findEmail = findEmailFactory({ db })
const requestNewEmailVerification = requestNewEmailVerificationFactory({
findEmail,
getUser: getUserFactory({ db }),
getServerInfo: getServerInfoFactory({ db }),
getServerInfo: getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
}),
deleteOldAndInsertNewVerification: deleteOldAndInsertNewVerificationFactory({ db }),
renderEmail,
sendEmail
})
const createUser = createUserFactory({
getServerInfo: getServerInfoFactory({ db }),
getServerInfo: getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
}),
findEmail,
storeUser: storeUserFactory({ db }),
countAdminUsers: countAdminUsersFactory({ db }),
Expand Down Expand Up @@ -111,7 +118,9 @@ const finalizeInvitedServerRegistration = finalizeInvitedServerRegistrationFacto
const resolveAuthRedirectPath = resolveAuthRedirectPathFactory()

const commonBuilderDeps = {
getServerInfo: getServerInfoFactory({ db }),
getServerInfo: getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
}),
getUserByEmail: legacyGetUserByEmailFactory({ db }),
findOrCreateUser,
validateServerInvite,
Expand Down
9 changes: 7 additions & 2 deletions packages/server/modules/auth/tests/apps.graphql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ const {
storeUserServerAppTokenFactory,
storePersonalApiTokenFactory
} = require('@/modules/core/repositories/tokens')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')
const {
getServerInfoFactory,
getServerConfigFactory
} = require('@/modules/core/repositories/server')
const { getEventBus } = require('@/modules/shared/services/eventBus')

let sendRequest
Expand All @@ -83,7 +86,9 @@ const createAppTokenFromAccessCode = createAppTokenFromAccessCodeFactory({
createBareToken
})

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const findEmail = findEmailFactory({ db })
const requestNewEmailVerification = requestNewEmailVerificationFactory({
findEmail,
Expand Down
9 changes: 7 additions & 2 deletions packages/server/modules/auth/tests/apps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ const {
getTokenResourceAccessDefinitionsByIdFactory,
updateApiTokenFactory
} = require('@/modules/core/repositories/tokens')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')
const {
getServerInfoFactory,
getServerConfigFactory
} = require('@/modules/core/repositories/server')
const { getEventBus } = require('@/modules/shared/services/eventBus')

const db = knex
Expand Down Expand Up @@ -115,7 +118,9 @@ const refreshAppToken = refreshAppTokenFactory({
createBareToken
})

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const findEmail = findEmailFactory({ db })
const requestNewEmailVerification = requestNewEmailVerificationFactory({
findEmail,
Expand Down
7 changes: 5 additions & 2 deletions packages/server/modules/auth/tests/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ const {
} = require('@/modules/serverinvites/services/processing')
const {
getServerInfoFactory,
updateServerInfoFactory
updateServerInfoFactory,
getServerConfigFactory
} = require('@/modules/core/repositories/server')

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const getUser = getUserFactory({ db })
const getUsers = getUsersFactory({ db })
const createInviteDirectly = createStreamInviteDirectly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ const {
const {
finalizeInvitedServerRegistrationFactory
} = require('@/modules/serverinvites/services/processing')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')
const {
getServerInfoFactory,
getServerConfigFactory
} = require('@/modules/core/repositories/server')

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const getUser = getUserFactory({ db })
const getUsers = getUsersFactory({ db })
const getStream = getStreamFactory({ db })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import {
storeTokenScopesFactory,
storeTokenResourceAccessDefinitionsFactory
} from '@/modules/core/repositories/tokens'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import {
getServerConfigFactory,
getServerInfoFactory
} from '@/modules/core/repositories/server'
import { BasicTestStream, createTestStream } from '@/test/speckle-helpers/streamHelper'
import { waitForRegionUser } from '@/test/speckle-helpers/regions'
import { createTestWorkspace } from '@/modules/workspaces/tests/helpers/creation'
Expand All @@ -43,7 +46,9 @@ import cryptoRandomString from 'crypto-random-string'
import type { BlobStorageItem } from '@/modules/blobstorage/domain/types'
import { getEventBus } from '@/modules/shared/services/eventBus'

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})

const findEmail = findEmailFactory({ db })
const requestNewEmailVerification = requestNewEmailVerificationFactory({
Expand Down
9 changes: 7 additions & 2 deletions packages/server/modules/comments/tests/comments.graph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ const {
const {
finalizeInvitedServerRegistrationFactory
} = require('@/modules/serverinvites/services/processing')
const { getServerInfoFactory } = require('@/modules/core/repositories/server')
const {
getServerInfoFactory,
getServerConfigFactory
} = require('@/modules/core/repositories/server')
const { createObjectFactory } = require('@/modules/core/services/objects/management')

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const getUser = getUserFactory({ db })
const getUsers = getUsersFactory({ db })
const markCommitStreamUpdated = markCommitStreamUpdatedFactory({ db })
Expand Down
9 changes: 7 additions & 2 deletions packages/server/modules/comments/tests/comments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ import { sendEmail } from '@/modules/emails/services/sending'
import { createUserFactory } from '@/modules/core/services/users/management'
import { validateAndCreateUserEmailFactory } from '@/modules/core/services/userEmails'
import { finalizeInvitedServerRegistrationFactory } from '@/modules/serverinvites/services/processing'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import {
getServerConfigFactory,
getServerInfoFactory
} from '@/modules/core/repositories/server'
import { createObjectFactory } from '@/modules/core/services/objects/management'
import type express from 'express'
import { ResourceType } from '@/modules/comments/domain/types'
Expand All @@ -127,7 +130,9 @@ type LegacyCommentRecord = CommentRecord & {
resources: Array<{ resourceId: string; resourceType: string }>
}

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const getUser = getUserFactory({ db })
const getUsers = getUsersFactory({ db })
const getStream = getStreamFactory({ db })
Expand Down
14 changes: 14 additions & 0 deletions packages/server/modules/core/cache/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ServerConfigRecord } from '@/modules/core/helpers/types'
import TTLCache from '@isaacs/ttlcache'

let serverConfigCache: TTLCache<string, ServerConfigRecord> | undefined

export const getServerConfigCache = () => {
if (!serverConfigCache)
serverConfigCache = new TTLCache<string, ServerConfigRecord>({
max: 1,
ttl: 60 * 1000
})

return serverConfigCache
}
9 changes: 7 additions & 2 deletions packages/server/modules/core/graph/resolvers/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ import {
storeProjectFactory,
storeProjectRoleFactory
} from '@/modules/core/repositories/projects'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import {
getServerConfigFactory,
getServerInfoFactory
} from '@/modules/core/repositories/server'
import {
getStreamFactory,
getStreamCollaboratorsFactory,
Expand Down Expand Up @@ -101,7 +104,9 @@ import {
} from '@/modules/shared/utils/subscriptions'
import { has } from 'lodash'

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const getUsers = getUsersFactory({ db })
const getUser = getUserFactory({ db })
const saveActivity = saveActivityFactory({ db })
Expand Down
24 changes: 10 additions & 14 deletions packages/server/modules/core/graph/resolvers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,26 @@ import {
updateServerInfoFactory,
getPublicRolesFactory,
getPublicScopesFactory,
getServerInfoFromCacheFactory,
storeServerInfoInCacheFactory
getServerConfigViaCacheFactory
} from '@/modules/core/repositories/server'
import { db } from '@/db/knex'
import { Resolvers } from '@/modules/core/graph/generated/graphql'
import { LRUCache } from 'lru-cache'
import { ServerInfo } from '@/modules/core/helpers/types'
import { getServerConfigCache } from '@/modules/core/cache/cache'

const cache = new LRUCache<string, ServerInfo>({ max: 1, ttl: 60 * 1000 })
const getServerInfoFromCache = getServerInfoFromCacheFactory({ cache })
const storeServerInfoInCache = storeServerInfoInCacheFactory({ cache })
const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigViaCacheFactory({
cache: getServerConfigCache(),
db
})
})
const updateServerInfo = updateServerInfoFactory({ db })
const getPublicRoles = getPublicRolesFactory({ db })
const getPublicScopes = getPublicScopesFactory({ db })

export = {
Query: {
async serverInfo() {
const cachedServerInfo = getServerInfoFromCache()
if (cachedServerInfo) return cachedServerInfo
const serverInfo = await getServerInfo()
storeServerInfoInCache({ serverInfo })
return serverInfo
return await getServerInfo()
}
},
ServerInfo: {
Expand Down Expand Up @@ -71,7 +67,7 @@ export = {
await updateServerInfo(update)
// we're currently going to ignore, that this should be propagated to all
// backend instances, and going to rely on the TTL in the cache to propagate the changes
cache.clear()
getServerConfigCache().clear()
return true
},
serverInfoMutations: () => ({})
Expand Down
9 changes: 7 additions & 2 deletions packages/server/modules/core/graph/resolvers/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,15 @@ import {
getFavoriteStreamsCollectionFactory
} from '@/modules/core/services/streams/favorite'
import { getUserFactory, getUsersFactory } from '@/modules/core/repositories/users'
import { getServerInfoFactory } from '@/modules/core/repositories/server'
import {
getServerConfigFactory,
getServerInfoFactory
} from '@/modules/core/repositories/server'
import { adminOverrideEnabled } from '@/modules/shared/helpers/envHelper'

const getServerInfo = getServerInfoFactory({ db })
const getServerInfo = getServerInfoFactory({
getServerConfig: getServerConfigFactory({ db })
})
const getUsers = getUsersFactory({ db })
const getUser = getUserFactory({ db })
const getFavoriteStreamsCollection = getFavoriteStreamsCollectionFactory({
Expand Down
Loading