|
|
|
@ -63,7 +63,7 @@ func (svc *ExchangeService) subscribeExchanges() { |
|
|
|
// 交易所订阅交易产品
|
|
|
|
// 交易所订阅交易产品
|
|
|
|
for _, exchange := range svc.exchangeMap { |
|
|
|
for _, exchange := range svc.exchangeMap { |
|
|
|
go func(exchange *Exchange) { |
|
|
|
go func(exchange *Exchange) { |
|
|
|
// get exchange trade instances
|
|
|
|
// get exchange all trade instances
|
|
|
|
insts, err := svc.tradeInstanceAside.ListExchangeTradeInstance(context.Background(), exchange.ExchangeType) |
|
|
|
insts, err := svc.tradeInstanceAside.ListExchangeTradeInstance(context.Background(), exchange.ExchangeType) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
zlog.Error(err) |
|
|
|
zlog.Error(err) |
|
|
|
@ -83,26 +83,28 @@ func (svc *ExchangeService) subscribeExchanges() { |
|
|
|
Leverages: inst.Leverages, |
|
|
|
Leverages: inst.Leverages, |
|
|
|
Exchange: exchange.ExchangeType, |
|
|
|
Exchange: exchange.ExchangeType, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 待初始化币种数据
|
|
|
|
|
|
|
|
processingInsts = append(processingInsts, *tradeInst) |
|
|
|
|
|
|
|
|
|
|
|
exchange.TradeInstIds.Store(inst.InstId, inst.ExchangeInstId) |
|
|
|
exchange.TradeInstIds.Store(inst.InstId, inst.ExchangeInstId) |
|
|
|
exchange.ExchangeInsts.Store(inst.ExchangeInstId, &ExchangeTradeInstance{ |
|
|
|
|
|
|
|
|
|
|
|
exchangeInst := &ExchangeTradeInstance{ |
|
|
|
Inst: tradeInst, |
|
|
|
Inst: tradeInst, |
|
|
|
LiveKline: types.NewIntervalState[types.Kline](), |
|
|
|
LiveKline: types.NewIntervalState[types.Kline](), |
|
|
|
LiveKStartTs: types.NewIntervalState[int64](), |
|
|
|
LiveKStartTs: types.NewIntervalState[int64](), |
|
|
|
HistoryMarkTs: types.NewIntervalState[int64](), |
|
|
|
HistoryMarkTs: types.NewIntervalState[int64](), |
|
|
|
}) |
|
|
|
|
|
|
|
// 待初始化币种数据
|
|
|
|
|
|
|
|
if inst.Status == int32(data.StatusProcessing) { |
|
|
|
|
|
|
|
processingInsts = append(processingInsts, *tradeInst) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
exchangeInst.Status.Store(int32(data.StatusProcessing)) |
|
|
|
|
|
|
|
exchange.ExchangeInsts.Store(inst.ExchangeInstId, exchangeInst) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// instIds := []string{"BTC-USDT", "DOGE-USDT-SWAP"}
|
|
|
|
// 订阅实时k线数据
|
|
|
|
err = exchange.Subscriber.SubscribeKline(exchangeInstIds...) |
|
|
|
err = exchange.Subscriber.SubscribeKline(exchangeInstIds...) // []string{"BTC-USDT", "DOGE-USDT-SWAP"}
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
zlog.Error(err) |
|
|
|
zlog.Error(err) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 消费实时k线数据
|
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
c := exchange.Subscriber.ConsumerKline() |
|
|
|
c := exchange.Subscriber.ConsumerKline() |
|
|
|
svc.consumerKline(exchange, c) |
|
|
|
svc.consumerKline(exchange, c) |
|
|
|
@ -110,7 +112,7 @@ func (svc *ExchangeService) subscribeExchanges() { |
|
|
|
zlog.Infof("unsubscribe exchange: %s", exchange.ExchangeType) |
|
|
|
zlog.Infof("unsubscribe exchange: %s", exchange.ExchangeType) |
|
|
|
}() |
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
// 初始化k线数据
|
|
|
|
// 初始化历史k线数据
|
|
|
|
go svc.initialKlines(exchange, processingInsts) |
|
|
|
go svc.initialKlines(exchange, processingInsts) |
|
|
|
}(exchange) |
|
|
|
}(exchange) |
|
|
|
} |
|
|
|
} |
|
|
|
@ -355,7 +357,7 @@ func (svc *ExchangeService) initialTradeInstanceKlines(exchange *Exchange, trade |
|
|
|
zlog.Infof("retry fetch history kline task %d times: task -> %s", task.times, task.logKey()) |
|
|
|
zlog.Infof("retry fetch history kline task %d times: task -> %s", task.times, task.logKey()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if lastKlineTs, ex := svc.fetchTaskKlines(exchange, task); ex != nil { |
|
|
|
if lastKlineTs, ex := svc.fetchTaskKlinesToTSDB(exchange, task); ex != nil { |
|
|
|
failTasks.Add(1) |
|
|
|
failTasks.Add(1) |
|
|
|
if task.times >= SingleKlineFetchTaskMaxFailTimes { |
|
|
|
if task.times >= SingleKlineFetchTaskMaxFailTimes { |
|
|
|
err = fmt.Errorf("task failed to many times %d, key: %s, err: %v", task.times, task.logKey(), ex) |
|
|
|
err = fmt.Errorf("task failed to many times %d, key: %s, err: %v", task.times, task.logKey(), ex) |
|
|
|
@ -375,9 +377,7 @@ func (svc *ExchangeService) initialTradeInstanceKlines(exchange *Exchange, trade |
|
|
|
exchangeInst.HistoryMarkTs.SetIf(task.interval, lastKlineTs, func(old int64) bool { |
|
|
|
exchangeInst.HistoryMarkTs.SetIf(task.interval, lastKlineTs, func(old int64) bool { |
|
|
|
return lastKlineTs > old |
|
|
|
return lastKlineTs > old |
|
|
|
}) |
|
|
|
}) |
|
|
|
// historyMarkTsMu.Lock()
|
|
|
|
// todo set trade instance status ok
|
|
|
|
// historyMarkTs[task.interval] = max(historyMarkTs[task.interval], lastKlineTs)
|
|
|
|
|
|
|
|
// historyMarkTsMu.Unlock()
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
zlog.Debugf("trade instance initial kline tasks processing: %s(%s), pub %d, sub %d, fail %d", tradeInst.InstId, tradeInst.Exchange, pubTasks.Load(), subTasks.Load(), failTasks.Load()) |
|
|
|
zlog.Debugf("trade instance initial kline tasks processing: %s(%s), pub %d, sub %d, fail %d", tradeInst.InstId, tradeInst.Exchange, pubTasks.Load(), subTasks.Load(), failTasks.Load()) |
|
|
|
@ -396,7 +396,7 @@ func (svc *ExchangeService) initialTradeInstanceKlines(exchange *Exchange, trade |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (svc *ExchangeService) fetchTaskKlines(exchange *Exchange, task fetchKlineTask) (lastKlineTs int64, err error) { |
|
|
|
func (svc *ExchangeService) fetchTaskKlinesToTSDB(exchange *Exchange, task fetchKlineTask) (lastKlineTs int64, err error) { |
|
|
|
interval, afterTs, beforeTs := task.interval, task.afterTs, task.beforeTs |
|
|
|
interval, afterTs, beforeTs := task.interval, task.afterTs, task.beforeTs |
|
|
|
klines, err := exchange.Fetcher.FetchHistoryKlines(context.Background(), task.inst.ExchangeInstId, interval, afterTs, beforeTs) |
|
|
|
klines, err := exchange.Fetcher.FetchHistoryKlines(context.Background(), task.inst.ExchangeInstId, interval, afterTs, beforeTs) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
|