Files
dex/pkg/orderbook/cpp_orderbook.go
zeekayandHanzo Dev 45bff3f5b6 fix(dex): close price sources synchronously; build pkg/dpdk and pkg/orderbook on linux
`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]>
2026-08-02 03:19:43 -07:00

81 lines
1.7 KiB
Go

//go:build cgo
// +build cgo
package orderbook
/*
#include <stdlib.h>
#include <stdint.h> // uint64_t below; stdlib.h does not declare it
// Placeholder for C++ order book implementation
void* create_orderbook() { return NULL; }
void destroy_orderbook(void* ob) {}
int place_order(void* ob, uint64_t id, int type, int side, double price, double quantity) { return 0; }
int cancel_order(void* ob, uint64_t id) { return 0; }
*/
import "C"
import (
"unsafe"
)
// NewCppOrderBook creates a C++ implementation of the order book
func NewCppOrderBook(cfg Config) OrderBook {
return &cppOrderBook{
ptr: C.create_orderbook(),
}
}
type cppOrderBook struct {
ptr unsafe.Pointer
}
func (c *cppOrderBook) AddOrder(order *Order) uint64 {
if c.ptr == nil {
return 0
}
C.place_order(c.ptr, C.uint64_t(order.ID),
C.int(order.Type), C.int(order.Side),
C.double(order.Price), C.double(order.Quantity))
return order.ID
}
func (c *cppOrderBook) CancelOrder(orderID uint64) bool {
if c.ptr == nil {
return false
}
result := C.cancel_order(c.ptr, C.uint64_t(orderID))
return result == 0
}
func (c *cppOrderBook) ModifyOrder(orderID uint64, newPrice, newQuantity float64) bool {
// Placeholder implementation
return false
}
func (c *cppOrderBook) MatchOrders() []Trade {
// Placeholder implementation
return []Trade{}
}
func (c *cppOrderBook) GetBestBid() float64 {
// Placeholder implementation
return 0
}
func (c *cppOrderBook) GetBestAsk() float64 {
// Placeholder implementation
return 0
}
func (c *cppOrderBook) GetDepth(level int) *Depth {
// Placeholder implementation
return &Depth{
Bids: []PriceLevel{},
Asks: []PriceLevel{},
}
}
func (c *cppOrderBook) GetVolume() uint64 {
// Placeholder implementation
return 0
}