forked from pinpoint-apm/pinpoint-node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc-unary-rpc.js
49 lines (42 loc) · 1.4 KB
/
grpc-unary-rpc.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
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/
'use strict'
const log = require('../utils/logger')
// DEFAULT_CLIENT_REQUEST_TIMEOUT in GrpcTransportConfig.java
const DEFAULT_CLIENT_REQUEST_TIMEOUT = 6000
class GrpcUnaryRPC {
constructor(name, client, unaryRPCFunction, retryInterval, retryTimes) {
this.name = name
this.client = client
this.unaryRPCFunction = unaryRPCFunction
this.retryInterval = retryInterval
this.retryTimes = retryTimes
}
request(data, callback, timesOfRetry = 1) {
const deadline = this.getDeadline()
const name = this.name
const self = this
this.unaryRPCFunction.call(this.client, data, { deadline }, (err, response) => {
if (name && err) {
log.error(`${name} err: ${err}`)
}
if (err && timesOfRetry < self.retryTimes) {
setTimeout(() => {
const times = timesOfRetry + 1
self.request(data, callback, times)
}, self.retryInterval)
} else if (callback) {
callback(err, response)
}
})
}
getDeadline() {
const deadline = new Date()
deadline.setMilliseconds(deadline.getMilliseconds() + DEFAULT_CLIENT_REQUEST_TIMEOUT)
return deadline
}
}
module.exports = GrpcUnaryRPC