Browse Source

connect with clientside id (#681)

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
ryjiang 8 months ago
parent
commit
abb0d09966

+ 9 - 3
client/src/context/Auth.tsx

@@ -18,6 +18,7 @@ export const authContext = createContext<AuthContextType>({
     token: '',
     database: '',
     checkHealth: true,
+    clientId: '',
   },
   setAuthReq: () => {},
   isManaged: false,
@@ -31,6 +32,7 @@ export const authContext = createContext<AuthContextType>({
 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<AuthReq>(localAuthReq);
-  const [clientId, setClientId] = useState<string>(
-    window.localStorage.getItem(MILVUS_CLIENT_ID) || ''
-  );
+  const [clientId, setClientId] = useState<string>(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

+ 4 - 9
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) || '[]'

+ 10 - 2
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);

+ 13 - 1
server/src/milvus/milvus.service.ts

@@ -26,7 +26,15 @@ export class MilvusService {
 
   async connectMilvus(data: AuthReq): Promise<AuthObject> {
     // 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) {

+ 1 - 0
server/src/types/index.ts

@@ -17,6 +17,7 @@ export type AuthReq = {
   token: string;
   database: string;
   checkHealth: boolean;
+  clientId: string;
 };
 
 export type AuthObject = {