2021-08-03 22:02:59 +03:00
|
|
|
//
|
2022-04-14 08:30:30 +03:00
|
|
|
// Copyright © 2022 Hardcore Engineering Inc.
|
2021-08-03 22:02:59 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2023-01-04 20:58:54 +03:00
|
|
|
import core, {
|
|
|
|
MeasureContext,
|
|
|
|
Ref,
|
|
|
|
Space,
|
2023-02-15 06:14:20 +03:00
|
|
|
Tx,
|
2023-01-04 20:58:54 +03:00
|
|
|
TxFactory,
|
2023-05-03 17:09:48 +03:00
|
|
|
WorkspaceId,
|
|
|
|
generateId,
|
|
|
|
toWorkspaceString
|
2023-01-04 20:58:54 +03:00
|
|
|
} from '@hcengineering/core'
|
2023-05-03 17:09:48 +03:00
|
|
|
import { Response, readRequest, unknownError } from '@hcengineering/platform'
|
2023-03-24 07:53:12 +03:00
|
|
|
import type { Pipeline, SessionContext } from '@hcengineering/server-core'
|
2023-05-03 17:09:48 +03:00
|
|
|
import { Token } from '@hcengineering/server-token'
|
|
|
|
// import WebSocket, { RawData } from 'ws'
|
|
|
|
|
|
|
|
import {
|
|
|
|
BroadcastCall,
|
|
|
|
ConnectionSocket,
|
|
|
|
LOGGING_ENABLED,
|
|
|
|
PipelineFactory,
|
|
|
|
ServerFactory,
|
|
|
|
Session,
|
|
|
|
SessionManager
|
|
|
|
} from './types'
|
2021-08-11 12:35:56 +03:00
|
|
|
|
2023-03-15 19:36:50 +03:00
|
|
|
function timeoutPromise (time: number): Promise<void> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, time)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-11 12:35:56 +03:00
|
|
|
interface Workspace {
|
2023-01-04 20:58:54 +03:00
|
|
|
id: string
|
|
|
|
pipeline: Promise<Pipeline>
|
2023-05-03 17:09:48 +03:00
|
|
|
sessions: [Session, ConnectionSocket][]
|
2022-05-23 18:53:33 +03:00
|
|
|
upgrade: boolean
|
2023-01-04 20:58:54 +03:00
|
|
|
closing?: Promise<void>
|
2021-08-11 12:35:56 +03:00
|
|
|
}
|
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
class TSessionManager implements SessionManager {
|
2023-04-20 13:11:22 +03:00
|
|
|
readonly workspaces = new Map<string, Workspace>()
|
2023-05-03 17:09:48 +03:00
|
|
|
checkInterval: any
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
readonly ctx: MeasureContext,
|
|
|
|
readonly sessionFactory: (token: Token, pipeline: Pipeline, broadcast: BroadcastCall) => Session
|
|
|
|
) {
|
|
|
|
this.checkInterval = setInterval(() => this.handleInterval(), 1000)
|
|
|
|
}
|
2021-08-11 12:35:56 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
handleInterval (): void {
|
|
|
|
for (const h of this.workspaces.entries()) {
|
|
|
|
for (const s of h[1].sessions) {
|
|
|
|
for (const r of s[0].requests.values()) {
|
|
|
|
const ed = Date.now()
|
|
|
|
|
|
|
|
if (ed - r.start > 30000) {
|
|
|
|
console.log(h[0], 'request hang found, 30sec', h[0], s[0].getUser(), r.params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 18:53:33 +03:00
|
|
|
|
|
|
|
createSession (token: Token, pipeline: Pipeline): Session {
|
|
|
|
return this.sessionFactory(token, pipeline, this.broadcast.bind(this))
|
|
|
|
}
|
|
|
|
|
2023-03-24 07:53:12 +03:00
|
|
|
upgradeId: string | undefined
|
|
|
|
|
2022-04-23 06:45:55 +03:00
|
|
|
async addSession (
|
|
|
|
ctx: MeasureContext,
|
2023-05-03 17:09:48 +03:00
|
|
|
ws: ConnectionSocket,
|
2022-04-23 06:45:55 +03:00
|
|
|
token: Token,
|
2023-02-15 06:14:20 +03:00
|
|
|
pipelineFactory: PipelineFactory,
|
2023-02-28 10:02:15 +03:00
|
|
|
productId: string,
|
|
|
|
sessionId?: string
|
2022-04-23 06:45:55 +03:00
|
|
|
): Promise<Session> {
|
2023-05-03 17:09:48 +03:00
|
|
|
return await ctx.with('add-session', {}, async (ctx) => {
|
|
|
|
const wsString = toWorkspaceString(token.workspace, '@')
|
2023-01-04 20:58:54 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
let workspace = this.workspaces.get(wsString)
|
|
|
|
await workspace?.closing
|
|
|
|
workspace = this.workspaces.get(wsString)
|
2023-01-04 20:58:54 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
if (workspace === undefined) {
|
|
|
|
workspace = this.createWorkspace(ctx, pipelineFactory, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.extra?.model === 'upgrade') {
|
|
|
|
return await this.createUpgradeSession(token, sessionId, ctx, wsString, workspace, pipelineFactory, ws)
|
|
|
|
}
|
2022-01-27 11:53:09 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
if (workspace.upgrade && sessionId !== this.upgradeId) {
|
|
|
|
ws.close()
|
|
|
|
throw new Error('Upgrade in progress....')
|
2022-05-23 18:53:33 +03:00
|
|
|
}
|
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
const pipeline = await ctx.with('pipeline', {}, async () => await (workspace as Workspace).pipeline)
|
|
|
|
|
2023-01-04 20:58:54 +03:00
|
|
|
const session = this.createSession(token, pipeline)
|
2023-05-03 17:09:48 +03:00
|
|
|
session.sessionId = sessionId
|
|
|
|
session.sessionInstanceId = generateId()
|
2021-08-11 12:35:56 +03:00
|
|
|
workspace.sessions.push([session, ws])
|
2023-05-03 17:09:48 +03:00
|
|
|
await ctx.with('set-status', {}, () => this.setStatus(ctx, session, true))
|
2021-08-11 12:35:56 +03:00
|
|
|
return session
|
2023-05-03 17:09:48 +03:00
|
|
|
})
|
|
|
|
}
|
2023-01-04 20:58:54 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
private async createUpgradeSession (
|
|
|
|
token: Token,
|
|
|
|
sessionId: string | undefined,
|
|
|
|
ctx: MeasureContext,
|
|
|
|
wsString: string,
|
|
|
|
workspace: Workspace,
|
|
|
|
pipelineFactory: PipelineFactory,
|
|
|
|
ws: ConnectionSocket
|
|
|
|
): Promise<Session> {
|
|
|
|
if (LOGGING_ENABLED) {
|
|
|
|
console.log(token.workspace.name, 'reloading workspace', JSON.stringify(token))
|
2023-01-04 20:58:54 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
this.upgradeId = sessionId
|
|
|
|
// If upgrade client is used.
|
|
|
|
// Drop all existing clients
|
|
|
|
await this.closeAll(ctx, wsString, workspace, 0, 'upgrade')
|
|
|
|
// Wipe workspace and update values.
|
|
|
|
if (!workspace.upgrade) {
|
|
|
|
// This is previous workspace, intended to be closed.
|
|
|
|
workspace.id = generateId()
|
|
|
|
workspace.sessions = []
|
|
|
|
workspace.upgrade = token.extra?.model === 'upgrade'
|
2023-02-28 10:02:15 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
if (LOGGING_ENABLED) {
|
|
|
|
console.log(token.workspace.name, 'no sessions for workspace', wsString)
|
|
|
|
}
|
|
|
|
// Re-create pipeline.
|
|
|
|
workspace.pipeline = pipelineFactory(ctx, token.workspace, true, (tx) => this.broadcastAll(workspace, tx))
|
2023-02-28 10:02:15 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
const pipeline = await workspace.pipeline
|
2023-01-04 20:58:54 +03:00
|
|
|
const session = this.createSession(token, pipeline)
|
|
|
|
workspace.sessions.push([session, ws])
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2023-02-15 06:14:20 +03:00
|
|
|
broadcastAll (workspace: Workspace, tx: Tx[]): void {
|
2023-05-03 17:09:48 +03:00
|
|
|
if (workspace?.upgrade ?? false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const ctx = this.ctx.newChild('broadcast-all', {})
|
|
|
|
const sessions = [...workspace.sessions]
|
|
|
|
function send (): void {
|
|
|
|
for (const session of sessions.splice(0, 1)) {
|
|
|
|
for (const _tx of tx) {
|
|
|
|
void session[1].send(ctx, { result: _tx })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sessions.length > 0) {
|
|
|
|
setImmediate(send)
|
|
|
|
} else {
|
|
|
|
ctx.end()
|
2023-02-15 06:14:20 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
send()
|
2023-02-15 06:14:20 +03:00
|
|
|
}
|
|
|
|
|
2023-03-02 06:31:47 +03:00
|
|
|
private createWorkspace (ctx: MeasureContext, pipelineFactory: PipelineFactory, token: Token): Workspace {
|
2023-01-04 20:58:54 +03:00
|
|
|
const upgrade = token.extra?.model === 'upgrade'
|
2023-02-15 06:14:20 +03:00
|
|
|
const workspace: Workspace = {
|
2023-01-04 20:58:54 +03:00
|
|
|
id: generateId(),
|
2023-03-02 06:31:47 +03:00
|
|
|
pipeline: pipelineFactory(ctx, token.workspace, upgrade, (tx) => this.broadcastAll(workspace, tx)),
|
2023-01-04 20:58:54 +03:00
|
|
|
sessions: [],
|
|
|
|
upgrade
|
|
|
|
}
|
2023-04-13 20:39:20 +03:00
|
|
|
if (LOGGING_ENABLED) console.time(token.workspace.name)
|
|
|
|
if (LOGGING_ENABLED) console.timeLog(token.workspace.name, 'Creating Workspace:', workspace.id)
|
2023-01-04 20:58:54 +03:00
|
|
|
this.workspaces.set(toWorkspaceString(token.workspace), workspace)
|
|
|
|
return workspace
|
2021-08-11 12:35:56 +03:00
|
|
|
}
|
|
|
|
|
2022-04-23 06:45:55 +03:00
|
|
|
private async setStatus (ctx: MeasureContext, session: Session, online: boolean): Promise<void> {
|
|
|
|
try {
|
|
|
|
const user = (
|
2023-03-01 19:46:36 +03:00
|
|
|
await session.findAll(
|
|
|
|
ctx,
|
2022-04-23 06:45:55 +03:00
|
|
|
core.class.Account,
|
|
|
|
{
|
|
|
|
email: session.getUser()
|
|
|
|
},
|
|
|
|
{ limit: 1 }
|
|
|
|
)
|
|
|
|
)[0]
|
|
|
|
if (user === undefined) return
|
|
|
|
const status = (await session.findAll(ctx, core.class.UserStatus, { modifiedBy: user._id }, { limit: 1 }))[0]
|
2023-04-05 10:12:06 +03:00
|
|
|
const txFactory = new TxFactory(user._id, true)
|
2022-04-23 06:45:55 +03:00
|
|
|
if (status === undefined) {
|
|
|
|
const tx = txFactory.createTxCreateDoc(core.class.UserStatus, user._id as string as Ref<Space>, {
|
|
|
|
online
|
|
|
|
})
|
|
|
|
await session.tx(ctx, tx)
|
|
|
|
} else if (status.online !== online) {
|
|
|
|
const tx = txFactory.createTxUpdateDoc(status._class, status.space, status._id, {
|
|
|
|
online
|
|
|
|
})
|
|
|
|
await session.tx(ctx, tx)
|
|
|
|
}
|
2022-04-29 08:27:17 +03:00
|
|
|
} catch {}
|
2022-04-23 06:45:55 +03:00
|
|
|
}
|
|
|
|
|
2022-11-16 18:03:03 +03:00
|
|
|
async close (
|
|
|
|
ctx: MeasureContext,
|
2023-05-03 17:09:48 +03:00
|
|
|
ws: ConnectionSocket,
|
2022-11-16 18:03:03 +03:00
|
|
|
workspaceId: WorkspaceId,
|
|
|
|
code: number,
|
|
|
|
reason: string
|
|
|
|
): Promise<void> {
|
2023-05-03 17:09:48 +03:00
|
|
|
// if (LOGGING_ENABLED) console.log(workspaceId.name, `closing websocket, code: ${code}, reason: ${reason}`)
|
2022-11-16 18:03:03 +03:00
|
|
|
const wsid = toWorkspaceString(workspaceId)
|
|
|
|
const workspace = this.workspaces.get(wsid)
|
2021-08-11 12:35:56 +03:00
|
|
|
if (workspace === undefined) {
|
2023-04-13 20:39:20 +03:00
|
|
|
if (LOGGING_ENABLED) console.error(new Error('internal: cannot find sessions'))
|
2022-01-27 11:53:09 +03:00
|
|
|
return
|
2021-08-11 12:35:56 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
const index = workspace.sessions.findIndex((p) => p[1].id === ws.id)
|
2022-04-23 06:45:55 +03:00
|
|
|
if (index !== -1) {
|
|
|
|
const session = workspace.sessions[index]
|
|
|
|
workspace.sessions.splice(index, 1)
|
2023-05-03 17:09:48 +03:00
|
|
|
try {
|
|
|
|
session[1].close()
|
|
|
|
} catch (err) {
|
|
|
|
// Ignore if closed
|
|
|
|
}
|
2022-04-23 06:45:55 +03:00
|
|
|
const user = session[0].getUser()
|
|
|
|
const another = workspace.sessions.findIndex((p) => p[0].getUser() === user)
|
|
|
|
if (another === -1) {
|
|
|
|
await this.setStatus(ctx, session[0], false)
|
|
|
|
}
|
|
|
|
if (workspace.sessions.length === 0) {
|
2023-04-13 20:39:20 +03:00
|
|
|
const wsUID = workspace.id
|
2023-04-20 13:11:22 +03:00
|
|
|
if (LOGGING_ENABLED) console.log(workspaceId.name, 'no sessions for workspace', wsid, wsUID)
|
2023-01-04 20:58:54 +03:00
|
|
|
|
2023-03-15 19:36:50 +03:00
|
|
|
const waitAndClose = async (workspace: Workspace): Promise<void> => {
|
|
|
|
try {
|
|
|
|
const pl = await workspace.pipeline
|
|
|
|
await Promise.race([pl, timeoutPromise(60000)])
|
|
|
|
await Promise.race([pl.close(), timeoutPromise(60000)])
|
|
|
|
|
2023-04-13 20:39:20 +03:00
|
|
|
if (this.workspaces.get(wsid)?.id === wsUID) {
|
2023-03-15 19:36:50 +03:00
|
|
|
this.workspaces.delete(wsid)
|
|
|
|
}
|
2023-04-13 20:39:20 +03:00
|
|
|
if (LOGGING_ENABLED) console.timeLog(workspaceId.name, 'Closed workspace', wsUID)
|
2023-03-15 19:36:50 +03:00
|
|
|
} catch (err: any) {
|
2023-01-04 20:58:54 +03:00
|
|
|
this.workspaces.delete(wsid)
|
2023-04-20 13:11:22 +03:00
|
|
|
if (LOGGING_ENABLED) console.error(workspaceId.name, err)
|
2023-01-04 20:58:54 +03:00
|
|
|
}
|
2023-03-15 19:36:50 +03:00
|
|
|
}
|
|
|
|
workspace.closing = waitAndClose(workspace)
|
2023-01-04 20:58:54 +03:00
|
|
|
await workspace.closing
|
2022-04-23 06:45:55 +03:00
|
|
|
}
|
2021-08-11 12:35:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 07:53:12 +03:00
|
|
|
async closeAll (
|
|
|
|
ctx: MeasureContext,
|
|
|
|
wsId: string,
|
|
|
|
workspace: Workspace,
|
|
|
|
code: number,
|
|
|
|
reason: 'upgrade' | 'shutdown'
|
|
|
|
): Promise<void> {
|
2023-04-13 20:39:20 +03:00
|
|
|
if (LOGGING_ENABLED) console.timeLog(wsId, `closing workspace ${workspace.id}, code: ${code}, reason: ${reason}`)
|
2023-01-04 20:58:54 +03:00
|
|
|
|
|
|
|
const sessions = Array.from(workspace.sessions)
|
|
|
|
workspace.sessions = []
|
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
const closeS = async (s: Session, webSocket: ConnectionSocket): Promise<void> => {
|
2023-03-24 07:53:12 +03:00
|
|
|
s.workspaceClosed = true
|
|
|
|
if (reason === 'upgrade') {
|
2023-05-03 17:09:48 +03:00
|
|
|
// Override message handler, to wait for upgrading response from clients.
|
2023-05-04 15:34:51 +03:00
|
|
|
await webSocket.send(ctx, {
|
2023-05-03 17:09:48 +03:00
|
|
|
result: {
|
|
|
|
_class: core.class.TxModelUpgrade
|
|
|
|
}
|
2023-03-24 07:53:12 +03:00
|
|
|
})
|
|
|
|
}
|
2023-01-26 16:53:00 +03:00
|
|
|
webSocket.close()
|
|
|
|
await this.setStatus(ctx, s, false)
|
2023-01-04 20:58:54 +03:00
|
|
|
}
|
2023-01-26 16:53:00 +03:00
|
|
|
|
2023-04-13 20:39:20 +03:00
|
|
|
if (LOGGING_ENABLED) console.timeLog(wsId, workspace.id, 'Clients disconnected. Closing Workspace...')
|
2023-01-26 16:53:00 +03:00
|
|
|
await Promise.all(sessions.map((s) => closeS(s[0], s[1])))
|
|
|
|
|
|
|
|
const closePipeline = async (): Promise<void> => {
|
|
|
|
try {
|
2023-04-13 20:39:20 +03:00
|
|
|
if (LOGGING_ENABLED) console.timeLog(wsId, 'closing pipeline')
|
2023-01-26 16:53:00 +03:00
|
|
|
await (await workspace.pipeline).close()
|
2023-04-13 20:39:20 +03:00
|
|
|
if (LOGGING_ENABLED) console.timeLog(wsId, 'closing pipeline done')
|
2023-01-26 16:53:00 +03:00
|
|
|
} catch (err: any) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2023-01-04 20:58:54 +03:00
|
|
|
}
|
2023-03-15 19:36:50 +03:00
|
|
|
await Promise.race([closePipeline(), timeoutPromise(15000)])
|
2023-04-13 20:39:20 +03:00
|
|
|
if (LOGGING_ENABLED) console.timeLog(wsId, 'Workspace closed...')
|
|
|
|
console.timeEnd(wsId)
|
2023-01-04 20:58:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async closeWorkspaces (ctx: MeasureContext): Promise<void> {
|
|
|
|
for (const w of this.workspaces) {
|
|
|
|
await this.closeAll(ctx, w[0], w[1], 1, 'shutdown')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 06:31:47 +03:00
|
|
|
broadcast (from: Session | null, workspaceId: WorkspaceId, resp: Response<any>, target?: string[]): void {
|
2022-11-16 18:03:03 +03:00
|
|
|
const workspace = this.workspaces.get(toWorkspaceString(workspaceId))
|
2021-08-11 12:35:56 +03:00
|
|
|
if (workspace === undefined) {
|
2022-01-27 11:53:09 +03:00
|
|
|
console.error(new Error('internal: cannot find sessions'))
|
|
|
|
return
|
2021-08-11 12:35:56 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
if (workspace?.upgrade ?? false) {
|
|
|
|
return
|
|
|
|
}
|
2023-04-20 13:11:22 +03:00
|
|
|
if (LOGGING_ENABLED) console.log(workspaceId.name, `server broadcasting to ${workspace.sessions.length} clients...`)
|
2023-05-03 17:09:48 +03:00
|
|
|
|
|
|
|
const sessions = [...workspace.sessions]
|
|
|
|
const ctx = this.ctx.newChild('broadcast', {})
|
|
|
|
function send (): void {
|
|
|
|
for (const session of sessions.splice(0, 1)) {
|
|
|
|
if (session[0] !== from) {
|
|
|
|
if (target === undefined) {
|
|
|
|
void session[1].send(ctx, resp)
|
|
|
|
} else if (target.includes(session[0].getUser())) {
|
|
|
|
void session[1].send(ctx, resp)
|
|
|
|
}
|
2022-04-14 08:30:30 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
if (sessions.length > 0) {
|
|
|
|
setImmediate(send)
|
|
|
|
} else {
|
|
|
|
ctx.end()
|
|
|
|
}
|
2021-08-11 12:35:56 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
send()
|
2021-08-11 12:35:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
async function handleSend (
|
2022-04-23 06:45:55 +03:00
|
|
|
ctx: MeasureContext,
|
2023-05-03 17:09:48 +03:00
|
|
|
ws: ConnectionSocket,
|
|
|
|
msg: Response<any>,
|
|
|
|
chunkLimit: number
|
2022-04-23 06:45:55 +03:00
|
|
|
): Promise<void> {
|
2023-05-03 17:09:48 +03:00
|
|
|
// ws.send(msg)
|
|
|
|
if (Array.isArray(msg.result) && chunkLimit > 0 && msg.result.length > chunkLimit) {
|
|
|
|
// Split and send by chunks
|
|
|
|
const data = [...msg.result]
|
|
|
|
|
|
|
|
let cid = 1
|
|
|
|
while (data.length > 0) {
|
|
|
|
const chunk = data.splice(0, chunkLimit)
|
|
|
|
if (chunk !== undefined) {
|
|
|
|
await ws.send(ctx, { ...msg, result: chunk, chunk: { index: cid, final: data.length === 0 } })
|
2023-04-13 20:39:20 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
cid++
|
2021-12-22 12:02:51 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
} else {
|
|
|
|
await ws.send(ctx, msg)
|
2021-12-06 18:16:38 +03:00
|
|
|
}
|
2021-08-03 22:02:59 +03:00
|
|
|
}
|
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
async function handleRequest<S extends Session> (
|
|
|
|
rctx: MeasureContext,
|
|
|
|
service: S,
|
|
|
|
ws: ConnectionSocket,
|
|
|
|
msg: string,
|
|
|
|
workspace: string,
|
|
|
|
chunkLimit: number
|
|
|
|
): Promise<void> {
|
|
|
|
const userCtx = rctx.newChild('client', { workspace }) as SessionContext
|
|
|
|
userCtx.sessionId = service.sessionInstanceId ?? ''
|
2021-08-19 20:34:58 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
const reqId = generateId()
|
|
|
|
|
|
|
|
const st = Date.now()
|
|
|
|
try {
|
|
|
|
await userCtx.with('handleRequest', {}, async (ctx) => {
|
|
|
|
const request = await ctx.with('read', {}, async () => readRequest(msg))
|
|
|
|
if (request.id === -1 && request.method === 'hello') {
|
|
|
|
if (LOGGING_ENABLED) console.timeLog(workspace, 'hello happen', service.getUser())
|
|
|
|
await ws.send(ctx, { id: -1, result: 'hello' })
|
2023-03-24 07:53:12 +03:00
|
|
|
return
|
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
service.requests.set(reqId, {
|
|
|
|
id: reqId,
|
|
|
|
params: request,
|
|
|
|
start: st
|
2023-04-20 13:11:22 +03:00
|
|
|
})
|
2023-05-03 17:09:48 +03:00
|
|
|
if (request.id === -1 && request.method === '#upgrade') {
|
|
|
|
ws.close()
|
|
|
|
return
|
2023-04-20 13:11:22 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
const f = (service as any)[request.method]
|
|
|
|
try {
|
|
|
|
const params = [...request.params]
|
2023-02-28 10:02:15 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
const result = await ctx.with('call', {}, async (callTx) => f.apply(service, [callTx, ...params]))
|
2022-11-16 18:03:03 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
const resp: Response<any> = { id: request.id, result }
|
2022-11-16 18:03:03 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
await handleSend(ctx, ws, resp, chunkLimit)
|
|
|
|
} catch (err: any) {
|
|
|
|
if (LOGGING_ENABLED) console.error(err)
|
2022-07-05 08:43:17 +03:00
|
|
|
const resp: Response<any> = {
|
2023-05-03 17:09:48 +03:00
|
|
|
id: request.id,
|
|
|
|
error: unknownError(err)
|
2022-07-05 08:43:17 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
await ws.send(ctx, resp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
userCtx.end()
|
|
|
|
service.requests.delete(reqId)
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 22:02:59 +03:00
|
|
|
|
2023-05-03 17:09:48 +03:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function start (
|
|
|
|
ctx: MeasureContext,
|
|
|
|
opt: {
|
|
|
|
port: number
|
|
|
|
pipelineFactory: PipelineFactory
|
|
|
|
sessionFactory: (token: Token, pipeline: Pipeline, broadcast: BroadcastCall) => Session
|
|
|
|
productId: string
|
|
|
|
serverFactory: ServerFactory
|
|
|
|
chunking: number // 25
|
2021-11-22 14:17:10 +03:00
|
|
|
}
|
2023-05-03 17:09:48 +03:00
|
|
|
): () => Promise<void> {
|
|
|
|
const sessions = new TSessionManager(ctx, opt.sessionFactory)
|
|
|
|
return opt.serverFactory(
|
|
|
|
sessions,
|
|
|
|
(rctx, service, ws, msg, workspace) => handleRequest(rctx, service, ws, msg, workspace, opt.chunking),
|
|
|
|
ctx,
|
|
|
|
opt.pipelineFactory,
|
|
|
|
opt.port,
|
|
|
|
opt.productId
|
|
|
|
)
|
2021-08-03 22:02:59 +03:00
|
|
|
}
|