forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc-client-side-stream.js
71 lines (59 loc) · 2.11 KB
/
grpc-client-side-stream.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
71
/**
* Pinpoint Node.js Agent
* Copyright 2021-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const log = require('../utils/logger')
const GrpcStream = require('./grpc-stream')
const DEFAULT_CLIENT_REQUEST_TIMEOUT = 10 * 60 * 1000
class GrpcClientSideStream {
constructor(name, client, newStream) {
this.grpcStream = new GrpcStream(name, () => {
this.deadline = Date.now()
const self = this
const stream = newStream.call(client, (err, response) => {
if (response) {
if (log.isDebug()) {
log.debug(`GrpcClientSideStream Server side stream data. ${name} on(data): ${response}`)
}
} else if (err) {
if (log.isDebug()) {
log.debug(`GrpcClientSideStream callback err: ${err}`)
}
}
this.grpcStream.endWithStream(stream)
if (self.callback && typeof self.callback === 'function') {
self.callback(err, response)
}
})
stream.on('error', (e) => {
if (log.isDebug() && e && name) {
log.debug(`GrpcClientSideStream stream on error. ${name} on(error): ${JSON.stringify(e)}`)
}
})
stream.on('status', (status) => {
if (log.isDebug() && status && name) {
log.debug(`GrpcClientSideStream stream data. ${name} on(status): ${JSON.stringify(status)}`)
}
})
return stream
})
}
write(data) {
this.grpcStream.write(data)
const now = Date.now()
if (now - this.deadline > this.grpcStreamDeadline) {
this.grpcStream.end()
}
}
get grpcStreamDeadline() {
return this.deadlineOfConfig || DEFAULT_CLIENT_REQUEST_TIMEOUT
}
setDeadlineMinutes(deadline) {
if (typeof deadline === 'number') {
this.deadlineOfConfig = deadline * 60 * 1000
}
}
}
module.exports = GrpcClientSideStream