Skip to content
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

wip: cache site version info to avoid redundant requests #6048

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions lib/shared/src/sourcegraph-api/graphql/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,11 @@ interface HighlightLineRange {
startLine: number
}

interface VersionInfo {
version: string
isInsiderBuild: boolean
}

type GraphQLAPIClientConfig = PickResolvedConfiguration<{
auth: true
configuration: 'telemetryLevel' | 'customHeaders'
Expand All @@ -621,19 +626,44 @@ export class SourcegraphGraphQLAPIClient {
return new SourcegraphGraphQLAPIClient(Observable.of(config))
}

private constructor(private readonly config: Observable<GraphQLAPIClientConfig>) {}
private versionInfo: Promise<VersionInfo | Error> | null = null
private lastServerEndpoint?: string

private constructor(private readonly config: Observable<GraphQLAPIClientConfig>) {
// Subscribe to config changes to clear cache when endpoint changes
this.config.subscribe(newConfig => {
if (this.lastServerEndpoint !== newConfig.auth.serverEndpoint) {
this.versionInfo = null // Clear version cache
this.lastServerEndpoint = newConfig.auth.serverEndpoint
}
})
}

public async getSiteVersion(signal?: AbortSignal): Promise<string | Error> {
return this.fetchSourcegraphAPI<APIResponse<SiteVersionResponse>>(
CURRENT_SITE_VERSION_QUERY,
{},
signal
).then(response =>
extractDataOrError(
response,
data => data.site?.productVersion ?? new Error('site version not found')
const versionInfo = await this.getVersionInfo(signal)
return isError(versionInfo) ? versionInfo : versionInfo.version
}

private async getVersionInfo(signal?: AbortSignal): Promise<VersionInfo | Error> {
if (!this.versionInfo) {
this.versionInfo = this.fetchSourcegraphAPI<APIResponse<SiteVersionResponse>>(
CURRENT_SITE_VERSION_QUERY,
{},
signal
).then(response =>
extractDataOrError(response, data => {
const version = data.site?.productVersion ?? new Error('site version not found')
if (isError(version)) {
return version
}
return {
version,
isInsiderBuild: version.length > 12 || version.includes('dev'),
}
})
)
)
}
return this.versionInfo
}

public async getRemoteFiles(
Expand Down Expand Up @@ -1014,15 +1044,13 @@ export class SourcegraphGraphQLAPIClient {
{ minimumVersion, insider = true }: { minimumVersion: string; insider?: boolean },
signal?: AbortSignal
): Promise<boolean> {
const version = await this.getSiteVersion(signal)
if (isError(version)) {
const versionInfo = await this.getVersionInfo(signal)
if (isError(versionInfo)) {
return false
}
signal?.throwIfAborted()

const isInsiderBuild = version.length > 12 || version.includes('dev')

return (insider && isInsiderBuild) || semver.gte(version, minimumVersion)
return (insider && versionInfo.isInsiderBuild) || semver.gte(versionInfo.version, minimumVersion)
}

public async contextSearch({
Expand Down
Loading