mirror of
https://github.com/luxfi/dex.git
synced 2026-09-24 00:26:31 +00:00
`go test ./...` failed three ways on the runner. Two were build failures that
cannot happen on a mac, and one was a race that only loses under load.
pkg/dpdk — `undefined: syscall.SO_NOSIGPIPE`. DarwinOptimizations guarded itself
with `if runtime.GOOS != "darwin" { return }`, but SO_NOSIGPIPE is a COMPILE-time
constant that exists only on Darwin; a runtime check cannot save a reference the
linux build still has to resolve. Split into _darwin.go / _nodarwin.go. Verified
by cross-compiling both ways -- it failed for GOOS=linux before, both pass now.
pkg/orderbook — the cgo preamble uses uint64_t while including only <stdlib.h>.
macOS pulls stdint.h in transitively and glibc does not, so this built here and
never on the runner. Added the include.
pkg/price — TestXChainSourceStalePrice: "Expected price to be stale".
Not a flaky test; Close() was not a barrier. It closed the done channel and
returned WITHOUT waiting for the polling goroutine. A loop selecting on done
against a ticker picks a ready case AT RANDOM, so a poll could still be running
after Close() returned, and poll writes last[symbol] = time.Now(). The test
closes the source, backdates that timestamp by 10s, and asserts staleness -- an
in-flight poll overwrote it. Wide window on a loaded runner, near-zero on a
laptop, which is why it reproduces 0/30 here.
All EIGHT sources had it, so the fix is one shared `lifecycle` (embedded, so
`s.done` still resolves) whose stop() joins before returning and is safe to call
twice. Oracle had the same defect written as
// Give time for goroutines to exit
time.Sleep(100 * time.Millisecond)
which could not work either -- calcAverages blocks on a 1s ticker, so it ran up
to a full second after Stop() returned. Now on the same lifecycle, and its loops
select on done instead of polling a flag, so they leave promptly rather than
after one more tick. Nothing restarts a stopped source (checked: 0 tests), and
the other sources already could not.
go build ./... OK
GOOS=linux go build ./... OK (failed before)
go vet price/dpdk/orderbook OK
go test ./pkg/price/ -race ok, 3/3 runs
go test -run StalePrice -count=30 ok
Co-authored-by: Hanzo Dev <[email protected]>
175 lines
3.4 KiB
Go
175 lines
3.4 KiB
Go
package price
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// OrderbookSource provides prices from local orderbook data.
|
|
// This is the primary source for co-located low-latency clients.
|
|
// Target latency: <100ns for price lookup.
|
|
type OrderbookSource struct {
|
|
books OrderbookProvider
|
|
prices map[string]*Data
|
|
lastUpdate map[string]time.Time
|
|
interval time.Duration
|
|
|
|
mu sync.RWMutex
|
|
lifecycle
|
|
running bool
|
|
}
|
|
|
|
// OrderbookProvider abstracts orderbook access.
|
|
type OrderbookProvider interface {
|
|
GetBestBid(symbol string) float64
|
|
GetBestAsk(symbol string) float64
|
|
GetSymbols() []string
|
|
}
|
|
|
|
// NewOrderbookSource creates a price source from orderbook data.
|
|
func NewOrderbookSource(books OrderbookProvider) *OrderbookSource {
|
|
return &OrderbookSource{
|
|
books: books,
|
|
prices: make(map[string]*Data),
|
|
lastUpdate: make(map[string]time.Time),
|
|
interval: 10 * time.Millisecond,
|
|
lifecycle: newLifecycle(),
|
|
}
|
|
}
|
|
|
|
// Start begins price updates.
|
|
func (s *OrderbookSource) Start() error {
|
|
s.mu.Lock()
|
|
if s.running {
|
|
s.mu.Unlock()
|
|
return nil
|
|
}
|
|
s.running = true
|
|
s.mu.Unlock()
|
|
|
|
s.run(s.loop)
|
|
return nil
|
|
}
|
|
|
|
func (s *OrderbookSource) loop() {
|
|
ticker := time.NewTicker(s.interval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-s.done:
|
|
return
|
|
case <-ticker.C:
|
|
s.update()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *OrderbookSource) update() {
|
|
if s.books == nil {
|
|
return
|
|
}
|
|
|
|
symbols := s.books.GetSymbols()
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
for _, symbol := range symbols {
|
|
bid := s.books.GetBestBid(symbol)
|
|
ask := s.books.GetBestAsk(symbol)
|
|
|
|
if bid == 0 && ask == 0 {
|
|
continue
|
|
}
|
|
|
|
var price float64
|
|
if bid > 0 && ask > 0 {
|
|
price = (bid + ask) / 2
|
|
} else if bid > 0 {
|
|
price = bid
|
|
} else {
|
|
price = ask
|
|
}
|
|
|
|
// Spread-based confidence
|
|
var confidence float64 = 1.0
|
|
if bid > 0 && ask > 0 && price > 0 {
|
|
spread := (ask - bid) / price
|
|
confidence = 1.0 - spread
|
|
if confidence < 0.5 {
|
|
confidence = 0.5
|
|
}
|
|
}
|
|
|
|
s.prices[symbol] = &Data{
|
|
Symbol: symbol,
|
|
Price: price,
|
|
Bid: bid,
|
|
Ask: ask,
|
|
Confidence: confidence,
|
|
Timestamp: time.Now(),
|
|
Source: "orderbook",
|
|
}
|
|
s.lastUpdate[symbol] = time.Now()
|
|
}
|
|
}
|
|
|
|
// Price returns the latest price for a symbol.
|
|
func (s *OrderbookSource) Price(symbol string) (*Data, error) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
|
|
p, ok := s.prices[symbol]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
if time.Since(s.lastUpdate[symbol]) > time.Second {
|
|
copy := *p
|
|
copy.Stale = true
|
|
return ©, nil
|
|
}
|
|
|
|
copy := *p
|
|
return ©, nil
|
|
}
|
|
|
|
// Prices returns prices for multiple symbols.
|
|
func (s *OrderbookSource) Prices(symbols []string) (map[string]*Data, error) {
|
|
result := make(map[string]*Data)
|
|
for _, sym := range symbols {
|
|
if p, err := s.Price(sym); err == nil {
|
|
result[sym] = p
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Subscribe is a no-op (always tracking).
|
|
func (s *OrderbookSource) Subscribe(symbol string) error { return nil }
|
|
|
|
// Unsubscribe is a no-op.
|
|
func (s *OrderbookSource) Unsubscribe(symbol string) error { return nil }
|
|
|
|
// Healthy returns true if running.
|
|
func (s *OrderbookSource) Healthy() bool {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.running
|
|
}
|
|
|
|
// Name returns "orderbook".
|
|
func (s *OrderbookSource) Name() string { return "orderbook" }
|
|
|
|
// Weight returns 1.0 (base weight).
|
|
func (s *OrderbookSource) Weight() float64 { return 1.0 }
|
|
|
|
// Stop halts the source.
|
|
func (s *OrderbookSource) Stop() error {
|
|
s.stop()
|
|
s.mu.Lock()
|
|
s.running = false
|
|
s.mu.Unlock()
|
|
return nil
|
|
}
|