From abb0d09966d465a6150c287084c9ceba26700d9a Mon Sep 17 00:00:00 2001 From: ryjiang Date: Thu, 7 Nov 2024 16:13:53 +0800 Subject: [PATCH] connect with clientside id (#681) Signed-off-by: ryjiang --- client/src/context/Auth.tsx | 12 +++++++++--- client/src/pages/connect/AuthForm.tsx | 13 ++++--------- server/src/milvus/milvus.controller.ts | 12 ++++++++++-- server/src/milvus/milvus.service.ts | 14 +++++++++++++- server/src/types/index.ts | 1 + 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/client/src/context/Auth.tsx b/client/src/context/Auth.tsx index a83910f5..c8ee4fa3 100644 --- a/client/src/context/Auth.tsx +++ b/client/src/context/Auth.tsx @@ -18,6 +18,7 @@ export const authContext = createContext({ token: '', database: '', checkHealth: true, + clientId: '', }, setAuthReq: () => {}, isManaged: false, @@ -31,6 +32,7 @@ export const authContext = createContext({ const { Provider } = authContext; export const AuthProvider = (props: { children: React.ReactNode }) => { // get data from local storage + const localClientId = window.localStorage.getItem(MILVUS_CLIENT_ID) || ''; const localAuthReq = JSON.parse( window.localStorage.getItem(ATTU_AUTH_REQ) || JSON.stringify({ @@ -40,14 +42,13 @@ export const AuthProvider = (props: { children: React.ReactNode }) => { token: '', database: MILVUS_DATABASE, checkHealth: true, + clientId: localClientId, }) ); // state const [authReq, setAuthReq] = useState(localAuthReq); - const [clientId, setClientId] = useState( - window.localStorage.getItem(MILVUS_CLIENT_ID) || '' - ); + const [clientId, setClientId] = useState(localClientId); // update local storage when authReq changes useEffect(() => { @@ -62,12 +63,17 @@ export const AuthProvider = (props: { children: React.ReactNode }) => { // login API const login = async (params: AuthReq) => { + // create a new client id + params.clientId = Math.random().toString(36).substring(16); // connect to Milvus const res = await MilvusService.connect(params); // update auth request setAuthReq({ ...params, database: res.database }); setClientId(res.clientId); + // save clientId to local storage + window.localStorage.setItem(MILVUS_CLIENT_ID, res.clientId); + return res; }; // logout API diff --git a/client/src/pages/connect/AuthForm.tsx b/client/src/pages/connect/AuthForm.tsx index 44ebb8f2..80e8fd3c 100644 --- a/client/src/pages/connect/AuthForm.tsx +++ b/client/src/pages/connect/AuthForm.tsx @@ -7,12 +7,7 @@ import { useFormValidation } from '@/hooks'; import { formatForm } from '@/utils'; import { useNavigate } from 'react-router-dom'; import { rootContext, authContext, dataContext } from '@/context'; -import { - MILVUS_CLIENT_ID, - ATTU_AUTH_HISTORY, - MILVUS_DATABASE, - MILVUS_URL, -} from '@/consts'; +import { ATTU_AUTH_HISTORY, MILVUS_DATABASE, MILVUS_URL } from '@/consts'; import { CustomRadio } from '@/components/customRadio/CustomRadio'; import Icons from '@/components/icons/Icons'; import CustomToolTip from '@/components/customToolTip/CustomToolTip'; @@ -32,6 +27,7 @@ const DEFAULT_CONNECTION = { password: '', checkHealth: true, time: -1, + clientId: '', }; export const AuthForm = () => { @@ -108,14 +104,13 @@ export const AuthForm = () => { try { // login - const result = await login(authReq); + await login(authReq); // set database setDatabase(authReq.database); // success message openSnackBar(successTrans('connect')); - // save clientId to local storage - window.localStorage.setItem(MILVUS_CLIENT_ID, result.clientId); + // get connection history const history = JSON.parse( window.localStorage.getItem(ATTU_AUTH_HISTORY) || '[]' diff --git a/server/src/milvus/milvus.controller.ts b/server/src/milvus/milvus.controller.ts index afa93b8a..8e2ee9fa 100644 --- a/server/src/milvus/milvus.controller.ts +++ b/server/src/milvus/milvus.controller.ts @@ -41,8 +41,15 @@ export class MilvusController { } async connectMilvus(req: Request, res: Response, next: NextFunction) { - const { address, username, password, database, token, checkHealth } = - req.body; + const { + address, + username, + password, + database, + token, + checkHealth, + clientId, + } = req.body; try { const result = await this.milvusService.connectMilvus({ address, @@ -51,6 +58,7 @@ export class MilvusController { password, database, checkHealth, + clientId, }); res.send(result); diff --git a/server/src/milvus/milvus.service.ts b/server/src/milvus/milvus.service.ts index 63cb1b3d..d062b357 100644 --- a/server/src/milvus/milvus.service.ts +++ b/server/src/milvus/milvus.service.ts @@ -26,7 +26,15 @@ export class MilvusService { async connectMilvus(data: AuthReq): Promise { // Destructure the data object to get the connection details - const { address, token, username, password, database, checkHealth } = data; + const { + address, + token, + username, + password, + database, + checkHealth, + clientId, + } = data; // Format the address to remove the http prefix const milvusAddress = MilvusService.formatAddress(address); @@ -39,6 +47,10 @@ export class MilvusService { password, logLevel: process.env.ATTU_LOG_LEVEL || 'info', database: database || this.DEFAULT_DATABASE, + id: clientId, + pool: { + max: 10, + }, }; if (process.env.ROOT_CERT_PATH) { diff --git a/server/src/types/index.ts b/server/src/types/index.ts index c2a476a0..99978425 100644 --- a/server/src/types/index.ts +++ b/server/src/types/index.ts @@ -17,6 +17,7 @@ export type AuthReq = { token: string; database: string; checkHealth: boolean; + clientId: string; }; export type AuthObject = {