/
keepAlivePolicy.ts
66 lines (60 loc) · 1.64 KB
/
keepAlivePolicy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import {
BaseRequestPolicy,
RequestPolicy,
RequestPolicyOptions,
RequestPolicyFactory
} from "./requestPolicy";
import { WebResourceLike } from "../webResource";
import { HttpOperationResponse } from "../httpOperationResponse";
/**
* Options for how HTTP connections should be maintained for future
* requests.
*/
export interface KeepAliveOptions {
/*
* When true, connections will be kept alive for multiple requests.
* Defaults to true.
*/
enable: boolean;
}
export const DefaultKeepAliveOptions: KeepAliveOptions = {
enable: true
};
export function keepAlivePolicy(keepAliveOptions?: KeepAliveOptions): RequestPolicyFactory {
return {
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new KeepAlivePolicy(nextPolicy, options, keepAliveOptions || DefaultKeepAliveOptions);
}
};
}
/**
* KeepAlivePolicy is a policy used to control keep alive settings for every request.
*/
export class KeepAlivePolicy extends BaseRequestPolicy {
/**
* Creates an instance of KeepAlivePolicy.
*
* @param nextPolicy -
* @param options -
* @param keepAliveOptions -
*/
constructor(
nextPolicy: RequestPolicy,
options: RequestPolicyOptions,
private readonly keepAliveOptions: KeepAliveOptions
) {
super(nextPolicy, options);
}
/**
* Sends out request.
*
* @param request -
* @returns
*/
public async sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> {
request.keepAlive = this.keepAliveOptions.enable;
return this._nextPolicy.sendRequest(request);
}
}