Skip to content

Commit 2393479

Browse files
committed
http:1.api优化响应器性能;constants:1.error和code使用sync.Map替换mutex;other:新增benchmark脚本
1 parent 98c3bad commit 2393479

File tree

15 files changed

+582
-316
lines changed

15 files changed

+582
-316
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ config.*.toml
44
!config.sample.toml
55
examples/logs/**
66
coverage.txt
7+
go-sail.test

README.md

Lines changed: 133 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
<div align="center">
22
<h1><img src="static/sailboat-solid-colorful.svg" alt="sailboat-solid" title="sailboat-solid" width="300" /></h1>
3-
</div>
3+
</div>
44

55
[![Go](https://github.com/keepchen/go-sail/actions/workflows/go.yml/badge.svg)](https://github.com/keepchen/go-sail/actions/workflows/go.yml)
66
[![CodeQL](https://github.com/keepchen/go-sail/actions/workflows/codeql.yml/badge.svg)](https://github.com/keepchen/go-sail/actions/workflows/codeql.yml)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/keepchen/go-sail/v3)](https://goreportcard.com/report/github.com/keepchen/go-sail/v3)
88
[![codecov](https://codecov.io/github/keepchen/go-sail/graph/badge.svg?token=UNLOORRJHA)](https://codecov.io/github/keepchen/go-sail)
99
[![Dependabot Status](https://img.shields.io/badge/dependabot-active-brightgreen?logo=dependabot)](https://github.com/keepchen/go-sail/security/dependabot)
1010
[![Snyk Security](https://img.shields.io/badge/Snyk-Secure-blueviolet?logo=snyk)](https://snyk.io/test/github/keepchen/go-sail)
11-
[![LICENSE: MIT](https://img.shields.io/github/license/keepchen/go-sail.svg?style=flat)](LICENSE)
11+
[![LICENSE: MIT](https://img.shields.io/github/license/keepchen/go-sail.svg?style=flat)](LICENSE)
1212

1313
简体中文 | [English](./README_EN.md)
1414

15-
## go-sail是什么?
15+
## go-sail是什么?
1616

17-
**go-sail**是一个轻量的渐进式Web框架,使用Go语言实现。它并**不是重复造轮子的产物**,而是站在巨人的肩膀上,整合现有的优秀组件,旨在帮助使用者以最简单的方式构建稳定可靠的服务。
18-
正如它的名字一般,你可以把它视作自己在golang生态的一个开始。go-sail将助力你从轻出发,扬帆起航。
17+
**go-sail**是一个轻量的渐进式Web框架,使用Go语言实现。它并**不是重复造轮子的产物**,而是站在巨人的肩膀上,整合现有的优秀组件,旨在帮助使用者以最简单的方式构建稳定可靠的服务。
18+
正如它的名字一般,你可以把它视作自己在golang生态的一个开始。go-sail将助力你从轻出发,扬帆起航。
1919

20-
## 如何使用
21-
> 推荐go version >= 1.20
20+
## 如何使用
21+
> 推荐go version >= 1.20
2222
2323
> go get -u github.com/keepchen/go-sail/v3
2424
25-
```go
25+
```go
2626
import (
2727
"net/http"
2828
"github.com/gin-gonic/gin"
@@ -42,18 +42,18 @@ var (
4242
func main() {
4343
sail.WakeupHttp("go-sail", conf).Hook(registerRoutes, nil, nil).Launch()
4444
}
45-
```
46-
当你看到终端如下图所示内容就表示服务启动成功了:
45+
```
46+
当你看到终端如下图所示内容就表示服务启动成功了:
4747

48-
<img src="static/launch.png" alt="launch.png" title="launch.png" width="600" />
48+
<img src="static/launch.png" alt="launch.png" title="launch.png" width="600" />
4949

50-
## 示例
51-
### 配置读取
50+
## 示例
51+
### 配置读取
5252
```go
5353
parseFn := func(content []byte, viaWatch bool){
5454
fmt.Println("config content: ", string(content))
5555
if viaWatch {
56-
//reload config...
56+
//reload config...
5757
}
5858
}
5959
etcdConf := etcd.Conf{
@@ -65,16 +65,16 @@ key := "go-sail.config.yaml"
6565

6666
sail.Config(true, parseFn).ViaEtcd(etcdConf, key).Parse(parseFn)
6767
```
68-
### 链路日志追踪
68+
### 链路日志追踪
6969
```go
7070
func UserRegisterSvc(c *gin.Context) {
7171
...
7272
sail.LogTrace(c).GetLogger().Warn("log something...")
7373
...
7474
}
75-
```
76-
### JWT认证
77-
- 颁发令牌
75+
```
76+
### JWT认证
77+
- 颁发令牌
7878
```go
7979
func UserLoginSvc(c *gin.Context) {
8080
...
@@ -85,28 +85,28 @@ func UserLoginSvc(c *gin.Context) {
8585
"avatar": "https://go-sail.dev/assets/avatar/1.png",
8686
...
8787
}
88-
ok, claims, err := sail.JWT().MakeToken(uid, exp, otherFields)
88+
ok, token, err := sail.JWT().MakeToken(uid, exp, otherFields)
8989
...
9090
}
91-
```
92-
- 认证
91+
```
92+
- 认证
9393
```go
9494
func UserInfoSvc(c *gin.Context) {
9595
...
9696
ok, claims, err := sail.JWT().ValidToken(token)
9797
...
9898
}
99-
```
100-
### 组件
101-
#### 响应器
99+
```
100+
### 组件
101+
#### 响应器
102102
```go
103103
func UserInfoSvc(c *gin.Context) {
104104
sail.Response(c).Wrap(constants.ErrNone, resp).Send()
105105
}
106-
```
106+
```
107107

108-
#### 数据库
109-
- 读写分离
108+
#### 数据库
109+
- 读写分离
110110
```go
111111
func UserInfoSvc(c *gin.Context) {
112112
uid := "user-1000"
@@ -118,11 +118,11 @@ func UserInfoSvc(c *gin.Context) {
118118
sail.GetDBW().Model(&models.User{}).
119119
Where("uid = ?", uid).
120120
Updates(map[string]interface{}{
121-
"avatar": "https://go-sail.dev/assets/avatar/2.png"
121+
"avatar": "https://go-sail.dev/assets/avatar/2.png"
122122
})
123123
}
124-
```
125-
- 事务
124+
```
125+
- 事务
126126
```go
127127
func UserInfoSvc(c *gin.Context) {
128128
uid := "user-1000"
@@ -142,38 +142,38 @@ func UserInfoSvc(c *gin.Context) {
142142
return e2
143143
})
144144
}
145-
```
146-
#### Redis
145+
```
146+
#### Redis
147147
```go
148148
func UserInfoSvc(c *gin.Context) {
149149
...
150150
sail.GetRedis().Set(ctx, "go-sail:userInfo", "user-1000", time.Hour*24).Result()
151151
...
152152
}
153-
```
154-
### 计划任务
155-
- 周期性的
153+
```
154+
### 计划任务
155+
- 周期性的
156156
```go
157157
func TodoSomething() {
158158
fn := func() { ... }
159159
sail.Schedule("todoSomething", fn).Daily()
160160
}
161-
```
162-
- Linux Crontab风格的
161+
```
162+
- Linux Crontab风格的
163163
```go
164164
func TodoSomething() {
165165
fn := func() { ... }
166166
sail.Schedule("todoSomething", fn).RunAt("*/5 * * * *")
167167
}
168-
```
169-
- 竞态检测
168+
```
169+
- 竞态检测
170170
```go
171171
func TodoSomething() {
172172
fn := func() { ... }
173173
sail.Schedule("todoSomething", fn).Withoutoverlapping().RunAt("*/5 * * * *")
174174
}
175-
```
176-
### 分布式锁
175+
```
176+
### 分布式锁
177177
```go
178178
func UpdateUserBalance() {
179179
if !sail.RedisLocker().TryLock(key) {
@@ -182,87 +182,105 @@ func UpdateUserBalance() {
182182
defer sail.RedisLocker().Unlock(key)
183183
...
184184
}
185-
```
185+
```
186186

187-
## 文档
187+
## 文档
188188
[文档传送门](https://go-sail.dev)
189189

190-
## 功能特性
191-
- [x] HTTP响应器
192-
- 统一响应字段
193-
- 管理HTTP状态码
194-
- 管理业务码
195-
- [x] 组件库
196-
- Database
197-
- Email
198-
- Jwt
199-
- Kafka
200-
- Logger
201-
- Nacos
202-
- Etcd
203-
- Nats
204-
- Redis
205-
- Valkey
206-
- [x] 服务注册与发现
207-
- Nacos
208-
- Etcd
209-
- [x] 工具类
210-
- 加解密
211-
- 文件
212-
- ip
213-
- 字符串
214-
- 随机数
215-
- 日期时间
190+
## 功能特性
191+
- [x] HTTP响应器
192+
- 统一响应字段
193+
- 管理HTTP状态码
194+
- 管理业务码
195+
- [x] 组件库
196+
- Database
197+
- Email
198+
- Jwt
199+
- Kafka
200+
- Logger
201+
- Nacos
202+
- Etcd
203+
- Nats
204+
- Redis
205+
- Valkey
206+
- [x] 服务注册与发现
207+
- Nacos
208+
- Etcd
209+
- [x] 工具类
210+
- 加解密
211+
- 文件
212+
- ip
213+
- 字符串
214+
- 随机数
215+
- 日期时间
216216
- ...
217-
- [x] 日志收集与导出
218-
- 本地文件
219-
- 导出器
220-
- Redis
221-
- Kafka
222-
- Nats
223-
- [x] 计划任务
224-
- 可取消的
225-
- 一次性的
226-
- 周期性的
227-
- Linux Crontab风格的
228-
- 竞态检测
229-
- [x] 遥测与可观测性
217+
- [x] 日志收集与导出
218+
- 本地文件
219+
- 导出器
220+
- Redis
221+
- Kafka
222+
- Nats
223+
- [x] 计划任务
224+
- 可取消的
225+
- 一次性的
226+
- 周期性的
227+
- Linux Crontab风格的
228+
- 竞态检测
229+
- [x] 遥测与可观测性
230230
- 调用链追踪
231-
- Prometheus
232-
- Pprof
233-
- 日志导出器
234-
- 性能检测
231+
- Prometheus
232+
- Pprof
233+
- 日志导出器
234+
- 性能检测
235235
- Prometheus
236236
- Pprof
237-
- [x] 接口错误码
238-
- 动态注入
239-
- 国际化
240-
- [x] 基于Redis的分布式锁
241-
- 阻塞式
242-
- 非阻塞式
243-
- [x] 接口文档
244-
- Redocly
245-
- Swagger
246-
- [x] 配置管理
247-
- File
248-
- Etcd
249-
- Nacos
237+
- [x] 接口错误码
238+
- 动态注入
239+
- 国际化
240+
- [x] 基于Redis的分布式锁
241+
- 阻塞式
242+
- 非阻塞式
243+
- [x] 接口文档
244+
- Redocly
245+
- Swagger
246+
- [x] 配置管理
247+
- File
248+
- Etcd
249+
- Nacos
250250

251-
#### 其他插件
252-
[README.md](plugins/README.md)
251+
#### 其他插件
252+
[README.md](plugins/README.md)
253253

254-
## 大感谢
255-
感谢在体验、使用过程中提出宝贵建议和意见以及提供过其他各种帮助的各位小伙伴!
256-
- 配置模块化优化建议 [@fujilin](https://github.com/fujilin)
257-
- 响应器语法糖增强优化建议 [@lichuanzhang](https://github.com/lichuanzhang)
258-
- Logo美化 [@ShuaiRen34](https://twitter.com/ShuaiRen34)
254+
## 基准测试
255+
```shell
256+
ulimit -n 65535 && sh run_benchmark.sh
257+
```
258+
测试结果(真实的HTTP请求)
259+
```text
260+
goos: darwin
261+
goarch: amd64
262+
pkg: github.com/keepchen/go-sail/v3
263+
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
264+
BenchmarkGoSailParallel-12 88252 12898 ns/op 8860 B/op 92 allocs/op
265+
BenchmarkGinParallel-12 96548 11722 ns/op 7187 B/op 82 allocs/op
266+
PASS
267+
ok github.com/keepchen/go-sail/v3 3.663s
268+
```
269+
![benchmark-result.png](static/benchmark-result.png)
259270

260-
## 其他
261-
- 欢迎大家提PR: [pull request](https://github.com/keepchen/go-sail/compare)
262-
- 欢迎大家提出自己的想法: [issue](https://github.com/keepchen/go-sail/issues/new/choose)
263-
- 感谢你的star如果你喜欢这个项目的话 :)
264271

265-
## 使用案例
272+
## 大感谢
273+
感谢在体验、使用过程中提出宝贵建议和意见以及提供过其他各种帮助的各位小伙伴!
274+
- 配置模块化优化建议 [@fujilin](https://github.com/fujilin)
275+
- 响应器语法糖增强优化建议 [@lichuanzhang](https://github.com/lichuanzhang)
276+
- Logo美化 [@ShuaiRen34](https://twitter.com/ShuaiRen34)
277+
278+
## 其他
279+
- 欢迎大家提PR: [pull request](https://github.com/keepchen/go-sail/compare)
280+
- 欢迎大家提出自己的想法: [issue](https://github.com/keepchen/go-sail/issues/new/choose)
281+
- 感谢你的star如果你喜欢这个项目的话 :)
282+
283+
## 使用案例
266284
<table style="text-align: center">
267285
<tr style="height:200px">
268286
<td style="border: 1px solid black; padding: 8px;">
@@ -286,11 +304,10 @@ func UpdateUserBalance() {
286304
<a href="https://fantagoal.io" target="_blank"><img src="static/usecases/fantaGoal-logo.png" alt="FantaGoal" width="200" title="https://fantagoal.io"/></a>
287305
</td>
288306
</tr>
289-
</table>
307+
</table>
290308

291-
## 赞助
292-
[![Powered by DartNode](https://dartnode.com/branding/DN-Open-Source-sm.png)](https://dartnode.com "Powered by DartNode - Free VPS for Open Source")
309+
## 赞助
310+
[![Powered by DartNode](https://dartnode.com/branding/DN-Open-Source-sm.png)](https://dartnode.com "Powered by DartNode - Free VPS for Open Source")
293311

294-
## Star历史
312+
## Star历史
295313
![star history](https://starchart.cc/keepchen/go-sail.svg)
296-

0 commit comments

Comments
 (0)