-
Notifications
You must be signed in to change notification settings - Fork 38
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
Fix IndexHostSingleton
holding onto one instance of ManageIndexesAPI
#224
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { ManageIndexesApi } from '../pinecone-generated-ts-fetch'; | ||
import type { PineconeConfiguration } from './types'; | ||
import type { IndexName } from '../control'; | ||
import { describeIndex, indexOperationsBuilder } from '../control'; | ||
|
@@ -10,16 +9,12 @@ import { normalizeUrl } from '../utils'; | |
// and index, so we cache them in a singleton for reuse. | ||
export const IndexHostSingleton = (function () { | ||
const hostUrls = {}; // map of apiKey-indexName to hostUrl | ||
let indexOperationsApi: InstanceType<typeof ManageIndexesApi> | null = null; | ||
|
||
const _describeIndex = async ( | ||
config: PineconeConfiguration, | ||
indexName: IndexName | ||
): Promise<string> => { | ||
if (!indexOperationsApi) { | ||
indexOperationsApi = indexOperationsBuilder(config); | ||
} | ||
|
||
const indexOperationsApi = indexOperationsBuilder(config); | ||
const describeResponse = await describeIndex(indexOperationsApi)(indexName); | ||
const host = describeResponse.host; | ||
|
||
|
@@ -34,19 +29,17 @@ export const IndexHostSingleton = (function () { | |
} | ||
}; | ||
|
||
const key = (config, indexName) => `${config.apiKey}-${indexName}`; | ||
const _key = (config: PineconeConfiguration, indexName: string) => | ||
`${config.apiKey}-${indexName}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor tweak, underscore just to make it more clear it's internal to the singleton. |
||
|
||
return { | ||
getHostUrl: async function ( | ||
config: PineconeConfiguration, | ||
indexName: IndexName | ||
) { | ||
const cacheKey = key(config, indexName); | ||
const singleton = { | ||
getHostUrl: async (config: PineconeConfiguration, indexName: IndexName) => { | ||
const cacheKey = _key(config, indexName); | ||
if (cacheKey in hostUrls) { | ||
return hostUrls[cacheKey]; | ||
} else { | ||
const hostUrl = await _describeIndex(config, indexName); | ||
this._set(config, indexName, hostUrl); | ||
singleton._set(config, indexName, hostUrl); | ||
|
||
Comment on lines
+35
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is primarily a stylistic change, I wanted the context of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I see now after talking this over with you. By the time the function that refers to |
||
if (!hostUrls[cacheKey]) { | ||
throw new PineconeUnableToResolveHostError( | ||
|
@@ -74,13 +67,15 @@ export const IndexHostSingleton = (function () { | |
return; | ||
} | ||
|
||
const cacheKey = key(config, indexName); | ||
const cacheKey = _key(config, indexName); | ||
hostUrls[cacheKey] = normalizedHostUrl; | ||
}, | ||
|
||
_delete: (config: PineconeConfiguration, indexName: IndexName) => { | ||
const cacheKey = key(config, indexName); | ||
const cacheKey = _key(config, indexName); | ||
delete hostUrls[cacheKey]; | ||
}, | ||
}; | ||
|
||
return singleton; | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't know what I was doing here, seems really silly to me now lol.