diff --git a/src/Common/CosmosClient.ts b/src/Common/CosmosClient.ts index 28e0745c9..7691c9040 100644 --- a/src/Common/CosmosClient.ts +++ b/src/Common/CosmosClient.ts @@ -132,6 +132,10 @@ let _client: Cosmos.CosmosClient; export function client(): Cosmos.CosmosClient { if (_client) return _client; + // if (_client) { + // _client.dispose(); + // } + let _defaultHeaders: Cosmos.CosmosHeaders = {}; _defaultHeaders["x-ms-cosmos-sdk-supportedcapabilities"] = SDKSupportedCapabilities.None | SDKSupportedCapabilities.PartitionMerge; @@ -192,7 +196,6 @@ export function client(): Cosmos.CosmosClient { console.log(`Current parsed write endpoint: ${JSON.stringify(parsedWriteEndpoint)}`); // const writeHostAddress = await findHostAddress(parsedWriteEndpoint); // console.log(`Current write host address: ${JSON.stringify(writeHostAddress)}`); - } catch (error) { console.error("Error getting read endpoints:", error); } @@ -208,7 +211,10 @@ export function client(): Cosmos.CosmosClient { tokenProvider, connectionPolicy: { enableEndpointDiscovery: true, - // preferredLocations: ["East US", "West US", "East US 2"], + preferredLocations: ["East US", "Central US"], + connectionMode: Cosmos.ConnectionMode.Gateway, + enableBackgroundEndpointRefreshing: true, + endpointRefreshRateInMs: 5000, }, userAgentSuffix: "Azure Portal", defaultHeaders: _defaultHeaders, diff --git a/src/Common/dataAccess/readCollection.ts b/src/Common/dataAccess/readCollection.ts index 3943b762d..eff2bb5dc 100644 --- a/src/Common/dataAccess/readCollection.ts +++ b/src/Common/dataAccess/readCollection.ts @@ -35,6 +35,8 @@ export async function readCollectionInternal( try { const response = await cosmosClient.database(databaseId).container(collectionId).read(); collection = response.resource as DataModels.Collection; + const currentReadRegions = await cosmosClient.getReadEndpoint(); + console.log(`Current account endpoints - readCollection: ${JSON.stringify(currentReadRegions)}`); } catch (error) { handleError(error, "ReadCollection", `Error while querying container ${collectionId}`); throw error; diff --git a/src/Common/dataAccess/readDatabases.ts b/src/Common/dataAccess/readDatabases.ts index 9cc0b0641..8db8f2ce4 100644 --- a/src/Common/dataAccess/readDatabases.ts +++ b/src/Common/dataAccess/readDatabases.ts @@ -47,7 +47,8 @@ export async function readDatabases(): Promise { clearMessage(); return databases; } - + const currentReadRegion = await client().getReadEndpoint(); + console.log(`Current account endpoints - readDatabases: ${JSON.stringify(currentReadRegion)}`); try { if ( userContext.authType === AuthType.AAD && diff --git a/src/Common/dataAccess/readDocument.ts b/src/Common/dataAccess/readDocument.ts index 9b7f224e0..96ea72ad8 100644 --- a/src/Common/dataAccess/readDocument.ts +++ b/src/Common/dataAccess/readDocument.ts @@ -25,6 +25,11 @@ export const readDocument = async (collection: CollectionBase, documentId: Docum .item(documentId.id(), getPartitionKeyValue(documentId)) .read(options); + const currentReadRegion = await client().getReadEndpoint(); + console.log(`Current account endpoints - readDocument: ${JSON.stringify(currentReadRegion)}`); + + console.log(response.diagnostics); + return response?.resource; } catch (error) { handleError(error, "ReadDocument", `Failed to read ${entityName} ${documentId.id()}`);