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
56 lines (46 loc) · 1.65 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
/**
* 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 = 5 * 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('status', (status) => {
if (log.isDebug() && status) {
log.debug(`GrpcClientSideStream Server side stream data. ${name} on(status): ${JSON.stringify(status)}`)
}
})
return stream
})
}
write(data) {
this.grpcStream.write(data)
const now = Date.now()
if (now - this.deadline > DEFAULT_CLIENT_REQUEST_TIMEOUT) {
this.grpcStream.end()
}
}
}
module.exports = GrpcClientSideStream