Skip to content

Commit

Permalink
UBERF-7266: Fix workspace rate limit (#5812)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Sobolev <[email protected]>
  • Loading branch information
haiodo authored Jun 14, 2024
1 parent ce40704 commit dda0446
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
23 changes: 13 additions & 10 deletions server/account-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,19 @@ export function serveAccount (
// We need to clean workspace with creating === true, since server is restarted.
void cleanInProgressWorkspaces(db, productId)

worker = new UpgradeWorker(db, p, version, txes, migrateOperations, productId)
await worker.upgradeAll(measureCtx, {
errorHandler: async (ws, err) => {
Analytics.handleError(err)
},
force: false,
console: false,
logs: 'upgrade-logs',
parallel: parseInt(process.env.PARALLEL ?? '1')
})
const performUpgrade = (process.env.PERFORM_UPGRADE ?? 'true') === 'true'
if (performUpgrade) {
worker = new UpgradeWorker(db, p, version, txes, migrateOperations, productId)
await worker.upgradeAll(measureCtx, {
errorHandler: async (ws, err) => {
Analytics.handleError(err)
},
force: false,
console: false,
logs: 'upgrade-logs',
parallel: parseInt(process.env.PARALLEL ?? '1')
})
}
})

const extractToken = (header: IncomingHttpHeaders): string | undefined => {
Expand Down
31 changes: 16 additions & 15 deletions server/account/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ async function generateWorkspaceRecord (

let searchPromise: Promise<Workspace> | undefined

const rateLimiter = new RateLimiter(3)
const rateLimiter = new RateLimiter(parseInt(process.env.RATELIMIT ?? '10'))

/**
* @public
Expand All @@ -905,26 +905,26 @@ export async function createWorkspace (
notifyHandler?: (workspace: Workspace) => void,
postInitHandler?: (workspace: Workspace, model: Tx[]) => Promise<void>
): Promise<{ workspaceInfo: Workspace, err?: any, model?: Tx[] }> {
return await rateLimiter.exec(async () => {
// We need to search for duplicate workspaceUrl
await searchPromise
// We need to search for duplicate workspaceUrl
await searchPromise

// Safe generate workspace record.
searchPromise = generateWorkspaceRecord(db, email, productId, version, workspaceName, workspace)
// Safe generate workspace record.
searchPromise = generateWorkspaceRecord(db, email, productId, version, workspaceName, workspace)

const workspaceInfo = await searchPromise
const workspaceInfo = await searchPromise

notifyHandler?.(workspaceInfo)
notifyHandler?.(workspaceInfo)

const wsColl = db.collection<Omit<Workspace, '_id'>>(WORKSPACE_COLLECTION)
const wsColl = db.collection<Omit<Workspace, '_id'>>(WORKSPACE_COLLECTION)

async function updateInfo (ops: Partial<Workspace>): Promise<void> {
await wsColl.updateOne({ _id: workspaceInfo._id }, { $set: ops })
console.log('update', ops)
}
async function updateInfo (ops: Partial<Workspace>): Promise<void> {
await wsColl.updateOne({ _id: workspaceInfo._id }, { $set: ops })
console.log('update', ops)
}

await updateInfo({ createProgress: 10 })
await updateInfo({ createProgress: 10 })

return await rateLimiter.exec(async () => {
const childLogger = ctx.newChild('createWorkspace', { workspace: workspaceInfo.workspace })
const ctxModellogger: ModelLogger = {
log: (msg, data) => {
Expand Down Expand Up @@ -959,7 +959,8 @@ export async function createWorkspace (
true,
async (value) => {
await updateInfo({ createProgress: 20 + Math.round((Math.min(value, 100) / 100) * 30) })
}
},
true
)
await updateInfo({ createProgress: 50 })
model = await upgradeModel(
Expand Down
17 changes: 15 additions & 2 deletions server/backup/src/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import core, {
Client as CoreClient,
Doc,
Domain,
DOMAIN_FULLTEXT_BLOB,
DOMAIN_MODEL,
DOMAIN_TRANSIENT,
MeasureContext,
Expand All @@ -29,7 +30,8 @@ import core, {
SortingOrder,
TxCollectionCUD,
WorkspaceId,
type Blob
type Blob,
type DocIndexState
} from '@hcengineering/core'
import { BlobClient, connect } from '@hcengineering/server-tool'
import { createGzip } from 'node:zlib'
Expand Down Expand Up @@ -223,7 +225,8 @@ export async function cloneWorkspace (
sourceWorkspaceId: WorkspaceId,
targetWorkspaceId: WorkspaceId,
clearTime: boolean = true,
progress: (value: number) => Promise<void>
progress: (value: number) => Promise<void>,
skipFullText: boolean
): Promise<void> {
const sourceConnection = (await connect(transactorUrl, sourceWorkspaceId, undefined, {
mode: 'backup'
Expand All @@ -245,6 +248,10 @@ export async function cloneWorkspace (

let i = 0
for (const c of domains) {
if (skipFullText && c === DOMAIN_FULLTEXT_BLOB) {
console.log('clone skip domain...', c)
continue
}
console.log('clone domain...', c)

// We need to clean target connection before copying something.
Expand Down Expand Up @@ -321,6 +328,12 @@ export async function cloneWorkspace (
} catch (err: any) {
console.log(err)
}

// if full text is skipped, we need to clean stages for indexes.
if (p._class === core.class.DocIndexState && skipFullText) {
;(p as DocIndexState).stages = {}
}

if (collectionCud) {
return {
...p,
Expand Down

0 comments on commit dda0446

Please sign in to comment.