import * as grpc from '@grpc/grpc-js'; import { CouchbaseError } from '../errors'; import type { CouchbaseLogger } from '../logger'; import type { ObservableRequestHandler } from '../observabilityhandler'; import { MappedError, RetryReason } from './errormapping'; import { RetryRequest, RetryStrategy } from './retry'; import { GatewayAddress } from './tracing'; /** * What the retry core needs to know about an operation. * * @internal */ export interface Couchbase2Op { /** * The operation's name, as logged with each retry. */ readonly name: string; /** * Whether the operation changes nothing, so that running out of time leaves * no doubt about its outcome and any retry strategy may send it again. * This is what makes an operation idempotent; an upsert, which has the * same effect when sent twice, is not. */ readonly readOnly: boolean; } /** * What a service's calls need from the connection. * * @internal */ export interface Couchbase2Channel { /** * The metadata every call carries. */ readonly metadata: grpc.Metadata; /** * A new request's context, traced under `obs` when it is given. */ context(op: Couchbase2Op, timeoutMs: number, obs?: ObservableRequestHandler | null): RequestContext; } /** * What to do after a failed attempt: wait `delayMs` and retry, or raise * `error`. * * @internal */ export type FailureDecision = { retry: true; delayMs: number; } | { retry: false; error: CouchbaseError; }; /** * The optional parts of a {@link RequestContext}. * * @internal */ export interface RequestContextOptions { /** * Decides retries for a reason that does not always retry. */ strategy?: RetryStrategy; /** * Whether the cluster has been closed since the request began. */ closed?: () => boolean; /** * A monotonic clock in milliseconds. */ now?: () => number; /** * Where each retry is logged, at debug. */ logger?: CouchbaseLogger; /** * The gateway's address, reported as where a dispatched request was sent. */ target?: string; /** * The operation's observability handler, which each attempt is traced * under and each retry is counted on. */ obs?: ObservableRequestHandler | null; /** * The gateway's address, as each attempt's span reports it. */ address?: GatewayAddress; /** * Told of each failure value the SDK has no mapping for; see * {@link MappedError.unrecognized}. */ unrecognized?: (value: string) => void; } /** * One couchbase2 request's state across its attempts, and every decision * about retrying it. The decisions are synchronous and do no I/O; the * drivers in `retry.ts` and `streaming.ts` make the calls and do the * waiting. * * A request has one deadline, fixed when the context is made, and every * attempt is given what is left of it. An error is built only once the * request gives up, so that it carries the whole retry history. * * @internal */ export declare class RequestContext implements RetryRequest { readonly op: Couchbase2Op; readonly identifier: string; /** * When the request times out, on the context's clock. */ readonly deadline: number; retryAttempts: number; readonly retryReasons: RetryReason[]; /** * The failure the most recent retry was for, as * `(), caused by gRPC status `. */ lastError: string | undefined; /** * Whether any attempt was handed to grpc-js, and so may have reached the * gateway. */ dispatched: boolean; private readonly _strategy; private readonly _closed; private readonly _now; private readonly _logger; private readonly _target; private readonly _obs; private readonly _interceptor; private readonly _unrecognized; constructor(op: Couchbase2Op, timeoutMs: number, options?: RequestContextOptions); /** * Whether the operation changes nothing; see {@link Couchbase2Op.readOnly}. */ get readOnly(): boolean; /** * Whether the operation may be sent again whatever the reason; see * {@link Couchbase2Op.readOnly}. */ get idempotent(): boolean; /** * The milliseconds left before the deadline, never negative. */ remaining(): number; /** * The error to raise, without dispatching, when the next attempt cannot be * made; undefined when it can. */ check(): CouchbaseError | undefined; /** * The milliseconds to give the attempt about to be sent. */ dispatch(): number; /** * The options to send the attempt about to be sent with: what is left of * the deadline, and the interceptor that traces the attempt. */ callOptions(): grpc.CallOptions; /** * What to do after an attempt failed with `mapped`. */ onFailure(mapped: MappedError): FailureDecision; /** * The error to raise after a stream that had returned a message failed * with `mapped`. Retrying would replay what the caller has already read, * so a failure that would otherwise retry cancels the request instead; any * other is raised as it maps. */ onStreamFailure(mapped: MappedError): CouchbaseError; private _noteUnrecognized; private _history; private _error; private _canceled; /** * An error the request raises on its own account. Its status code is the * one grpc-js would have reported for the same outcome. */ private _fail; }