-
Notifications
You must be signed in to change notification settings - Fork 23
/
tracer.go
163 lines (139 loc) · 4.5 KB
/
tracer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sources
import (
"context"
"encoding/json"
"sync"
"time"
"github.com/Narasimha1997/ratelimiter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/tonkeeper/opentonapi/pkg/cache"
"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/tongo"
"go.uber.org/zap"
)
var (
traceNumber = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "streaming_api_trace_dispatch",
Help: "Number of traces",
}, []string{"type"})
)
type SubscribeToTraceOptions struct {
AllAccounts bool
Accounts []tongo.AccountID
}
type TraceSource interface {
SubscribeToTraces(ctx context.Context, deliveryFn DeliveryFn, opts SubscribeToTraceOptions) CancelFn
}
type storage interface {
GetTrace(ctx context.Context, hash tongo.Bits256) (*core.Trace, error)
}
type dispatcher interface {
dispatch(accountIDs []tongo.AccountID, event []byte)
registerSubscriber(fn DeliveryFn, options SubscribeToTraceOptions) CancelFn
}
type Tracer struct {
logger *zap.Logger
storage storage
dispatcher dispatcher
source TransactionSource
// mu protects traceCache.
// Tracer usually receives multiple tx hashes that are parts of the same trace,
// and we want to avoid sending the same trace to subscribers multiple times.
// We use a cache to keep track of already sent traces.
// "cache.Cache" doesn't provide a single atomic operation to check and set a value if it hasn't been set yet,
// so we use a mutex to serialize access to the cache.
mu sync.Mutex
traceCache cache.Cache[string, struct{}]
}
func NewTracer(logger *zap.Logger, storage storage, source TransactionSource) *Tracer {
return &Tracer{
logger: logger,
storage: storage,
source: source,
dispatcher: NewTraceDispatcher(logger),
traceCache: cache.NewLRUCache[string, struct{}](10000, "tracer_trace_cache"),
}
}
var _ TraceSource = (*Tracer)(nil)
func (t *Tracer) SubscribeToTraces(ctx context.Context, deliveryFn DeliveryFn, opts SubscribeToTraceOptions) CancelFn {
t.logger.Debug("subscribe to traces",
zap.Bool("all-accounts", opts.AllAccounts),
zap.Stringers("accounts", opts.Accounts))
return t.dispatcher.registerSubscriber(deliveryFn, opts)
}
func (t *Tracer) Run(ctx context.Context) {
txCh := make(chan TransactionEventData, 1000)
cancelFn := t.source.SubscribeToTransactions(ctx, func(eventData []byte) {
var tx TransactionEventData
if err := json.Unmarshal(eventData, &tx); err != nil {
t.logger.Error("json.Unmarshal() failed", zap.Error(err))
return
}
txCh <- tx
}, SubscribeToTransactionsOptions{AllAccounts: true, AllOperations: true})
defer cancelFn()
limiter := ratelimiter.NewDefaultLimiter(300, 10*time.Second)
defer limiter.Kill()
for txEvent := range txCh {
if ctx.Err() != nil {
return
}
if allow, _ := limiter.ShouldAllow(1); !allow {
traceNumber.With(map[string]string{"type": "dropped"}).Inc()
continue
}
go func(txEvent TransactionEventData) {
var hash tongo.Bits256
if err := hash.FromHex(txEvent.TxHash); err != nil {
// this should never happen
t.logger.Error("hash.FromHex() failed", zap.Error(err))
return
}
for i := 0; i < 15; i++ {
trace, err := t.storage.GetTrace(ctx, hash)
if err != nil {
time.Sleep(1 * time.Second)
continue
}
t.dispatch(trace)
return
}
traceNumber.With(map[string]string{"type": "failed-to-load"}).Inc()
}(txEvent)
}
}
// putTraceInCache returns true if the trace was not in the cache before.
func (t *Tracer) putTraceInCache(hash string) (success bool) {
t.mu.Lock()
defer t.mu.Unlock()
if _, ok := t.traceCache.Get(hash); ok {
return false
}
t.traceCache.Set(hash, struct{}{}, cache.WithExpiration(10*time.Minute))
return true
}
func (t *Tracer) dispatch(trace *core.Trace) {
if trace.InProgress() {
traceNumber.With(map[string]string{"type": "dispatched-in-progress"}).Inc()
return
}
traceNumber.With(map[string]string{"type": "dispatched-completed"}).Inc()
if success := t.putTraceInCache(trace.Hash.Hex()); !success {
// ok, this trace is already in cache meaning that we already sent it to subscribers.
// ignore it.
return
}
traceNumber.With(map[string]string{"type": "converted-to-event"}).Inc()
accounts := core.DistinctAccounts(trace)
eventData := &TraceEventData{
AccountIDs: accounts,
Hash: trace.Hash.Hex(),
}
eventJSON, err := json.Marshal(eventData)
if err != nil {
t.logger.Error("json.Marshal() failed: %v", zap.Error(err))
return
}
t.dispatcher.dispatch(accounts, eventJSON)
}