|
1 | 1 | import { |
2 | 2 | KeyValuePair, |
3 | | - DataType, |
4 | | - ERROR_REASONS, |
5 | 3 | FieldSchema, |
6 | 4 | DataTypeStringEnum, |
7 | 5 | DEFAULT_MIN_INT64, |
8 | 6 | SparseFloatVector, |
9 | 7 | FieldData, |
10 | | - FieldType, |
| 8 | + METADATA, |
11 | 9 | } from '../'; |
12 | 10 | import { Pool } from 'generic-pool'; |
| 11 | +import { Metadata } from '@grpc/grpc-js'; |
13 | 12 |
|
14 | 13 | /** |
15 | | - * Promisify a function call with optional timeout |
16 | | - * @param obj - The object containing the target function |
| 14 | + * Promisify a function call with optional timeout and metadata |
| 15 | + * @param pool - The pool of gRPC clients |
17 | 16 | * @param target - The name of the target function to call |
18 | | - * @param params - The parameters to pass to the target function |
| 17 | + * @param params - The parameters to pass to the target function (may contain client_request_id or client-request-id) |
19 | 18 | * @param timeout - Optional timeout in milliseconds |
| 19 | + * @param requestMetadata - Optional metadata to include in the request (e.g., client-request-id). If not provided, will be extracted from params automatically. |
20 | 20 | * @returns A Promise that resolves with the result of the target function call |
21 | 21 | */ |
22 | 22 | export async function promisify( |
23 | 23 | pool: Pool<any>, |
24 | 24 | target: string, |
25 | 25 | params: any, |
26 | | - timeout: number |
| 26 | + timeout: number, |
| 27 | + requestMetadata?: { 'client-request-id'?: string; client_request_id?: string } |
27 | 28 | ): Promise<any> { |
28 | 29 | // Calculate the deadline for the function call |
29 | 30 | const t = timeout === 0 ? 1000 * 60 * 60 * 24 : timeout; |
30 | 31 |
|
31 | 32 | // get client |
32 | 33 | const client = await pool.acquire(); |
33 | 34 |
|
| 35 | + // Extract traceid from params if requestMetadata is not explicitly provided |
| 36 | + let finalRequestMetadata = requestMetadata; |
| 37 | + if (!finalRequestMetadata && params) { |
| 38 | + finalRequestMetadata = extractRequestMetadata(params); |
| 39 | + } |
| 40 | + |
| 41 | + // Create metadata object if traceid is found |
| 42 | + const metadata = finalRequestMetadata ? new Metadata() : undefined; |
| 43 | + |
| 44 | + if (metadata && finalRequestMetadata) { |
| 45 | + // Support both client_request_id and client-request-id (for compatibility) |
| 46 | + // Priority: client_request_id > client-request-id (JavaScript/TypeScript convention) |
| 47 | + const clientRequestId = getClientRequestId(finalRequestMetadata); |
| 48 | + if (clientRequestId) { |
| 49 | + // Convert to string to prevent runtime errors if non-string value is passed |
| 50 | + metadata.add(METADATA.CLIENT_REQUEST_ID, String(clientRequestId)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
34 | 54 | // Create a new Promise that wraps the target function call |
35 | 55 | return new Promise((resolve, reject) => { |
36 | 56 | try { |
37 | | - // Call the target function with the provided parameters and deadline |
38 | | - client[target]( |
39 | | - params, |
40 | | - { deadline: new Date(Date.now() + t) }, |
41 | | - (err: any, result: any) => { |
42 | | - if (err) { |
43 | | - // If there was an error, reject the Promise with the error |
44 | | - reject(err); |
45 | | - } else { |
46 | | - // Otherwise, resolve the Promise with the result |
47 | | - resolve(result); |
48 | | - } |
49 | | - if (client) { |
50 | | - pool.release(client); |
51 | | - } |
| 57 | + // Call the target function with the provided parameters, deadline, and metadata |
| 58 | + const callOptions: any = { deadline: new Date(Date.now() + t) }; |
| 59 | + if (metadata) { |
| 60 | + callOptions.metadata = metadata; |
| 61 | + } |
| 62 | + |
| 63 | + client[target](params, callOptions, (err: any, result: any) => { |
| 64 | + if (err) { |
| 65 | + // If there was an error, reject the Promise with the error |
| 66 | + reject(err); |
| 67 | + } else { |
| 68 | + // Otherwise, resolve the Promise with the result |
| 69 | + resolve(result); |
52 | 70 | } |
53 | | - ); |
| 71 | + if (client) { |
| 72 | + pool.release(client); |
| 73 | + } |
| 74 | + }); |
54 | 75 | } catch (e: any) { |
55 | 76 | reject(e); |
56 | 77 | if (client) { |
@@ -139,3 +160,40 @@ export const getValidDataArray = (data: FieldData[], length: number) => { |
139 | 160 | return data[i] !== undefined && data[i] !== null; |
140 | 161 | }); |
141 | 162 | }; |
| 163 | + |
| 164 | +/** |
| 165 | + * Extracts client request ID from metadata object with priority handling. |
| 166 | + * Priority: client_request_id > client-request-id (JavaScript/TypeScript convention) |
| 167 | + * @param metadata - Metadata object that may contain traceid |
| 168 | + * @returns Client request ID as string or undefined if not found |
| 169 | + */ |
| 170 | +const getClientRequestId = (metadata?: { |
| 171 | + 'client-request-id'?: string; |
| 172 | + client_request_id?: string; |
| 173 | +}): string | undefined => { |
| 174 | + if (!metadata) { |
| 175 | + return undefined; |
| 176 | + } |
| 177 | + // Priority: client_request_id > client-request-id (JavaScript/TypeScript convention) |
| 178 | + return metadata.client_request_id || metadata['client-request-id']; |
| 179 | +}; |
| 180 | + |
| 181 | +/** |
| 182 | + * Extracts request metadata (traceid) from request data. |
| 183 | + * Supports both client_request_id and client-request-id formats. |
| 184 | + * Priority: client_request_id > client-request-id (JavaScript/TypeScript convention) |
| 185 | + * @param data - Request data that may contain traceid |
| 186 | + * @returns Request metadata object or undefined if no traceid provided |
| 187 | + */ |
| 188 | +export const extractRequestMetadata = ( |
| 189 | + data: any |
| 190 | +): |
| 191 | + | { |
| 192 | + 'client-request-id': string; |
| 193 | + } |
| 194 | + | undefined => { |
| 195 | + const clientRequestId = getClientRequestId(data); |
| 196 | + return clientRequestId |
| 197 | + ? { 'client-request-id': String(clientRequestId) } |
| 198 | + : undefined; |
| 199 | +}; |
0 commit comments