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
package multisend
import (
"context"
"encoding/json"
//"fmt"
"runtime"
"time"
"github.com/go-redis/redis/v8"
"golang.org/x/time/rate"
)
var (
jobnum = runtime.NumCPU()
)
type Job struct {
Client *redis.Client
Id int
}
func initClient(poolSize int, redisAddr, passwd string) *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
DialTimeout: time.Second,
ReadTimeout: time.Second,
WriteTimeout: time.Second,
PoolSize: poolSize,
Password: passwd,
DB: 0,
})
if err := client.FlushAll(context.Background()).Err(); err != nil {
panic(err)
}
return client
}
func Start(redisAddr, passwd string) {
client := initClient(10, redisAddr, passwd)
count := 0
limiter := rate.NewLimiter(rate.Every(time.Millisecond*100), 1)
cxt, _ := context.WithCancel(context.TODO())
for {
limiter.Wait(cxt)
select {
case batchTxs := <-batchTxsForRedis:
//startTime := time.Now()
// data, err := proto.Marshal(txs)
// if err != nil {
// panic(err)
// }
batchTxsAsBytes, err := json.Marshal(batchTxs)
if err != nil {
panic(err)
}
if err := client.LPush(context.Background(), "list", batchTxsAsBytes).Err(); err != nil {
panic(err)
}
count += 1
//fmt.Printf("count %d txs size: %d takes %v time: %s \n", count, len(batchTxsAsBytes), time.Since(startTime), time.Now())
}
}
}