-
Notifications
You must be signed in to change notification settings - Fork 0
/
RateLimitingServiceFactory.kt
44 lines (37 loc) · 1.69 KB
/
RateLimitingServiceFactory.kt
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
package com.wavesenterprise.sdk.node.client.blocking.ratelimit
import com.wavesenterprise.sdk.node.client.blocking.node.NodeBlockingServiceFactory
import com.wavesenterprise.sdk.node.client.blocking.privacy.PrivacyService
import com.wavesenterprise.sdk.node.client.blocking.tx.TxService
import com.wavesenterprise.sdk.node.domain.privacy.SendDataRequest
import com.wavesenterprise.sdk.node.domain.sign.SignRequest
import com.wavesenterprise.sdk.node.domain.tx.PolicyDataHashTx
import com.wavesenterprise.sdk.node.domain.tx.Tx
class RateLimitingServiceFactory(
private val nodeBlockingServiceFactory: NodeBlockingServiceFactory,
private val rateLimiter: RateLimiter,
) : NodeBlockingServiceFactory by nodeBlockingServiceFactory {
override fun txService(): TxService {
val txService = nodeBlockingServiceFactory.txService()
return object : TxService by txService {
override fun <T : Tx> broadcast(tx: T): T {
rateLimiter.waitIfNeeded()
return txService.broadcast(tx)
}
override fun <T : Tx> signAndBroadcast(request: SignRequest<T>): T {
rateLimiter.waitIfNeeded()
return txService.signAndBroadcast(request)
}
}
}
override fun privacyService(): PrivacyService {
val privacyService = nodeBlockingServiceFactory.privacyService()
return object : PrivacyService by privacyService {
override fun sendData(request: SendDataRequest): PolicyDataHashTx {
if (request.broadcastTx) {
rateLimiter.waitIfNeeded()
}
return privacyService.sendData(request)
}
}
}
}