diff --git a/cmd/haobase/orders/func.go b/cmd/haobase/orders/func.go index e4c5b2f3..b8ca44a9 100644 --- a/cmd/haobase/orders/func.go +++ b/cmd/haobase/orders/func.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/sirupsen/logrus" "github.com/yzimhao/trading_engine/cmd/haobase/base" "github.com/yzimhao/trading_engine/trading_core" "github.com/yzimhao/trading_engine/types" @@ -28,7 +29,12 @@ func generate_order_id(prefix string) string { return fmt.Sprintf("%s%s%06d%02d", prefix, s, ns, rn) } -func push_order_to_redis(symbol string, data []byte) { +func push_new_order_to_redis(symbol string, data []byte) { + topic := types.FormatNewOrder.Format(symbol) + logrus.Infof("push %s new: %s", topic, data) ctx := context.Background() - base.RDC().RPush(ctx, types.FormatNewOrder.Format(symbol), data) + err := base.RDC().RPush(ctx, topic, data).Err() + if err != nil { + logrus.Errorf("push %s err: %s", topic, err.Error()) + } } diff --git a/cmd/haobase/orders/limit_order.go b/cmd/haobase/orders/limit_order.go index ae0edddc..5414d292 100644 --- a/cmd/haobase/orders/limit_order.go +++ b/cmd/haobase/orders/limit_order.go @@ -86,7 +86,7 @@ func limit_order(user_id string, symbol string, side trading_core.OrderSide, pri return nil, err } - push_order_to_redis(neworder.Symbol, func() []byte { + push_new_order_to_redis(neworder.Symbol, func() []byte { data := haotrader.Order{ OrderId: neworder.OrderId, OrderType: neworder.OrderType.String(), diff --git a/cmd/haobase/orders/market_order.go b/cmd/haobase/orders/market_order.go index 72881130..21838854 100644 --- a/cmd/haobase/orders/market_order.go +++ b/cmd/haobase/orders/market_order.go @@ -75,7 +75,7 @@ func market_order_qty(user_id string, symbol string, side trading_core.OrderSide return nil, err } - push_order_to_redis(neworder.Symbol, func() []byte { + push_new_order_to_redis(neworder.Symbol, func() []byte { data := haotrader.Order{ OrderId: neworder.OrderId, OrderType: neworder.OrderType.String(), @@ -160,7 +160,7 @@ func market_order_amount(user_id string, symbol string, side trading_core.OrderS return nil, err } - push_order_to_redis(neworder.Symbol, func() []byte { + push_new_order_to_redis(neworder.Symbol, func() []byte { data := haotrader.Order{ OrderId: neworder.OrderId, OrderType: neworder.OrderType.String(), diff --git a/cmd/haobase/www/order.go b/cmd/haobase/www/order.go index 027f6948..79abefda 100644 --- a/cmd/haobase/www/order.go +++ b/cmd/haobase/www/order.go @@ -28,7 +28,6 @@ func order_create(ctx *gin.Context) { var err error user_id := ctx.MustGet("user_id").(string) - if req.OrderType == trading_core.OrderTypeLimit { info, err = orders.NewLimitOrder(user_id, req.Symbol, req.Side, req.Price, req.Quantity) } else if req.OrderType == trading_core.OrderTypeMarket { @@ -38,6 +37,7 @@ func order_create(ctx *gin.Context) { info, err = orders.NewMarketOrderByQty(user_id, req.Symbol, req.Side, req.Quantity) } } + if err != nil { utils.ResponseFailJson(ctx, err.Error()) return diff --git a/go.mod b/go.mod index 826100e7..940f932d 100644 --- a/go.mod +++ b/go.mod @@ -56,7 +56,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect - github.com/redis/go-redis/v9 v9.1.0 // indirect + github.com/redis/go-redis/v9 v9.2.1 // indirect github.com/rs/cors v1.10.1 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sevlyar/go-daemon v0.1.6 // indirect diff --git a/go.sum b/go.sum index 4c011276..9b354103 100644 --- a/go.sum +++ b/go.sum @@ -373,6 +373,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/redis/go-redis/v9 v9.1.0 h1:137FnGdk+EQdCbye1FW+qOEcY5S+SpY9T0NiuqvtfMY= github.com/redis/go-redis/v9 v9.1.0/go.mod h1:urWj3He21Dj5k4TK1y59xH8Uj6ATueP8AH1cY3lZl4c= +github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg= +github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= diff --git a/utils/app/app.go b/utils/app/app.go index c45533d6..7a60bcaf 100644 --- a/utils/app/app.go +++ b/utils/app/app.go @@ -60,6 +60,21 @@ func RedisInit() *redis.Client { Addr: viper.GetString("redis.host"), Password: viper.GetString("redis.password"), DB: viper.GetInt("redis.db"), + + DialTimeout: 10 * time.Second, + ReadTimeout: 30 * time.Second, + WriteTimeout: 30 * time.Second, + ContextTimeoutEnabled: true, + + MaxRetries: -1, + + PoolTimeout: 30 * time.Second, + ConnMaxIdleTime: time.Minute, + + PoolSize: 15, + MinIdleConns: 10, + + ConnMaxLifetime: 0, }) }