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

CB-4436 update session only in touchSession #2377

Merged
merged 50 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2ad767b
CB-4436 update session only in touchSession
alexander-skoblikov Feb 12, 2024
ba7ef88
Merge branch 'devel' into CB-4346-session-doesnt-expire-after-30-min
Feb 13, 2024
a005e24
CB-4346 adds session touch for user actions
Feb 13, 2024
18d7576
CB-4346 adds core-activity package
Feb 13, 2024
69a8576
CB-4346 fix: mobx version core-activity package
Feb 13, 2024
203f756
CB-4346 ClientActivityService cleanup
Feb 13, 2024
1b83462
CB-4346 ClientActivityService cleanup [2]
Feb 13, 2024
7128689
CB-4346 fix: client activity function contexts bind
Feb 13, 2024
0ebffcb
CB-4346 useClientActivity cleanup
Feb 13, 2024
38a74e7
CB-4346 removed unneeded deps
Feb 13, 2024
0b4ed1e
CB-4346 session warning dialog props renaming
Feb 13, 2024
74d3fd1
CB-4364 adds client activity service setsActivity method
Feb 13, 2024
855fa61
Merge branch 'devel' into CB-4346-session-doesnt-expire-after-30-min
Feb 13, 2024
4c87772
CB-4346 activity timers refactor
Feb 13, 2024
e830159
Merge branch 'devel' into CB-4346-session-doesnt-expire-after-30-min
Feb 14, 2024
1eef3f9
CB-4346 adds min time for session expire setting
Feb 14, 2024
1d50f04
CB-4346 adds update client activity each 300ms
Feb 14, 2024
e27de12
CB-4346 touchSession logic moved to SessionExpiredService
Feb 14, 2024
0450ac5
СB-4346 session expire service uses session resource to touchSession
Feb 14, 2024
721a843
CB-4346 code cleanup
Feb 14, 2024
9137803
CB-4346 removed unused activity package from plugin session expiration
Feb 14, 2024
adba2d5
CB-4346 SessionExpireService - removed cycle dependency
Feb 14, 2024
06a98e7
CB-4346 code cleanup
Feb 14, 2024
cc8aa30
CB-4346 update session state if it is invalid
Feb 14, 2024
e58962d
CB-4346 removed arrow function from useClientActivity
Feb 14, 2024
9ad7025
Revert "CB-4346 SessionExpireService - removed cycle dependency"
Feb 14, 2024
dacd1de
CB-4346 touchSession logic moved from SessionExpiredService to Sessio…
Feb 14, 2024
d38bc06
CB-4346 chore: core-activity -> core-client-activity renaming
Feb 19, 2024
527cb27
CB-4346 chore: vars renaming
Feb 19, 2024
535ff87
CB-4346 refactor: removed force from sessionResource.touchSession()
Feb 19, 2024
51b3887
CB-4346 adds SessionTouchService
Feb 19, 2024
afa80c6
CB-4346 chore: SESSION_EXPIRE_MIN_TIME const new name
Feb 19, 2024
ad5367f
CB-4346 sessionTouchService license
Feb 19, 2024
7061cdb
CB-4346 uses SessionTouchService as Dependency
Feb 19, 2024
6dca65a
CB-4346 pr fixes
Feb 19, 2024
79c3700
CB-4346 adds core-localization to plugin-session-expiration
Feb 19, 2024
f124f5d
CB-4346 adds core coreClientActivityManifest to unit tests
Feb 19, 2024
01a403e
CB-4346 updates ts references
Feb 20, 2024
edc7593
CB-4436 new update session gql api
alexander-skoblikov Feb 20, 2024
b98ac74
CB-4346 adds updateSession API call
Feb 20, 2024
a823244
CB-4346 code cleanup sessionResource
Feb 20, 2024
93a1926
CB-4346 do not send state event for not authorized session
alexander-skoblikov Feb 22, 2024
ce827fe
CB-4346 chore: update session info with event
Wroud Feb 22, 2024
4c990f3
CB-4346 adds update activity on touch (tablets, phones support)
Feb 22, 2024
9f1b2aa
CB-4346 add debug log
alexander-skoblikov Feb 22, 2024
f9919a1
grouping
Feb 22, 2024
2b8fac5
Merge branch 'devel' into CB-4346-session-doesnt-expire-after-30-min
Feb 22, 2024
19e498f
Merge branch 'devel' into CB-4346-session-doesnt-expire-after-30-min
EvgeniaBzzz Feb 23, 2024
4069d98
CB-4346 force update session on create
alexander-skoblikov Feb 23, 2024
81c0879
Merge branch 'devel' into CB-4346-session-doesnt-expire-after-30-min
EvgeniaBzzz Feb 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ public WebSession getWebSession(@NotNull HttpServletRequest request,
}

@NotNull
public WebSession getWebSession(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, boolean errorOnNoFound) throws DBWebException {
return getWebSession(request, response, true, errorOnNoFound);
}

@NotNull
public WebSession getWebSession(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, boolean updateInfo, boolean errorOnNoFound) throws DBWebException {
public WebSession getWebSession(
@NotNull HttpServletRequest request,
@NotNull HttpServletResponse response,
boolean errorOnNoFound
) throws DBWebException {
HttpSession httpSession = request.getSession(true);
String sessionId = httpSession.getId();
WebSession webSession;
Expand Down Expand Up @@ -139,13 +138,6 @@ public WebSession getWebSession(@NotNull HttpServletRequest request, @NotNull Ht
throw new DBWebException("Unexpected session type: " + baseWebSession.getClass().getName());
}
webSession = (WebSession) baseWebSession;
if (updateInfo) {
// Update only once per request
if (!CommonUtils.toBoolean(request.getAttribute("sessionUpdated"))) {
webSession.updateInfo(request, response);
request.setAttribute("sessionUpdated", true);
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@cloudbeaver/core-blocks": "~0.1.0",
"@cloudbeaver/core-client-activity": "~0.1.0",
"@cloudbeaver/core-di": "~0.1.0",
"@cloudbeaver/core-executor": "~0.1.0",
"@cloudbeaver/core-localization": "~0.1.0",
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/core-app/src/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useAppVersion } from '@cloudbeaver/core-version';

import style from './Body.m.css';
import { useAppHeight } from './useAppHeight';
import { useClientActivity } from './useClientActivity';

export const Body = observer(function Body() {
// const serverConfigLoader = useResource(Body, ServerConfigResource, undefined);
Expand All @@ -44,6 +45,7 @@ export const Body = observer(function Body() {
});

useAppHeight();
useClientActivity();

return (
<DNDProvider>
Expand Down
44 changes: 44 additions & 0 deletions webapp/packages/core-app/src/useClientActivity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { useEffect } from 'react';

import { ClientActivityService } from '@cloudbeaver/core-client-activity';
import { useService } from '@cloudbeaver/core-di';
import { throttle } from '@cloudbeaver/core-utils';

const UPDATE_THROTTLE = 300;

export function useClientActivity() {
const clientActivityService = useService(ClientActivityService);

const updateActivity = throttle(function updateActivity() {
clientActivityService.updateActivity();
}, UPDATE_THROTTLE);

function subscribeEvents() {
document.addEventListener('mousemove', updateActivity);
document.addEventListener('click', updateActivity);
document.addEventListener('keydown', updateActivity);
document.addEventListener('scroll', updateActivity);
}

function unsubscribeEvents() {
document.removeEventListener('mousemove', updateActivity);
document.removeEventListener('click', updateActivity);
document.removeEventListener('keydown', updateActivity);
document.removeEventListener('scroll', updateActivity);
}

useEffect(() => {
subscribeEvents();

return () => {
unsubscribeEvents();
};
}, []);

Check warning on line 43 in webapp/packages/core-app/src/useClientActivity.ts

View check run for this annotation

Jenkins-CI-integration / CheckStyle TypeScript Report

webapp/packages/core-app/src/useClientActivity.ts#L43

React Hook useEffect has missing dependencies: subscribeEvents and unsubscribeEvents. Either include them or remove the dependency array. (react-hooks/exhaustive-deps)
}
3 changes: 3 additions & 0 deletions webapp/packages/core-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
{
"path": "../core-blocks/tsconfig.json"
},
{
"path": "../core-client-activity/tsconfig.json"
},
{
"path": "../core-di/tsconfig.json"
},
Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@cloudbeaver/core-browser": "~0.1.0",
"@cloudbeaver/core-browser-cookies": "~0.1.0",
"@cloudbeaver/core-browser-settings": "~0.1.0",
"@cloudbeaver/core-client-activity": "~0.1.0",
"@cloudbeaver/core-connections": "~0.1.0",
"@cloudbeaver/core-di": "~0.1.0",
"@cloudbeaver/core-dialogs": "~0.1.0",
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/core-bootstrap/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { coreBlocksManifest } from '@cloudbeaver/core-blocks';
import { coreBrowserManifest } from '@cloudbeaver/core-browser';
import { coreBrowserCookiesManifest } from '@cloudbeaver/core-browser-cookies';
import { coreBrowserSettingsManifest } from '@cloudbeaver/core-browser-settings';
import { coreClientActivityManifest } from '@cloudbeaver/core-client-activity';
import { coreConnectionsManifest } from '@cloudbeaver/core-connections';
import { coreDIManifest, PluginManifest } from '@cloudbeaver/core-di';
import { coreDialogsManifest } from '@cloudbeaver/core-dialogs';
Expand Down Expand Up @@ -74,5 +75,6 @@ export const coreManifests: PluginManifest[] = [
coreDialogsManifest,
resourceManagerManifest,
coreAppManifest,
coreClientActivityManifest,
coreNavigationTree,
];
3 changes: 3 additions & 0 deletions webapp/packages/core-bootstrap/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
{
"path": "../core-browser/tsconfig.json"
},
{
"path": "../core-client-activity/tsconfig.json"
},
{
"path": "../core-connections/tsconfig.json"
},
Expand Down
21 changes: 21 additions & 0 deletions webapp/packages/core-client-activity/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/lib

# misc
.DS_Store
.env*

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
28 changes: 28 additions & 0 deletions webapp/packages/core-client-activity/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@cloudbeaver/core-client-activity",
"sideEffects": [
"src/**/*.css",
"src/**/*.scss",
"public/**/*"
],
"version": "0.1.0",
"description": "",
"license": "Apache-2.0",
"main": "dist/index.js",
"scripts": {
"build": "tsc -b",
"lint": "eslint ./src/ --ext .ts,.tsx",
"lint-fix": "eslint ./src/ --ext .ts,.tsx --fix",
"validate-dependencies": "core-cli-validate-dependencies",
"update-ts-references": "rimraf --glob dist && typescript-resolve-references"
},
"dependencies": {
"@cloudbeaver/core-di": "~0.1.0",
"@cloudbeaver/core-executor": "~0.1.0",
"mobx": "^6.12.0"
},
"peerDependencies": {},
"devDependencies": {
"typescript": "^5.3.2"
}
}
58 changes: 58 additions & 0 deletions webapp/packages/core-client-activity/src/ClientActivityService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { makeObservable, observable } from 'mobx';

import { injectable } from '@cloudbeaver/core-di';
import { Executor, IExecutor } from '@cloudbeaver/core-executor';

const INACTIVE_PERIOD_TIME = 1000 * 60;

@injectable()
export class ClientActivityService {
private timer: ReturnType<typeof setTimeout> | null;
public isActive: boolean;
public onActiveStateChange: IExecutor<boolean>;

constructor() {
this.setActivity = this.setActivity.bind(this);
this.updateActivity = this.updateActivity.bind(this);
this.resetActivity = this.resetActivity.bind(this);

this.timer = null;
this.isActive = false;
this.onActiveStateChange = new Executor();

makeObservable(this, {
isActive: observable,
});
}

private setActivity(value: boolean) {
this.isActive = value;
this.onActiveStateChange.execute(value);
}

resetActivity() {
this.setActivity(false);

if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}

updateActivity() {
this.setActivity(true);

if (this.timer) {
clearTimeout(this.timer);
}

this.timer = setTimeout(this.resetActivity, INACTIVE_PERIOD_TIME);
}
}
2 changes: 2 additions & 0 deletions webapp/packages/core-client-activity/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ClientActivityService';
export * from './manifest';
18 changes: 18 additions & 0 deletions webapp/packages/core-client-activity/src/manifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import type { PluginManifest } from '@cloudbeaver/core-di';

import { ClientActivityService } from './ClientActivityService';

export const coreClientActivityManifest: PluginManifest = {
info: {
name: 'Core Client Activity',
},

providers: [ClientActivityService],
};
28 changes: 28 additions & 0 deletions webapp/packages/core-client-activity/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/tsconfig.tsbuildinfo"
},
"references": [
{
"path": "../core-di/tsconfig.json"
},
{
"path": "../core-executor/tsconfig.json"
}
],
"include": [
"__custom_mocks__/**/*",
"src/**/*",
"src/**/*.json",
"src/**/*.css",
"src/**/*.scss"
],
"exclude": [
"**/node_modules",
"lib/**/*",
"dist/**/*"
]
}
1 change: 1 addition & 0 deletions webapp/packages/core-root/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"update-ts-references": "rimraf --glob dist && typescript-resolve-references"
},
"dependencies": {
"@cloudbeaver/core-client-activity": "~0.1.0",
"@cloudbeaver/core-di": "~0.1.0",
"@cloudbeaver/core-executor": "~0.1.0",
"@cloudbeaver/core-resource": "~0.1.0",
Expand Down
14 changes: 12 additions & 2 deletions webapp/packages/core-root/src/SessionExpireService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@ import { Bootstrap, injectable } from '@cloudbeaver/core-di';
import { Executor, IExecutor } from '@cloudbeaver/core-executor';
import { EServerErrorCode, GQLError, GraphQLService, SessionError } from '@cloudbeaver/core-sdk';
import { errorOf } from '@cloudbeaver/core-utils';

export const SESSION_EXPIRE_MIN_TIME = 5 * 1000 * 60;

@injectable()
export class SessionExpireService extends Bootstrap {
expired = false;
private isExpired = false;

onSessionExpire: IExecutor;
constructor(private readonly graphQLService: GraphQLService) {
super();

this.sessionExpired = this.sessionExpired.bind(this);

this.onSessionExpire = new Executor();
}

get expired() {
return this.isExpired;
}

register(): void {
this.graphQLService.registerInterceptor(this.sessionExpiredInterceptor.bind(this));
}
Expand All @@ -32,7 +42,7 @@ export class SessionExpireService extends Bootstrap {

const e = new SessionError('Session expired');
this.graphQLService.blockRequests(e);
this.expired = true;
this.isExpired = true;
this.onSessionExpire.execute();
}

Expand Down
29 changes: 22 additions & 7 deletions webapp/packages/core-root/src/SessionResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export class SessionResource extends CachedDataResource<SessionState | null> {
this,
);

this.touchSession = this.touchSession.bind(this);
this.processAction = this.processAction.bind(this);
this.setDefaultLocale = this.setDefaultLocale.bind(this);
this.changeLanguage = this.changeLanguage.bind(this);

Wroud marked this conversation as resolved.
Show resolved Hide resolved
this.action = null;
this.sync(
serverConfigResource,
Expand All @@ -68,13 +73,6 @@ export class SessionResource extends CachedDataResource<SessionState | null> {
this.defaultLocale = defaultLocale;
}

//! this method results in onDataUpdate handler skipping
async refreshSilent(): Promise<void> {
const session = await this.loader();

this.setData(session);
}

async changeLanguage(locale: string): Promise<void> {
if (this.data?.locale === locale) {
return;
Expand All @@ -95,6 +93,23 @@ export class SessionResource extends CachedDataResource<SessionState | null> {
return session;
}

async touchSession() {
if (!this.data?.valid) {
return;
}

const valid = await this.graphQLService.sdk.touchSession();

if (!valid) {
this.setData({
...this.data,
valid,
});
}
Wroud marked this conversation as resolved.
Show resolved Hide resolved

return valid;
}

protected setData(data: SessionState | null) {
if (!this.action) {
this.action = data?.actionParameters;
Expand Down
Loading
Loading