1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.DeadlineFilterFactory = exports.DeadlineFilter = void 0;
|
20 | const constants_1 = require("./constants");
|
21 | const filter_1 = require("./filter");
|
22 | const units = [
|
23 | ['m', 1],
|
24 | ['S', 1000],
|
25 | ['M', 60 * 1000],
|
26 | ['H', 60 * 60 * 1000],
|
27 | ];
|
28 | function getDeadline(deadline) {
|
29 | const now = new Date().getTime();
|
30 | const timeoutMs = Math.max(deadline - now, 0);
|
31 | for (const [unit, factor] of units) {
|
32 | const amount = timeoutMs / factor;
|
33 | if (amount < 1e8) {
|
34 | return String(Math.ceil(amount)) + unit;
|
35 | }
|
36 | }
|
37 | throw new Error('Deadline is too far in the future');
|
38 | }
|
39 | class DeadlineFilter extends filter_1.BaseFilter {
|
40 | constructor(channel, callStream) {
|
41 | super();
|
42 | this.channel = channel;
|
43 | this.callStream = callStream;
|
44 | this.timer = null;
|
45 | this.deadline = Infinity;
|
46 | this.retreiveDeadline();
|
47 | this.runTimer();
|
48 | }
|
49 | retreiveDeadline() {
|
50 | const callDeadline = this.callStream.getDeadline();
|
51 | if (callDeadline instanceof Date) {
|
52 | this.deadline = callDeadline.getTime();
|
53 | }
|
54 | else {
|
55 | this.deadline = callDeadline;
|
56 | }
|
57 | }
|
58 | runTimer() {
|
59 | var _a, _b;
|
60 | if (this.timer) {
|
61 | clearTimeout(this.timer);
|
62 | }
|
63 | const now = new Date().getTime();
|
64 | const timeout = this.deadline - now;
|
65 | if (timeout <= 0) {
|
66 | process.nextTick(() => {
|
67 | this.callStream.cancelWithStatus(constants_1.Status.DEADLINE_EXCEEDED, 'Deadline exceeded');
|
68 | });
|
69 | }
|
70 | else if (this.deadline !== Infinity) {
|
71 | this.timer = setTimeout(() => {
|
72 | this.callStream.cancelWithStatus(constants_1.Status.DEADLINE_EXCEEDED, 'Deadline exceeded');
|
73 | }, timeout);
|
74 | (_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
75 | }
|
76 | }
|
77 | refresh() {
|
78 | this.retreiveDeadline();
|
79 | this.runTimer();
|
80 | }
|
81 | async sendMetadata(metadata) {
|
82 | if (this.deadline === Infinity) {
|
83 | return metadata;
|
84 | }
|
85 | |
86 |
|
87 |
|
88 | const finalMetadata = await metadata;
|
89 | const timeoutString = getDeadline(this.deadline);
|
90 | finalMetadata.set('grpc-timeout', timeoutString);
|
91 | return finalMetadata;
|
92 | }
|
93 | receiveTrailers(status) {
|
94 | if (this.timer) {
|
95 | clearTimeout(this.timer);
|
96 | }
|
97 | return status;
|
98 | }
|
99 | }
|
100 | exports.DeadlineFilter = DeadlineFilter;
|
101 | class DeadlineFilterFactory {
|
102 | constructor(channel) {
|
103 | this.channel = channel;
|
104 | }
|
105 | createFilter(callStream) {
|
106 | return new DeadlineFilter(this.channel, callStream);
|
107 | }
|
108 | }
|
109 | exports.DeadlineFilterFactory = DeadlineFilterFactory;
|
110 |
|
\ | No newline at end of file |