Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
keepchen committed Jan 26, 2024
1 parent 2c389fe commit 5fc0a73
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
4 changes: 2 additions & 2 deletions sail/httpserver/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// RunPrometheusServerOnDebugMode 启动prometheus指标收集服务
// RunPrometheusServerWhenEnable 启动prometheus指标收集服务
//
// 当配置文件指明启用时才会启动
func RunPrometheusServerOnDebugMode(conf config.PrometheusConf) {
func RunPrometheusServerWhenEnable(conf config.PrometheusConf) {
if !conf.Enable {
return
}
Expand Down
4 changes: 2 additions & 2 deletions sail/httpserver/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
ginSwagger "github.com/swaggo/gin-swagger"
)

// RunSwaggerServerOnDebugMode 启动swagger文档服务
// RunSwaggerServerWhenEnable 启动swagger文档服务
//
// 当配置文件指明启用时才会启动
func RunSwaggerServerOnDebugMode(conf config.SwaggerConf, ginEngine *gin.Engine) {
func RunSwaggerServerWhenEnable(conf config.SwaggerConf, ginEngine *gin.Engine) {
if !conf.Enable {
//如果不是调试模式就不注册swagger路由
return
Expand Down
8 changes: 4 additions & 4 deletions sail/sail.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func (f *Framework) Launch(registerRoutes func(ginEngine *gin.Engine)) {
httpserver.EnablePProfOnDebugMode(f.conf.HttpServer, ginEngine)

//- prometheus
httpserver.RunPrometheusServerOnDebugMode(f.conf.HttpServer.Prometheus)
httpserver.RunPrometheusServerWhenEnable(f.conf.HttpServer.Prometheus)

//- swagger
httpserver.RunSwaggerServerOnDebugMode(f.conf.HttpServer.Swagger, ginEngine)
httpserver.RunSwaggerServerWhenEnable(f.conf.HttpServer.Swagger, ginEngine)

//- http server
wg.Add(1)
Expand Down Expand Up @@ -239,10 +239,10 @@ func (l *Launcher) Launch() {
httpserver.EnablePProfOnDebugMode(l.fw.conf.HttpServer, ginEngine)

//- prometheus
httpserver.RunPrometheusServerOnDebugMode(l.fw.conf.HttpServer.Prometheus)
httpserver.RunPrometheusServerWhenEnable(l.fw.conf.HttpServer.Prometheus)

//- swagger
httpserver.RunSwaggerServerOnDebugMode(l.fw.conf.HttpServer.Swagger, ginEngine)
httpserver.RunSwaggerServerWhenEnable(l.fw.conf.HttpServer.Swagger, ginEngine)

//- http server
wg.Add(1)
Expand Down
52 changes: 29 additions & 23 deletions utils/redislock.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func RedisLock(key string) (cancel CancelFunc) {
locked = false
ticker = time.NewTicker(time.Millisecond)
cancelChan = make(chan struct{})
cancelFunc = func() {
cancelChan <- struct{}{}
close(cancelChan)
}
)
cancel = func() {
cancelChan <- struct{}{}
close(cancelChan)
}

LOOP:
for {
Expand All @@ -83,7 +83,7 @@ LOOP:

ticker.Stop()

return cancelFunc
return
}

// RedisUnlock redis锁-解锁(自动推测连接类型)
Expand Down Expand Up @@ -122,9 +122,7 @@ func RedisStandaloneLock(key string) bool {

if ok {
cancelChan := make(chan struct{})
states.mux.Lock()
states.listeners[key] = cancelChan
states.mux.Unlock()

//自动续期
go func() {
ticker := time.NewTicker(renewalCheckInterval)
Expand All @@ -135,16 +133,18 @@ func RedisStandaloneLock(key string) bool {
for {
select {
case <-ticker.C:
_, redisErr := redis.GetInstance().Get(innerCtx, key).Result()
if redisErr != nil {
if ok, redisErr := redis.GetInstance().Expire(innerCtx, key, lockTTL).Result(); !ok || redisErr != nil {
break LOOP
}
_, _ = redis.GetInstance().Expire(innerCtx, key, lockTTL).Result()
case <-cancelChan:
break LOOP
}
}
}()

states.mux.Lock()
states.listeners[key] = cancelChan
states.mux.Unlock()
}

return ok
Expand All @@ -166,12 +166,15 @@ func RedisStandaloneUnlock(key string) {

go func() {
states.mux.Lock()
if ch, ok := states.listeners[key]; ok {
ch <- struct{}{}
close(ch)
ch, ok := states.listeners[key]
if ok {
delete(states.listeners, key)
}
states.mux.Unlock()
if ok {
ch <- struct{}{}
close(ch)
}
}()
}

Expand All @@ -194,9 +197,7 @@ func RedisClusterLock(key string) bool {

if ok {
cancelChan := make(chan struct{})
states.mux.Lock()
states.listeners[key] = cancelChan
states.mux.Unlock()

//自动续期
go func() {
ticker := time.NewTicker(renewalCheckInterval)
Expand All @@ -207,16 +208,18 @@ func RedisClusterLock(key string) bool {
for {
select {
case <-ticker.C:
_, redisErr := redis.GetClusterInstance().Get(innerCtx, key).Result()
if redisErr != nil {
if ok, redisErr := redis.GetClusterInstance().Expire(innerCtx, key, lockTTL).Result(); !ok || redisErr != nil {
break LOOP
}
_, _ = redis.GetClusterInstance().Expire(innerCtx, key, lockTTL).Result()
case <-cancelChan:
break LOOP
}
}
}()

states.mux.Lock()
states.listeners[key] = cancelChan
states.mux.Unlock()
}

return ok
Expand All @@ -238,11 +241,14 @@ func RedisClusterUnlock(key string) {

go func() {
states.mux.Lock()
if ch, ok := states.listeners[key]; ok {
ch <- struct{}{}
close(ch)
ch, ok := states.listeners[key]
if ok {
delete(states.listeners, key)
}
states.mux.Unlock()
if ok {
ch <- struct{}{}
close(ch)
}
}()
}

0 comments on commit 5fc0a73

Please sign in to comment.