-
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 all 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) => | ||
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. |
||
`${config.apiKey}-${indexName}`; | ||
|
||
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; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,9 +70,9 @@ describe('Error handling', () => { | |
await p.index('foo-index').query({ topK: 10, id: '1' }); | ||
} catch (e) { | ||
const err = e as PineconeConnectionError; | ||
expect(err.name).toEqual('PineconeAuthorizationError'); | ||
expect(err.name).toEqual('PineconeConnectionError'); | ||
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. We get the correct error here now, which is great. |
||
expect(err.message).toEqual( | ||
'The API key you provided was rejected while calling https://api.pinecone.io/indexes/foo-index. Please check your configuration values and try again. You can find the configuration values for your project in the Pinecone developer console at https://app.pinecone.io' | ||
'Request failed to reach Pinecone. This can occur for reasons such as network problems that prevent the request from being completed, or a Pinecone API outage. Check your network connection, and visit https://status.pinecone.io/ to see whether any outages are ongoing.' | ||
); | ||
} | ||
}); | ||
|
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.