Skip to content

Commit

Permalink
redis使用连接pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 16, 2023
1 parent 41323b0 commit 92361b0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
10 changes: 8 additions & 2 deletions cmd/haobase/orders/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
}
}
2 changes: 1 addition & 1 deletion cmd/haobase/orders/limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions cmd/haobase/orders/market_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/haobase/www/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
15 changes: 15 additions & 0 deletions utils/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down

0 comments on commit 92361b0

Please sign in to comment.