From 81414eea9c14562ce84489adac26ddb6b4c43b77 Mon Sep 17 00:00:00 2001 From: zhuyasen Date: Mon, 30 Sep 2024 21:11:20 +0800 Subject: [PATCH] optimize code --- README.md | 2 ++ assets/readme-cn.md | 2 ++ .../internal/generate/handler/template.go | 3 ++- cmd/sponge/commands/generate/template.go | 11 +++++----- internal/model/init.go | 11 +++++----- pkg/krand/krand.go | 22 +++++++++++++++---- 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index bbbdfd7..4d1c04b 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,8 @@ Detailed step-by-step, configuration, deployment instructions for developing pro #### Distributed transaction examples - [Simple distributed order system](https://github.com/zhufuyi/sponge_examples/tree/main/9_order-grpc-distributed-transaction) +- [Flash sale](https://github.com/zhufuyi/sponge_examples/tree/main/_12_sponge-dtm-flashSale) +- [E-Commerce system](https://github.com/zhufuyi/sponge_examples/tree/main/_14_eshop)

diff --git a/assets/readme-cn.md b/assets/readme-cn.md index 33232a7..7b42146 100644 --- a/assets/readme-cn.md +++ b/assets/readme-cn.md @@ -186,6 +186,8 @@ sponge run #### 分布式事务示例 - [简单的分布式订单系统](https://github.com/zhufuyi/sponge_examples/tree/main/9_order-grpc-distributed-transaction) +- [秒杀抢购活动](https://github.com/zhufuyi/sponge_examples/tree/main/_12_sponge-dtm-flashSale) +- [电商系统](https://github.com/zhufuyi/sponge_examples/tree/main/_14_eshop)

diff --git a/cmd/protoc-gen-go-gin/internal/generate/handler/template.go b/cmd/protoc-gen-go-gin/internal/generate/handler/template.go index 59e8b72..d813b3f 100644 --- a/cmd/protoc-gen-go-gin/internal/generate/handler/template.go +++ b/cmd/protoc-gen-go-gin/internal/generate/handler/template.go @@ -266,7 +266,8 @@ func {{.LowerName}}Router( iService {{.ProtoPkgName}}.{{.Name}}Logicer) { ctxFn := func(c *gin.Context) context.Context { md := metadata.New(map[string]string{ - middleware.ContextRequestIDKey: middleware.GCtxRequestID(c), + middleware.ContextRequestIDKey: middleware.GCtxRequestID(c), // request_id + //middleware.HeaderAuthorizationKey: c.GetHeader(middleware.HeaderAuthorizationKey), // authorization }) return metadata.NewIncomingContext(c.Request.Context(), md) } diff --git a/cmd/sponge/commands/generate/template.go b/cmd/sponge/commands/generate/template.go index dd5f045..2b81b96 100644 --- a/cmd/sponge/commands/generate/template.go +++ b/cmd/sponge/commands/generate/template.go @@ -573,12 +573,11 @@ func InitMysql() { opts = append(opts, ggorm.WithEnableTrace()) } - // setting mysql slave and master dsn addresses, - // if there is no read/write separation, you can comment out the following piece of code - opts = append(opts, ggorm.WithRWSeparation( - config.Get().Database.Mysql.SlavesDsn, - config.Get().Database.Mysql.MastersDsn..., - )) + // setting mysql slave and master dsn addresses + //opts = append(opts, ggorm.WithRWSeparation( + // config.Get().Database.Mysql.SlavesDsn, + // config.Get().Database.Mysql.MastersDsn..., + //)) // add custom gorm plugin //opts = append(opts, ggorm.WithGormPlugin(yourPlugin)) diff --git a/internal/model/init.go b/internal/model/init.go index 47032ad..ec4bfe1 100644 --- a/internal/model/init.go +++ b/internal/model/init.go @@ -145,12 +145,11 @@ func InitMysql() { opts = append(opts, ggorm.WithEnableTrace()) } - // setting mysql slave and master dsn addresses, - // if there is no read/write separation, you can comment out the following piece of code - opts = append(opts, ggorm.WithRWSeparation( - config.Get().Database.Mysql.SlavesDsn, - config.Get().Database.Mysql.MastersDsn..., - )) + // setting mysql slave and master dsn addresses + //opts = append(opts, ggorm.WithRWSeparation( + // config.Get().Database.Mysql.SlavesDsn, + // config.Get().Database.Mysql.MastersDsn..., + //)) // add custom gorm plugin //opts = append(opts, ggorm.WithGormPlugin(yourPlugin)) diff --git a/pkg/krand/krand.go b/pkg/krand/krand.go index f72d4dd..4eeb987 100644 --- a/pkg/krand/krand.go +++ b/pkg/krand/krand.go @@ -106,12 +106,26 @@ func NewStringID() string { return strconv.FormatInt(NewID(), 16) } -var datetimeUsLayout = "20060102150405.000000" - // NewSeriesID Generate a datetime+random string ID, // datetime is microsecond precision, 20 bytes, random is 6 bytes, total 26 bytes. // example: 20060102150405000000123456 func NewSeriesID() string { - dt := time.Now().Format(datetimeUsLayout) - return dt[:14] + dt[15:] + String(R_NUM) + // Declare a buffer, only 26 bytes are needed + var buf [27]byte + t := time.Now() + + // Format datetime with microsecond precision, and store in the buffer + copy(buf[:], t.Format("20060102150405.000000")) + + // Generate a 6-digit random number and append it to the buffer + random := rand.Intn(1000000) + buf[21] = '0' + byte(random/100000%10) + buf[22] = '0' + byte(random/10000%10) + buf[23] = '0' + byte(random/1000%10) + buf[24] = '0' + byte(random/100%10) + buf[25] = '0' + byte(random/10%10) + buf[26] = '0' + byte(random%10) + + // Return the final string without the dot + return string(buf[:14]) + string(buf[15:]) }