-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathChannel.ts
103 lines (88 loc) · 3.54 KB
/
Channel.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import Contracts = require("../Declarations/Contracts");
import Logging = require("./Logging");
import Sender = require("./Sender");
import Util = require("./Util");
class Channel {
protected _lastSend: number;
protected _timeoutHandle: any;
protected _isDisabled: () => boolean;
protected _getBatchSize: () => number;
protected _getBatchIntervalMs: () => number;
public _sender: Sender;
public _buffer: Contracts.EnvelopeTelemetry[];
constructor(isDisabled: () => boolean, getBatchSize: () => number, getBatchIntervalMs: () => number, sender: Sender) {
this._buffer = [];
this._lastSend = 0;
this._isDisabled = isDisabled;
this._getBatchSize = getBatchSize;
this._getBatchIntervalMs = getBatchIntervalMs;
this._sender = sender;
}
/**
* Enable or disable disk-backed retry caching to cache events when client is offline (enabled by default)
* These cached events are stored in your system or user's temporary directory and access restricted to your user when possible.
* @param value if true events that occurred while client is offline will be cached on disk
* @param resendInterval The wait interval for resending cached events.
* @param maxBytesOnDisk The maximum size (in bytes) that the created temporary directory for cache events can grow to, before caching is disabled.
* @returns {Configuration} this class
*/
public setUseDiskRetryCaching(value: boolean, resendInterval?: number, maxBytesOnDisk?: number) {
this._sender.setDiskRetryMode(value, resendInterval, maxBytesOnDisk);
}
/**
* Add a telemetry item to the send buffer
*/
public send(envelope: Contracts.EnvelopeTelemetry) {
// if master off switch is set, don't send any data
if (this._isDisabled()) {
// Do not send/save data
return;
}
// validate input
if (!envelope) {
Logging.warn("Cannot send null/undefined telemetry");
return;
}
// enqueue the payload
this._buffer.push(envelope);
// flush if we would exceed the max-size limit by adding this item
if (this._buffer.length >= this._getBatchSize()) {
this.triggerSend(false);
return;
}
// ensure an invocation timeout is set if anything is in the buffer
if (!this._timeoutHandle && this._buffer.length > 0) {
this._timeoutHandle = setTimeout(() => {
this._timeoutHandle = null;
this.triggerSend(false);
}, this._getBatchIntervalMs());
}
}
/**
* Immediately send buffered data
*/
public triggerSend(isNodeCrashing: boolean, callback?: (v: string) => void) {
let bufferIsEmpty = this._buffer.length < 1;
if (!bufferIsEmpty) {
// invoke send
if (isNodeCrashing || Util.isNodeExit) {
this._sender.saveOnCrash(this._buffer);
if (typeof callback === "function") {
callback("data saved on crash");
}
} else {
this._sender.send(this._buffer, callback);
}
}
// update lastSend time to enable throttling
this._lastSend = +new Date;
// clear buffer
this._buffer = [];
clearTimeout(this._timeoutHandle);
this._timeoutHandle = null;
if (bufferIsEmpty && typeof callback === "function") {
callback("no data to send");
}
}
}
export = Channel;