Skip to content

Commit 0166990

Browse files
committed
utils:1.简化并优化redis锁代码,2.支持传入自定义redis连接客户端;schedule:1.支持设定自定义redis连接客户端;http:响应器时间戳响应字段赋值位置调整,2.分页器json tag修正;other:更新README
1 parent f679b97 commit 0166990

File tree

12 files changed

+205
-256
lines changed

12 files changed

+205
-256
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func main() {
120120

121121
## 使用案例
122122
<table style="text-align: center">
123-
<tr>
123+
<tr style="height:200px">
124124
<td style="border: 1px solid black; padding: 8px;">
125125
<a href="https://stardots.io?ref=go-sail" target="_blank"><img src="static/usecases/stardots-logo.png" alt="stardots.io" width="200" title="https://stardots.io"/></a>
126126
</td>
@@ -131,7 +131,7 @@ func main() {
131131
<img src="static/usecases/miniprogram-hpp.png" alt="生活好评助手-小程序" width="200" />
132132
</td>
133133
</tr>
134-
<tr>
134+
<tr style="height:200px">
135135
<td style="border: 1px solid black; padding: 8px;">
136136
<img src="static/usecases/wingoal-metaland.png" alt="WinGoal" width="200" />
137137
</td>

README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Thank you to everyone who provided valuable suggestions and comments during the
123123

124124
## Use cases
125125
<table style="text-align: center">
126-
<tr>
126+
<tr style="height:200px">
127127
<td style="border: 1px solid black; padding: 8px;">
128128
<a href="https://stardots.io?ref=go-sail" target="_blank"><img src="static/usecases/stardots-logo.png" alt="stardots.io" width="200" title="https://stardots.io"/></a>
129129
</td>
@@ -134,7 +134,7 @@ Thank you to everyone who provided valuable suggestions and comments during the
134134
<img src="static/usecases/miniprogram-hpp.png" alt="生活好评助手-小程序" width="200" />
135135
</td>
136136
</tr>
137-
<tr>
137+
<tr style="height:200px">
138138
<td style="border: 1px solid black; padding: 8px;">
139139
<img src="static/usecases/wingoal-metaland.png" alt="WinGoal" width="200" />
140140
</td>

http/api/response.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,7 @@ func (a *responseEngine) mergeBody(code constants.ICodeType, resp interface{}, m
275275
body.Code = anotherErrNoneCode.Int()
276276
}
277277
body.Message = constants.CodeType(body.Code).String(language...)
278-
if loc != nil {
279-
//loc可能用于后续其他字段,这里暂时这样调用
280-
body.Timestamp = time.Now().In(loc).UnixMilli()
281-
} else {
282-
body.Timestamp = time.Now().UnixMilli()
283-
}
278+
284279
switch code {
285280
case anotherErrNoneCode:
286281
body.Success = constants.Success
@@ -354,6 +349,13 @@ func (a *responseEngine) mergeBody(code constants.ICodeType, resp interface{}, m
354349
}
355350
}
356351

352+
if loc != nil {
353+
//loc可能用于后续其他字段,这里暂时这样调用
354+
body.Timestamp = time.Now().In(loc).UnixMilli()
355+
} else {
356+
body.Timestamp = time.Now().UnixMilli()
357+
}
358+
357359
a.requestId = requestId
358360
a.data = body
359361

http/pojo/vo/paginate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package vo
22

33
// Paginate 分页器
44
type Paginate struct {
5-
Page int `json:"page" query:"page" form:"page" path:"page" format:"int" example:"1"` //页码,从1开始
6-
PageSize int `json:"pageSize" query:"pageSize" form:"pageSize" path:"pageSize" format:"int" example:"20"` //页面条数
5+
Page int `json:"page" query:"page" form:"page" path:"page" format:"number" example:"1"` //页码,从1开始
6+
PageSize int `json:"pageSize" query:"pageSize" form:"pageSize" path:"pageSize" format:"number" example:"20"` //页面条数
77
}

sail/schedule.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package sail
2+
3+
import (
4+
redisLib "github.com/go-redis/redis/v8"
5+
"github.com/keepchen/go-sail/v3/schedule"
6+
)
7+
8+
// SetScheduleRedisClient 设置计划任务使用的redis客户端
9+
func SetScheduleRedisClient(client redisLib.UniversalClient) {
10+
schedule.SetRedisClientOnce(client)
11+
}
12+
13+
// Schedule 计划任务
14+
func Schedule(name string, task func()) schedule.Scheduler {
15+
return schedule.NewJob(name, task)
16+
}

sail/schedule_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sail
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/keepchen/go-sail/v3/lib/redis"
8+
)
9+
10+
func TestSetScheduleRedisClient(t *testing.T) {
11+
t.Run("SetScheduleRedisClient", func(t *testing.T) {
12+
SetScheduleRedisClient(redis.GetInstance())
13+
SetScheduleRedisClient(redis.GetInstance())
14+
SetScheduleRedisClient(redis.GetClusterInstance())
15+
SetScheduleRedisClient(redis.GetClusterInstance())
16+
})
17+
}
18+
19+
func TestSchedule(t *testing.T) {
20+
t.Run("Schedule", func(t *testing.T) {
21+
t.Log(Schedule("test-sail-schedule", func() {
22+
fmt.Println("test-sail-schedule")
23+
}))
24+
})
25+
}

schedule/interval.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func (j *taskJob) run() {
3434
j.task()
3535
return
3636
}
37-
if utils.RedisLocker().TryLock(j.lockerKey) {
37+
if utils.RedisLocker(redisClients...).TryLock(j.lockerKey) {
3838
defer func() {
39-
utils.RedisLocker().Unlock(j.lockerKey)
39+
utils.RedisLocker(redisClients...).Unlock(j.lockerKey)
4040
j.lockedByMe = false
4141
}()
4242
j.lockedByMe = true
@@ -51,7 +51,7 @@ func (j *taskJob) run() {
5151
//收到退出信号,终止任务
5252
case <-j.cancelTaskChan:
5353
if j.withoutOverlapping && j.lockedByMe {
54-
utils.RedisLocker().Unlock(j.lockerKey)
54+
utils.RedisLocker(redisClients...).Unlock(j.lockerKey)
5555
}
5656

5757
taskSchedules.mux.Lock()

schedule/schedule.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"sync"
66
"time"
77

8+
redisLib "github.com/go-redis/redis/v8"
9+
810
"github.com/keepchen/go-sail/v3/utils"
911
"github.com/robfig/cron/v3"
1012
)
@@ -157,6 +159,18 @@ func generateJobNameKey(name string) string {
157159
return fmt.Sprintf("go-sail:task-schedule-locker:%s", utils.Base64().Encode([]byte(name)))
158160
}
159161

162+
var (
163+
setRedisOnce = &sync.Once{}
164+
redisClients = make([]redisLib.UniversalClient, 0)
165+
)
166+
167+
// SetRedisClientOnce 设置redis连接客户端
168+
func SetRedisClientOnce(client redisLib.UniversalClient) {
169+
setRedisOnce.Do(func() {
170+
redisClients = append(redisClients, client)
171+
})
172+
}
173+
160174
// NewJob 实例化任务
161175
//
162176
// name 任务名称唯一标识

schedule/schedule_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package schedule
22

33
import (
44
"fmt"
5+
"net"
56
"testing"
67
"time"
78

9+
"github.com/keepchen/go-sail/v3/lib/redis"
10+
811
"github.com/stretchr/testify/assert"
912
)
1013

@@ -92,3 +95,21 @@ func TestMustCall(t *testing.T) {
9295
time.Sleep(time.Second * 2)
9396
})
9497
}
98+
99+
func TestSetRedisClientOnce(t *testing.T) {
100+
t.Run("Run-SetRedisClientOnce", func(t *testing.T) {
101+
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", sConf.Host, sConf.Port))
102+
if err != nil {
103+
return
104+
}
105+
_ = conn.Close()
106+
redisClient, err := redis.New(sConf)
107+
108+
assert.NoError(t, err)
109+
110+
SetRedisClientOnce(redisClient)
111+
//multiple set
112+
SetRedisClientOnce(redisClient)
113+
SetRedisClientOnce(redisClient)
114+
})
115+
}

schedule/specs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ func (j *taskJob) RunAt(crontabExpr string) (cancel CancelFunc) {
4646
j.task()
4747
return
4848
}
49-
if utils.RedisLocker().TryLock(j.lockerKey) {
49+
if utils.RedisLocker(redisClients...).TryLock(j.lockerKey) {
5050
defer func() {
51-
utils.RedisLocker().Unlock(j.lockerKey)
51+
utils.RedisLocker(redisClients...).Unlock(j.lockerKey)
5252
j.lockedByMe = false
5353
}()
5454
j.lockedByMe = true

0 commit comments

Comments
 (0)