feature: add ExchangePublic

This commit is contained in:
zenix.huang 2024-03-12 12:10:42 +09:00
parent c5dbd4b961
commit ec45ad3bdc
3 changed files with 10 additions and 4 deletions

6
pkg/cache/cache.go vendored
View File

@ -126,7 +126,7 @@ func WithCache(key string, obj interface{}, fetcher DataFetcher) error {
return nil return nil
} }
func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (markets types.MarketMap, err error) { func LoadExchangeMarketsWithCache(ctx context.Context, ex types.ExchangePublic) (markets types.MarketMap, err error) {
inMem, ok := util.GetEnvVarBool("USE_MARKETS_CACHE_IN_MEMORY") inMem, ok := util.GetEnvVarBool("USE_MARKETS_CACHE_IN_MEMORY")
if ok && inMem { if ok && inMem {
return loadMarketsFromMem(ctx, ex) return loadMarketsFromMem(ctx, ex)
@ -137,7 +137,7 @@ func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (marke
} }
// loadMarketsFromMem is useful for one process to run multiple bbgos in different go routines. // loadMarketsFromMem is useful for one process to run multiple bbgos in different go routines.
func loadMarketsFromMem(ctx context.Context, ex types.Exchange) (markets types.MarketMap, _ error) { func loadMarketsFromMem(ctx context.Context, ex types.ExchangePublic) (markets types.MarketMap, _ error) {
exName := ex.Name().String() exName := ex.Name().String()
if globalMarketMemCache.IsOutdated(exName) { if globalMarketMemCache.IsOutdated(exName) {
op := func() error { op := func() error {
@ -162,7 +162,7 @@ func loadMarketsFromMem(ctx context.Context, ex types.Exchange) (markets types.M
return rst, nil return rst, nil
} }
func loadMarketsFromFile(ctx context.Context, ex types.Exchange) (markets types.MarketMap, err error) { func loadMarketsFromFile(ctx context.Context, ex types.ExchangePublic) (markets types.MarketMap, err error) {
key := fmt.Sprintf("%s-markets", ex.Name()) key := fmt.Sprintf("%s-markets", ex.Name())
if futureExchange, implemented := ex.(types.FuturesExchange); implemented { if futureExchange, implemented := ex.(types.FuturesExchange); implemented {
settings := futureExchange.GetFuturesSettings() settings := futureExchange.GetFuturesSettings()

View File

@ -67,7 +67,7 @@ func Test_loadMarketsFromMem(t *testing.T) {
mockCtrl := gomock.NewController(t) mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish() defer mockCtrl.Finish()
mockEx := mocks.NewMockExchange(mockCtrl) mockEx := mocks.NewMockExchangePublic(mockCtrl)
mockEx.EXPECT().Name().Return(types.ExchangeName("max")).AnyTimes() mockEx.EXPECT().Name().Return(types.ExchangeName("max")).AnyTimes()
mockEx.EXPECT().QueryMarkets(gomock.Any()).Return(nil, errors.New("faked")).Times(1) mockEx.EXPECT().QueryMarkets(gomock.Any()).Return(nil, errors.New("faked")).Times(1)
mockEx.EXPECT().QueryMarkets(gomock.Any()).Return(types.MarketMap{ mockEx.EXPECT().QueryMarkets(gomock.Any()).Return(types.MarketMap{

View File

@ -87,6 +87,12 @@ type Exchange interface {
ExchangeTradeService ExchangeTradeService
} }
//go:generate mockgen -destination=mocks/mock_exchange_public.go -package=mocks . ExchangePublic
type ExchangePublic interface {
ExchangeMinimal
ExchangeMarketDataService
}
// ExchangeBasic is the new type for replacing the original Exchange interface // ExchangeBasic is the new type for replacing the original Exchange interface
type ExchangeBasic = Exchange type ExchangeBasic = Exchange