-
Notifications
You must be signed in to change notification settings - Fork 18
/
position.go
171 lines (152 loc) · 5.63 KB
/
position.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
164
165
166
167
168
169
170
171
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package service
import (
"context"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/utils"
"code.vegaprotocol.io/vega/logging"
lru "github.com/hashicorp/golang-lru"
)
type PositionStore interface {
Flush(ctx context.Context) ([]entities.Position, error)
Add(ctx context.Context, p entities.Position) error
GetByMarketAndParty(ctx context.Context, marketID string, partyID string) (entities.Position, error)
GetByMarketAndParties(ctx context.Context, marketIDRaw string, partyIDsRaw []string) ([]entities.Position, error)
GetByMarket(ctx context.Context, marketID string) ([]entities.Position, error)
GetByParty(ctx context.Context, partyID string) ([]entities.Position, error)
GetByPartyConnection(ctx context.Context, partyID []string, marketID []string, pagination entities.CursorPagination) ([]entities.Position, entities.PageInfo, error)
GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Position, error)
GetAll(ctx context.Context) ([]entities.Position, error)
}
type positionCacheKey struct {
MarketID entities.MarketID
PartyID entities.PartyID
}
type Position struct {
store PositionStore
observer utils.Observer[entities.Position]
cache *lru.Cache
}
func NewPosition(store PositionStore, log *logging.Logger) *Position {
cache, err := lru.New(10000)
if err != nil {
panic(err)
}
return &Position{
store: store,
observer: utils.NewObserver[entities.Position]("positions", log, 0, 0),
cache: cache,
}
}
func (p *Position) Flush(ctx context.Context) error {
flushed, err := p.store.Flush(ctx)
if err != nil {
return err
}
p.observer.Notify(flushed)
return nil
}
func (p *Position) Add(ctx context.Context, pos entities.Position) error {
key := positionCacheKey{pos.MarketID, pos.PartyID}
p.cache.Add(key, pos)
return p.store.Add(ctx, pos)
}
func (p *Position) GetByMarketAndParties(ctx context.Context, marketID string, partyIDs []string) ([]entities.Position, error) {
missedParties := make([]string, 0, len(partyIDs))
ret := make([]entities.Position, 0, len(partyIDs))
key := positionCacheKey{
MarketID: entities.MarketID(marketID),
}
for _, partyID := range partyIDs {
key.PartyID = entities.PartyID(partyID)
if v, ok := p.cache.Get(key); ok {
switch val := v.(type) {
case entities.Position:
ret = append(ret, val)
default:
// this includes errors from cache, ignore them and try again?
missedParties = append(missedParties, partyID)
}
} else {
missedParties = append(missedParties, partyID)
}
}
// everything was cached, we're done
if len(missedParties) == 0 {
return ret, nil
}
storePos, err := p.store.GetByMarketAndParties(ctx, marketID, missedParties)
// append the positions from store to those from cache
ret = append(ret, storePos...)
if err == nil {
// we had cache misses, and got them from store, so add them to cache
for _, sp := range storePos {
key.PartyID = sp.PartyID
p.cache.Add(key, sp)
}
}
return ret, err
}
func (p *Position) GetByMarketAndParty(ctx context.Context, marketID string, partyID string) (entities.Position, error) {
key := positionCacheKey{entities.MarketID(marketID), entities.PartyID(partyID)}
value, ok := p.cache.Get(key)
if ok {
// make sure the value in cache is a position entity, ignore errors
if v, ok := value.(entities.Position); ok {
return v, nil
}
}
// either cache miss, or an error was cached, either way fall back to store and update cache
pos, err := p.store.GetByMarketAndParty(
ctx, marketID, partyID)
// let's not cache errors here
if err == nil {
p.cache.Add(key, pos)
}
return pos, err
}
func (p *Position) GetByMarket(ctx context.Context, marketID string) ([]entities.Position, error) {
return p.store.GetByMarket(ctx, marketID)
}
func (p *Position) GetByParty(ctx context.Context, partyID entities.PartyID) ([]entities.Position, error) {
return p.store.GetByParty(ctx, partyID.String())
}
func (p *Position) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Position, error) {
return p.store.GetByTxHash(ctx, txHash)
}
func (p *Position) GetByPartyConnection(ctx context.Context, partyIDs []entities.PartyID, marketIDs []entities.MarketID, pagination entities.CursorPagination) ([]entities.Position, entities.PageInfo, error) {
ps := make([]string, len(partyIDs))
for i, p := range partyIDs {
ps[i] = p.String()
}
ms := make([]string, len(marketIDs))
for i, m := range marketIDs {
ms[i] = m.String()
}
return p.store.GetByPartyConnection(ctx, ps, ms, pagination)
}
func (p *Position) GetAll(ctx context.Context) ([]entities.Position, error) {
return p.store.GetAll(ctx)
}
func (p *Position) Observe(ctx context.Context, retries int, partyID, marketID string) (<-chan []entities.Position, uint64) {
ch, ref := p.observer.Observe(ctx,
retries,
func(pos entities.Position) bool {
return (len(marketID) == 0 || marketID == pos.MarketID.String()) &&
(len(partyID) == 0 || partyID == pos.PartyID.String())
})
return ch, ref
}