Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufuyi committed Sep 30, 2024
1 parent 0a77bab commit 81414ee
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<br>
<br>
Expand Down
2 changes: 2 additions & 0 deletions assets/readme-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<br>
<br>
Expand Down
3 changes: 2 additions & 1 deletion cmd/protoc-gen-go-gin/internal/generate/handler/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/sponge/commands/generate/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 5 additions & 6 deletions internal/model/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 18 additions & 4 deletions pkg/krand/krand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
}

0 comments on commit 81414ee

Please sign in to comment.