-
Notifications
You must be signed in to change notification settings - Fork 11
/
subqueue.go
74 lines (65 loc) · 1.67 KB
/
subqueue.go
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
72
73
74
package tuna
import (
"log"
"time"
"github.com/nknorg/nkn-sdk-go"
"github.com/nknorg/nkn/v2/config"
)
const (
subQueueLen = 1024
maxRetry = 3
)
type subscribeData struct {
client *nkn.MultiClient
identifier string
topic string
duration int
meta string
config *nkn.TransactionConfig
replaceTxPool bool
}
var subQueue chan *subscribeData
func init() {
subQueue = make(chan *subscribeData, subQueueLen)
go func() {
for subData := range subQueue {
for i := 0; i < maxRetry; i++ {
if subData.replaceTxPool {
nonce, err := subData.client.GetNonce(false)
if err != nil {
log.Println("get nonce error:", err)
time.Sleep(time.Second)
continue
}
subData.config.Nonce = nonce
subData.config.FixNonce = true
}
txnHash, err := subData.client.Subscribe(subData.identifier, subData.topic, subData.duration, subData.meta, subData.config)
if err != nil {
log.Println("subscribe to topic", subData.topic, "error:", err)
time.Sleep(time.Second)
continue
}
log.Println("Subscribed to topic", subData.topic, "success:", txnHash)
break
}
time.Sleep(config.ConsensusTimeout)
}
}()
}
func addToSubscribeQueue(client *nkn.MultiClient, identifier string, topic string, duration int, meta string, config *nkn.TransactionConfig, replaceTxPool bool) {
subData := &subscribeData{
client: client,
identifier: identifier,
topic: topic,
duration: duration,
meta: meta,
config: config,
replaceTxPool: replaceTxPool,
}
select {
case subQueue <- subData:
default:
log.Println("Subscribe queue full, discard request.")
}
}