forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffered-storage.js
70 lines (57 loc) · 1.38 KB
/
buffered-storage.js
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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const log = require('../utils/logger')
const DEFAULT_BUFFER_SIZE = 10
class BufferedStorage {
constructor(dataSender, createSpanChunk, bufferSize = DEFAULT_BUFFER_SIZE) {
this.dataSender = dataSender
this.createSpanChunk = createSpanChunk
this.bufferSize = bufferSize
this.storage = null
}
storeSpanEvent (spanEvent) {
if (spanEvent) {
const storage = this.getBuffer()
storage.push(spanEvent)
if (this.overflow(storage)) {
const spanEventList = this.clearBuffer()
this.sendSpanChunk(spanEventList)
}
}
}
storeSpan (span) {
if (span) {
span.spanEventList = this.clearBuffer()
this.dataSender.send(span)
}
}
getBuffer () {
if (this.storage === null) {
this.storage = []
}
return this.storage
}
clearBuffer () {
const copy = this.storage
this.storage = null
return copy
}
flush () {
const spanEventList = this.clearBuffer()
this.sendSpanChunk(spanEventList)
}
overflow (storage) {
return storage.length >= this.bufferSize
}
sendSpanChunk (spanEventList) {
if (this.dataSender && spanEventList) {
const spanChunk = this.createSpanChunk(spanEventList)
this.dataSender.send(spanChunk)
}
}
}
module.exports = BufferedStorage