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

Add support for retry-after header #95

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ jobs:

- uses: pnpm/action-setup@v2
with:
version: 9.15.0
run_install: false

- name: Use Node.js ${{ matrix.node-version }}
15 changes: 13 additions & 2 deletions packages/js-client-grpc/src/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {Transport, Interceptor, createPromiseClient, PromiseClient} from '@bufbuild/connect';
import {createGrpcTransport, compressionGzip} from '@bufbuild/connect-node';
import {Code, ConnectError, createPromiseClient, Interceptor, PromiseClient, Transport} from '@bufbuild/connect';
import {compressionGzip, createGrpcTransport} from '@bufbuild/connect-node';
import {Collections} from './proto/collections_service_connect.js';
import {Points} from './proto/points_service_connect.js';
import {Snapshots} from './proto/snapshots_service_connect.js';
import {Qdrant} from './proto/qdrant_connect.js';
import {PACKAGE_VERSION} from './client-version.js';
import {ResourceExhaustedError} from './errors.js';

type Clients = {
collections: PromiseClient<typeof Collections>;
@@ -54,6 +55,16 @@ export function createApis(baseUrl: string, {timeout, apiKey}: {timeout: number;
req.header.set('user-agent', 'qdrant-js/' + String(PACKAGE_VERSION));
return next(req);
},
(next) => (req) =>
next(req)
.then((response) => response)
.catch((error) => {
if (error instanceof ConnectError && error.code === Code.ResourceExhausted) {
const retryAfterHeader = error.metadata.get('retry-after')?.[0] ?? '';
throw new ResourceExhaustedError(retryAfterHeader);
}
throw error;
}),
];
if (apiKey !== undefined) {
interceptors.push((next) => (req) => {
15 changes: 15 additions & 0 deletions packages/js-client-grpc/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Code, ConnectError} from '@bufbuild/connect';

class CustomError extends Error {
constructor(message: string) {
super(message);
@@ -7,3 +9,16 @@ class CustomError extends Error {
}

export class QdrantClientConfigError extends CustomError {}

class CustomConnectError extends ConnectError {
retry_after: string;

constructor(retryAfter: string) {
super('Resource exhausted: Retry after specified duration', Code.ResourceExhausted);
this.name = this.constructor.name;
this.retry_after = retryAfter;
Object.setPrototypeOf(this, new.target.prototype);
}
}

export class ResourceExhaustedError extends CustomConnectError {}
2 changes: 1 addition & 1 deletion packages/js-client-rest/package.json
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@
"dependencies": {
"@qdrant/openapi-typescript-fetch": "1.2.6",
"@sevinf/maybe": "0.5.0",
"undici": "~5.28.4"
"undici": "~5.28.5"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.1.0",
25 changes: 20 additions & 5 deletions packages/js-client-rest/src/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Fetcher, Middleware} from '@qdrant/openapi-typescript-fetch';
import {ApiError, Fetcher, Middleware} from '@qdrant/openapi-typescript-fetch';
import {paths} from './openapi/generated_schema.js';
import {createDispatcher} from './dispatcher.js';
import {createClusterApi} from './api/cluster-api.js';
@@ -7,7 +7,11 @@ import {createPointsApi} from './api/points-api.js';
import {createServiceApi} from './api/service-api.js';
import {createSnapshotsApi} from './api/snapshots-api.js';
import {createShardsApi} from './api/shards-api.js';
import {QdrantClientTimeoutError, QdrantClientUnexpectedResponseError} from './errors.js';
import {
QdrantClientResourceExhaustedError,
QdrantClientTimeoutError,
QdrantClientUnexpectedResponseError,
} from './errors.js';
import {RestArgs} from './types.js';

export type Client = ReturnType<typeof Fetcher.for<paths>>;
@@ -47,10 +51,21 @@ export function createClient(baseUrl: string, {headers, timeout, connections}: R
}

use.push(async (url, init, next) => {
const response = await next(url, init);
if (response.status === 200 || response.status === 201) {
return response;
let response;
try {
response = await next(url, init);

if (response.status === 200 || response.status === 201) {
return response;
}
} catch (error) {
if (error instanceof ApiError && error.status === 429) {
const retryAfterHeader = error.headers.get('retry-after')?.[0] ?? '';
throw new QdrantClientResourceExhaustedError(retryAfterHeader);
}
throw error;
}

throw QdrantClientUnexpectedResponseError.forResponse(response);
});

11 changes: 11 additions & 0 deletions packages/js-client-rest/src/errors.ts
Original file line number Diff line number Diff line change
@@ -29,3 +29,14 @@ export class QdrantClientUnexpectedResponseError extends CustomError {
export class QdrantClientConfigError extends CustomError {}

export class QdrantClientTimeoutError extends CustomError {}

export class QdrantClientResourceExhaustedError extends CustomError {
retry_after: string;

constructor(retryAfter: string) {
super('Resource exhausted: Retry after specified duration');
this.name = this.constructor.name;
this.retry_after = retryAfter;
Object.setPrototypeOf(this, new.target.prototype);
}
}
3,482 changes: 1,930 additions & 1,552 deletions pnpm-lock.yaml

Large diffs are not rendered by default.