UNPKG

4.6 kBTypeScriptView Raw
1/// <reference types="node" />
2import { IBackoffStrategy } from "./backoff/backoff";
3import { Host } from "./host";
4import * as http from "http";
5import * as https from "https";
6import * as urlModule from "url";
7export interface IPoolOptions {
8 /**
9 * Number of times we should retry running a query
10 * before calling back with an error.
11 */
12 maxRetries?: number;
13 /**
14 * The length of time after which HTTP requests will error
15 * if they do not receive a response.
16 */
17 requestTimeout?: number;
18 /**
19 * Options to configure the backoff policy for the pool. Defaults
20 * to using exponential backoff.
21 */
22 backoff?: IBackoffStrategy;
23}
24export interface IPoolRequestOptions {
25 /**
26 * Request method.
27 */
28 method: "GET" | "POST";
29 /**
30 * Path to hit on the database server, must begin with a leading slash.
31 */
32 path: string;
33 /**
34 * Query string to be appended to the request path.
35 */
36 query?: any;
37 /**
38 * Request body to include.
39 */
40 body?: string;
41 /**
42 * For internal use only, a counter of the number of times we've retried
43 * running this request.
44 */
45 retries?: number;
46}
47/**
48 * An ServiceNotAvailableError is returned as an error from requests that
49 * result in a > 500 error code.
50 */
51export declare class ServiceNotAvailableError extends Error {
52 constructor(message: string);
53}
54/**
55 * An RequestError is returned as an error from requests that
56 * result in a 300 <= error code <= 500.
57 */
58export declare class RequestError extends Error {
59 req: http.ClientRequest;
60 res: http.IncomingMessage;
61 constructor(req: http.ClientRequest, res: http.IncomingMessage, body: string);
62 static Create(req: http.ClientRequest, res: http.IncomingMessage, callback: (e: RequestError) => void): void;
63}
64export interface IPingStats {
65 url: urlModule.Url;
66 res: http.IncomingMessage;
67 online: boolean;
68 rtt: number;
69 version: string;
70}
71/**
72 *
73 * The Pool maintains a list available Influx hosts and dispatches requests
74 * to them. If there are errors connecting to hosts, it will disable that
75 * host for a period of time.
76 */
77export declare class Pool {
78 private readonly _options;
79 private _index;
80 private readonly _timeout;
81 private readonly _hostsAvailable;
82 private readonly _hostsDisabled;
83 /**
84 * Creates a new Pool instance.
85 * @param {IPoolOptions} options
86 */
87 constructor(options: IPoolOptions);
88 /**
89 * Returns a list of currently active hosts.
90 * @return {Host[]}
91 */
92 getHostsAvailable(): Host[];
93 /**
94 * Returns a list of hosts that are currently disabled due to network
95 * errors.
96 * @return {Host[]}
97 */
98 getHostsDisabled(): Host[];
99 /**
100 * Inserts a new host to the pool.
101 */
102 addHost(url: string, options?: https.RequestOptions): Host;
103 /**
104 * Returns true if there's any host available to by queried.
105 * @return {Boolean}
106 */
107 hostIsAvailable(): boolean;
108 /**
109 * Makes a request and calls back with the response, parsed as JSON.
110 * An error is returned on a non-2xx status code or on a parsing exception.
111 */
112 json(options: IPoolRequestOptions): Promise<any>;
113 /**
114 * Makes a request and resolves with the plain text response,
115 * if possible. An error is raised on a non-2xx status code.
116 */
117 text(options: IPoolRequestOptions): Promise<string>;
118 /**
119 * Makes a request and discards any response body it receives.
120 * An error is returned on a non-2xx status code.
121 */
122 discard(options: IPoolRequestOptions): Promise<void>;
123 /**
124 * Ping sends out a request to all available Influx servers, reporting on
125 * their response time and version number.
126 */
127 ping(timeout: number, path?: string, auth?: string | undefined): Promise<IPingStats[]>;
128 /**
129 * Makes a request and calls back with the IncomingMessage stream,
130 * if possible. An error is returned on a non-2xx status code.
131 */
132 stream(options: IPoolRequestOptions, callback: (err: Error, res: http.IncomingMessage) => void): void;
133 /**
134 * Returns the next available host for querying.
135 * @return {Host}
136 */
137 private _getHost;
138 /**
139 * Re-enables the provided host, returning it to the pool to query.
140 * @param {Host} host
141 */
142 private _enableHost;
143 /**
144 * Disables the provided host, removing it from the query pool. It will be
145 * re-enabled after a backoff interval
146 */
147 private _disableHost;
148 private _handleRequestError;
149}