mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 09:24:35 +00:00
feat(session-manager): improving performance
This commit is contained in:
@@ -14,6 +14,7 @@ export class SessionManager {
|
|||||||
private contextName: string,
|
private contextName: string,
|
||||||
private requestClient: RequestClient
|
private requestClient: RequestClient
|
||||||
) {
|
) {
|
||||||
|
console.log(`🤖[SessionManager constructor]🤖`)
|
||||||
if (serverUrl) isUrl(serverUrl)
|
if (serverUrl) isUrl(serverUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,27 +35,76 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getSession(accessToken?: string) {
|
async getSession(accessToken?: string) {
|
||||||
await this.createSessions(accessToken)
|
console.log(`🤖[]🤖`)
|
||||||
await this.createAndWaitForSession(accessToken)
|
console.log(`🤖[---- SessionManager getSession start]🤖`)
|
||||||
const session = this.sessions.pop()
|
console.log(
|
||||||
const secondsSinceSessionCreation =
|
`🤖[this.sessions]🤖`,
|
||||||
(new Date().getTime() - new Date(session!.creationTimeStamp).getTime()) /
|
this.sessions.map((session: any) => session.id)
|
||||||
1000
|
)
|
||||||
|
|
||||||
if (
|
if (this.sessions.length) {
|
||||||
!session!.attributes ||
|
const session = this.sessions[0]
|
||||||
secondsSinceSessionCreation >= session!.attributes.sessionInactiveTimeout
|
|
||||||
) {
|
this.createSessions(accessToken)
|
||||||
|
this.createAndWaitForSession(accessToken)
|
||||||
|
|
||||||
|
// TODO: check secondsSinceSessionCreation
|
||||||
|
|
||||||
|
return session
|
||||||
|
} else {
|
||||||
await this.createSessions(accessToken)
|
await this.createSessions(accessToken)
|
||||||
const freshSession = this.sessions.pop()
|
console.log(
|
||||||
|
`🤖[ 45 this.sessions]🤖`,
|
||||||
|
this.sessions.map((session: any) => session.id)
|
||||||
|
)
|
||||||
|
await this.createAndWaitForSession(accessToken)
|
||||||
|
console.log(
|
||||||
|
`🤖[ 50 this.sessions]🤖`,
|
||||||
|
this.sessions.map((session: any) => session.id)
|
||||||
|
)
|
||||||
|
|
||||||
return freshSession
|
const session = this.sessions.pop()
|
||||||
|
console.log(`🤖[session]🤖`, session!.id)
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`🤖[59 this.sessions]🤖`,
|
||||||
|
this.sessions.map((session: any) => session.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
const secondsSinceSessionCreation =
|
||||||
|
(new Date().getTime() -
|
||||||
|
new Date(session!.creationTimeStamp).getTime()) /
|
||||||
|
1000
|
||||||
|
console.log(
|
||||||
|
`🤖[secondsSinceSessionCreation]🤖`,
|
||||||
|
secondsSinceSessionCreation
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
!session!.attributes ||
|
||||||
|
secondsSinceSessionCreation >=
|
||||||
|
session!.attributes.sessionInactiveTimeout
|
||||||
|
) {
|
||||||
|
console.log(`🤖[54]🤖`, 54)
|
||||||
|
await this.createSessions(accessToken)
|
||||||
|
const freshSession = this.sessions.pop()
|
||||||
|
console.log(`🤖[freshSession]🤖`, freshSession!.id)
|
||||||
|
return freshSession
|
||||||
|
}
|
||||||
|
console.log(`🤖[60]🤖`, 60)
|
||||||
|
console.log(`🤖[---- SessionManager getSession end]🤖`)
|
||||||
|
console.log(`🤖[]🤖`)
|
||||||
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
return session
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async clearSession(id: string, accessToken?: string) {
|
async clearSession(id: string, accessToken?: string) {
|
||||||
|
console.log(
|
||||||
|
`🤖[clearSession this.sessions]🤖`,
|
||||||
|
this.sessions.map((session: any) => session.id)
|
||||||
|
)
|
||||||
|
console.log(`🤖[SessionManager clearSession id]🤖`, id)
|
||||||
|
|
||||||
return await this.requestClient
|
return await this.requestClient
|
||||||
.delete<Session>(`/compute/sessions/${id}`, accessToken)
|
.delete<Session>(`/compute/sessions/${id}`, accessToken)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
@@ -66,6 +116,8 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async createSessions(accessToken?: string) {
|
private async createSessions(accessToken?: string) {
|
||||||
|
console.log(`🤖[SessionManager createSessions]🤖`)
|
||||||
|
|
||||||
if (!this.sessions.length) {
|
if (!this.sessions.length) {
|
||||||
if (!this.currentContext) {
|
if (!this.currentContext) {
|
||||||
await this.setCurrentContext(accessToken).catch((err) => {
|
await this.setCurrentContext(accessToken).catch((err) => {
|
||||||
@@ -73,6 +125,11 @@ export class SessionManager {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`🤖[createSessions start this.sessions]🤖`,
|
||||||
|
this.sessions.map((session: any) => session.id)
|
||||||
|
)
|
||||||
|
|
||||||
await asyncForEach(new Array(MAX_SESSION_COUNT), async () => {
|
await asyncForEach(new Array(MAX_SESSION_COUNT), async () => {
|
||||||
const createdSession = await this.createAndWaitForSession(
|
const createdSession = await this.createAndWaitForSession(
|
||||||
accessToken
|
accessToken
|
||||||
@@ -80,14 +137,23 @@ export class SessionManager {
|
|||||||
throw err
|
throw err
|
||||||
})
|
})
|
||||||
|
|
||||||
this.sessions.push(createdSession)
|
// console.log(`🤖[createSessions new session id]🤖`, createdSession.id)
|
||||||
|
|
||||||
|
// this.sessions.push(createdSession)
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
throw err
|
throw err
|
||||||
})
|
})
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`🤖[createSessions end this.sessions]🤖`,
|
||||||
|
this.sessions.map((session: any) => session.id)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async createAndWaitForSession(accessToken?: string) {
|
private async createAndWaitForSession(accessToken?: string) {
|
||||||
|
console.log(`🤖[SessionManager createAndWaitForSession]🤖`)
|
||||||
|
|
||||||
const { result: createdSession, etag } = await this.requestClient
|
const { result: createdSession, etag } = await this.requestClient
|
||||||
.post<Session>(
|
.post<Session>(
|
||||||
`${this.serverUrl}/compute/contexts/${
|
`${this.serverUrl}/compute/contexts/${
|
||||||
@@ -102,12 +168,23 @@ export class SessionManager {
|
|||||||
|
|
||||||
await this.waitForSession(createdSession, etag, accessToken)
|
await this.waitForSession(createdSession, etag, accessToken)
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`🤖[createAndWaitForSession this.sessions.map((session: any) => session.id)]🤖`,
|
||||||
|
this.sessions.map((session: any) => session.id)
|
||||||
|
)
|
||||||
|
console.log(
|
||||||
|
`🤖[createAndWaitForSession adding createdSession.id]🤖`,
|
||||||
|
createdSession.id
|
||||||
|
)
|
||||||
|
|
||||||
this.sessions.push(createdSession)
|
this.sessions.push(createdSession)
|
||||||
|
|
||||||
return createdSession
|
return createdSession
|
||||||
}
|
}
|
||||||
|
|
||||||
private async setCurrentContext(accessToken?: string) {
|
private async setCurrentContext(accessToken?: string) {
|
||||||
|
console.log(`🤖[SessionManager setCurrentContext]🤖`)
|
||||||
|
|
||||||
if (!this.currentContext) {
|
if (!this.currentContext) {
|
||||||
const { result: contexts } = await this.requestClient
|
const { result: contexts } = await this.requestClient
|
||||||
.get<{
|
.get<{
|
||||||
@@ -138,6 +215,7 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DEPRECATE
|
||||||
private getHeaders(accessToken?: string) {
|
private getHeaders(accessToken?: string) {
|
||||||
const headers: any = {
|
const headers: any = {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@@ -155,6 +233,8 @@ export class SessionManager {
|
|||||||
etag: string | null,
|
etag: string | null,
|
||||||
accessToken?: string
|
accessToken?: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
|
console.log(`🤖[SessionManager waitForSession]🤖`)
|
||||||
|
|
||||||
const logger = process.logger || console
|
const logger = process.logger || console
|
||||||
|
|
||||||
let sessionState = session.state
|
let sessionState = session.state
|
||||||
@@ -230,6 +310,8 @@ export class SessionManager {
|
|||||||
etag: string,
|
etag: string,
|
||||||
accessToken?: string
|
accessToken?: string
|
||||||
) {
|
) {
|
||||||
|
console.log(`🤖[SessionManager getSessionState]🤖`)
|
||||||
|
|
||||||
return await this.requestClient
|
return await this.requestClient
|
||||||
.get(url, accessToken, 'text/plain', { 'If-None-Match': etag })
|
.get(url, accessToken, 'text/plain', { 'If-None-Match': etag })
|
||||||
.then((res) => ({
|
.then((res) => ({
|
||||||
@@ -242,6 +324,8 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getVariable(sessionId: string, variable: string, accessToken?: string) {
|
async getVariable(sessionId: string, variable: string, accessToken?: string) {
|
||||||
|
console.log(`🤖[SessionManager getVariable]🤖`)
|
||||||
|
|
||||||
return await this.requestClient
|
return await this.requestClient
|
||||||
.get<SessionVariable>(
|
.get<SessionVariable>(
|
||||||
`${this.serverUrl}/compute/sessions/${sessionId}/variables/${variable}`,
|
`${this.serverUrl}/compute/sessions/${sessionId}/variables/${variable}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user