-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconsumer.go
131 lines (108 loc) · 3.36 KB
/
consumer.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
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
package async_geyser
import (
"context"
"time"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/code-payments/code-server/pkg/metrics"
"github.com/code-payments/code-server/pkg/code/common"
)
func (p *service) consumeGeyserProgramUpdateEvents(ctx context.Context) error {
log := p.log.WithField("method", "consumeGeyserProgramUpdateEvents")
for {
// Is the service stopped?
select {
case <-ctx.Done():
return ctx.Err()
default:
}
err := p.subscribeToProgramUpdatesFromGeyser(ctx, p.conf.grpcPluginEndpoint.Get(ctx))
if err != nil && !errors.Is(err, context.Canceled) {
log.WithError(err).Warn("program update consumer unexpectedly terminated")
}
// Avoid spamming new connections when something is wrong
time.Sleep(time.Second)
}
}
func (p *service) consumeGeyserSlotUpdateEvents(ctx context.Context) error {
log := p.log.WithField("method", "consumeGeyserSlotUpdateEvents")
for {
// Is the service stopped?
select {
case <-ctx.Done():
return ctx.Err()
default:
}
err := p.subscribeToSlotUpdatesFromGeyser(ctx, p.conf.grpcPluginEndpoint.Get(ctx))
if err != nil && !errors.Is(err, context.Canceled) {
log.WithError(err).Warn("slot update consumer unexpectedly terminated")
}
// Avoid spamming new connections when something is wrong
time.Sleep(time.Second)
}
}
func (p *service) programUpdateWorker(serviceCtx context.Context, id int) {
p.metricStatusLock.Lock()
_, ok := p.programUpdateWorkerMetrics[id]
if !ok {
p.programUpdateWorkerMetrics[id] = &eventWorkerMetrics{}
}
p.programUpdateWorkerMetrics[id].active = false
p.metricStatusLock.Unlock()
log := p.log.WithFields(logrus.Fields{
"method": "programUpdateWorker",
"worker_id": id,
})
log.Debug("worker started")
defer func() {
log.Debug("worker stopped")
}()
for update := range p.programUpdatesChan {
func() {
nr := serviceCtx.Value(metrics.NewRelicContextKey).(*newrelic.Application)
m := nr.StartTransaction("async__geyser_consumer_service__program_update_worker")
defer m.End()
tracedCtx := newrelic.NewContext(serviceCtx, m)
p.metricStatusLock.Lock()
p.programUpdateWorkerMetrics[id].active = true
p.metricStatusLock.Unlock()
defer func() {
p.metricStatusLock.Lock()
p.programUpdateWorkerMetrics[id].active = false
p.metricStatusLock.Unlock()
}()
publicKey, err := common.NewAccountFromPublicKeyBytes(update.Pubkey)
if err != nil {
log.WithError(err).Warn("invalid public key")
return
}
program, err := common.NewAccountFromPublicKeyBytes(update.Owner)
if err != nil {
log.WithError(err).Warn("invalid owner account")
return
}
log := log.WithFields(logrus.Fields{
"program": program.PublicKey().ToBase58(),
"account": publicKey.PublicKey().ToBase58(),
"slot": update.Slot,
})
if update.TxSignature != nil {
log = log.WithField("transaction", *update.TxSignature)
}
handler, ok := p.programUpdateHandlers[program.PublicKey().ToBase58()]
if !ok {
log.Debug("not handling update from program")
return
}
err = handler.Handle(tracedCtx, update)
if err != nil {
m.NoticeError(err)
log.WithError(err).Warn("failed to process program account update")
}
p.metricStatusLock.Lock()
p.programUpdateWorkerMetrics[id].eventsProcessed += 1
p.metricStatusLock.Unlock()
}()
}
}