1 | /*!
|
2 | * Copyright 2018 Google Inc. All Rights Reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 | /// <reference types="node" />
|
17 | import { CallOptions, grpc } from 'google-gax';
|
18 | import defer = require('p-defer');
|
19 | import { Message, Subscriber } from './subscriber';
|
20 | declare type QueuedMessages = Array<[string, number?]>;
|
21 | export interface BatchOptions {
|
22 | callOptions?: CallOptions;
|
23 | maxMessages?: number;
|
24 | maxMilliseconds?: number;
|
25 | }
|
26 | /**
|
27 | * Error class used to signal a batch failure.
|
28 | *
|
29 | * @class
|
30 | *
|
31 | * @param {string} message The error message.
|
32 | * @param {ServiceError} err The grpc service error.
|
33 | */
|
34 | export declare class BatchError extends Error implements grpc.ServiceError {
|
35 | ackIds: string[];
|
36 | code: grpc.status;
|
37 | details: string;
|
38 | metadata: grpc.Metadata;
|
39 | constructor(err: grpc.ServiceError, ackIds: string[], rpc: string);
|
40 | }
|
41 | /**
|
42 | * @typedef {object} BatchOptions
|
43 | * {object} [callOptions] Request configuration option, outlined
|
44 | * here: {//googleapis.github.io/gax-nodejs/interfaces/CallOptions.html}.
https: |
45 | * number} [maxMessages=3000] Maximum number of messages allowed in
{ |
46 | * each batch sent.
|
47 | * number} [maxMilliseconds=100] Maximum duration to wait before
{ |
48 | * sending a batch. Batches can be sent earlier if the maxMessages option
|
49 | * is met before the configured duration has passed.
|
50 | */
|
51 | /**
|
52 | * Class for buffering ack/modAck requests.
|
53 | *
|
54 | * @private
|
55 | * @class
|
56 | *
|
57 | * @param {Subscriber} sub The subscriber we're queueing requests for.
|
58 | * @param {BatchOptions} options Batching options.
|
59 | */
|
60 | export declare abstract class MessageQueue {
|
61 | numPendingRequests: number;
|
62 | numInFlightRequests: number;
|
63 | protected _onFlush?: defer.DeferredPromise<void>;
|
64 | protected _onDrain?: defer.DeferredPromise<void>;
|
65 | protected _options: BatchOptions;
|
66 | protected _requests: QueuedMessages;
|
67 | protected _subscriber: Subscriber;
|
68 | protected _timer?: NodeJS.Timer;
|
69 | protected abstract _sendBatch(batch: QueuedMessages): Promise<void>;
|
70 | constructor(sub: Subscriber, options?: BatchOptions);
|
71 | /**
|
72 | * Gets the default buffer time in ms.
|
73 | *
|
74 | * @returns {number}
|
75 | *
|
76 | */
|
77 | get maxMilliseconds(): number;
|
78 | /**
|
79 | * Adds a message to the queue.
|
80 | *
|
81 | * @param {Message} message The message to add.
|
82 | * @param {number} [deadline] The deadline.
|
83 | * @private
|
84 | */
|
85 | add({ ackId }: Message, deadline?: number): void;
|
86 | /**
|
87 | * Sends a batch of messages.
|
88 | * @private
|
89 | */
|
90 | flush(): Promise<void>;
|
91 | /**
|
92 | * Returns a promise that resolves after the next flush occurs.
|
93 | *
|
94 | * @returns {Promise}
|
95 | * @private
|
96 | */
|
97 | onFlush(): Promise<void>;
|
98 | /**
|
99 | * Returns a promise that resolves when all in-flight messages have settled.
|
100 | */
|
101 | onDrain(): Promise<void>;
|
102 | /**
|
103 | * Set the batching options.
|
104 | *
|
105 | * @param {BatchOptions} options Batching options.
|
106 | * @private
|
107 | */
|
108 | setOptions(options: BatchOptions): void;
|
109 | }
|
110 | /**
|
111 | * Queues up Acknowledge (ack) requests.
|
112 | *
|
113 | * @private
|
114 | * @class
|
115 | */
|
116 | export declare class AckQueue extends MessageQueue {
|
117 | /**
|
118 | * Sends a batch of ack requests.
|
119 | *
|
120 | * @private
|
121 | *
|
122 | * @param {Array.<Array.<string|number>>} batch Array of ackIds and deadlines.
|
123 | * @return {Promise}
|
124 | */
|
125 | protected _sendBatch(batch: QueuedMessages): Promise<void>;
|
126 | }
|
127 | /**
|
128 | * Queues up ModifyAckDeadline requests and sends them out in batches.
|
129 | *
|
130 | * @private
|
131 | * @class
|
132 | */
|
133 | export declare class ModAckQueue extends MessageQueue {
|
134 | /**
|
135 | * Sends a batch of modAck requests. Each deadline requires its own request,
|
136 | * so we have to group all the ackIds by deadline and send multiple requests.
|
137 | *
|
138 | * @private
|
139 | *
|
140 | * @param {Array.<Array.<string|number>>} batch Array of ackIds and deadlines.
|
141 | * @return {Promise}
|
142 | */
|
143 | protected _sendBatch(batch: QueuedMessages): Promise<void>;
|
144 | }
|
145 | export {};
|