mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 00:05:15 +00:00
grpc: fix user data stream subscribe
This commit is contained in:
parent
84c34f1d50
commit
8b8cffbd06
|
@ -25,9 +25,17 @@ type UserDataService struct {
|
|||
}
|
||||
|
||||
func (s *UserDataService) Subscribe(request *pb.UserDataRequest, server pb.UserDataService_SubscribeServer) error {
|
||||
streamPool := map[string]types.Stream{}
|
||||
for sessionName, session := range s.Environ.Sessions() {
|
||||
if request.Session == sessionName {
|
||||
sessionName := request.Session
|
||||
|
||||
if len(sessionName) == 0 {
|
||||
return fmt.Errorf("session name can not be empty")
|
||||
}
|
||||
|
||||
session, ok := s.Environ.Session(sessionName)
|
||||
if !ok {
|
||||
return fmt.Errorf("session %s not found", sessionName)
|
||||
}
|
||||
|
||||
userDataStream := session.Exchange.NewStream()
|
||||
userDataStream.OnOrderUpdate(func(order types.Order) {
|
||||
err := server.Send(&pb.UserData{
|
||||
|
@ -49,7 +57,8 @@ func (s *UserDataService) Subscribe(request *pb.UserDataRequest, server pb.UserD
|
|||
log.WithError(err).Errorf("grpc: can not send user data")
|
||||
}
|
||||
})
|
||||
userDataStream.OnBalanceUpdate(func(balances types.BalanceMap) {
|
||||
|
||||
balanceHandler := func(balances types.BalanceMap) {
|
||||
err := server.Send(&pb.UserData{
|
||||
Channel: pb.Channel_BALANCE,
|
||||
Event: pb.Event_UPDATE,
|
||||
|
@ -58,21 +67,17 @@ func (s *UserDataService) Subscribe(request *pb.UserDataRequest, server pb.UserD
|
|||
if err != nil {
|
||||
log.WithError(err).Errorf("grpc: can not send user data")
|
||||
}
|
||||
})
|
||||
streamPool[ sessionName ] = userDataStream
|
||||
}
|
||||
}
|
||||
|
||||
<-server.Context().Done()
|
||||
userDataStream.OnBalanceUpdate(balanceHandler)
|
||||
userDataStream.OnBalanceSnapshot(balanceHandler)
|
||||
|
||||
defer func() {
|
||||
for _, stream := range streamPool {
|
||||
if err := stream.Close(); err != nil {
|
||||
if err := userDataStream.Close(); err != nil {
|
||||
log.WithError(err).Errorf("user data stream close error")
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
<-server.Context().Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user