-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessor.go
More file actions
152 lines (134 loc) · 3.32 KB
/
processor.go
File metadata and controls
152 lines (134 loc) · 3.32 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
"github.com/hashicorp/golang-lru/v2/expirable"
"golang.org/x/sync/errgroup"
)
type ProcessorConfig struct {
Buffer int
Workers int
}
type Listener interface {
enqueue(message *kafka.Message)
}
type dedupCache struct {
cache *expirable.LRU[string, bool]
mu sync.Mutex
}
type processFn func(context.Context, *kafka.Message, int, *dedupCache) error
type Processor struct {
queue chan (*kafka.Message)
wg errgroup.Group
config ProcessorConfig
processFn processFn
stat *Statistics
dedup *dedupCache
}
func newProcessor(config *Config) (*Processor, error) {
processor := &Processor{
queue: make(chan *kafka.Message, config.Processor.Buffer),
config: config.Processor,
stat: newStatistics(),
dedup: &dedupCache{
cache: expirable.NewLRU[string, bool](100000, nil,
time.Second*time.Duration(240)),
},
}
processFn, err := selectProcessFn(processor, config.Consumer.Topic)
if err != nil {
return nil, err
}
processor.processFn = processFn
return processor, nil
}
func selectProcessFn(p *Processor, topic string) (processFn, error) {
parts := strings.SplitN(topic, ".", 2)
if len(parts) < 2 {
return nil, fmt.Errorf("invalid topic: %s", topic)
}
chain, suffix := parts[0], parts[1]
suffix = strings.TrimPrefix(suffix, "broadcasted.")
switch chain {
case "solana":
switch suffix {
case "dextrades.proto":
return p.solanaDexTradesHandler, nil
case "tokens.proto":
return p.solanaTokensHandler, nil
case "transactions.proto":
return p.solanaTransactionsHandler, nil
}
case "tron":
switch suffix {
case "dextrades.proto":
return p.tronDexTradesHandler, nil
case "tokens.proto":
return p.tronTokensHandler, nil
case "transactions.proto":
return p.tronTransactionsHandler, nil
}
default:
switch suffix {
case "dextrades.proto":
return p.evmDexTradesHandler, nil
case "tokens.proto":
return p.evmTokensHandler, nil
case "transactions.proto":
return p.evmTransactionsHandler, nil
case "dexpools.proto":
return p.evmDexPoolsHandler, nil
case "predictions.proto":
return p.evmPredictionsHandler, nil
}
}
return nil, fmt.Errorf("unsupported topic: %s", topic)
}
func (processor *Processor) enqueue(message *kafka.Message) {
processor.queue <- message
}
func (processor *Processor) start(ctx context.Context) {
counter := 0
for i := 0; i < processor.config.Workers; i++ {
processor.wg.Go(func() error {
i := i
fmt.Println("Starting worker ", i)
for {
select {
case <-ctx.Done():
fmt.Println("Done, exiting processor loop worker ", i)
return nil
case message := <-processor.queue:
err := processor.processFn(ctx, message, i, processor.dedup)
if err != nil {
fmt.Println("Error processing message", err)
}
counter++
if counter%100 == 0 {
processor.stat.report()
}
}
}
return nil
})
}
}
func (processor *Processor) close() {
fmt.Println("Shutting down processor...")
processor.wg.Wait()
fmt.Println("Processor stopped")
processor.stat.report()
}
func (dedup *dedupCache) isDuplicated(key string) bool {
dedup.mu.Lock()
defer dedup.mu.Unlock()
if dedup.cache.Contains(key) {
return true
}
dedup.cache.Add(key, true)
return false
}