Skip to content

Commit

Permalink
refactor(health): 采用 min/max 代替自有的实现
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Mar 6, 2024
1 parent ba15943 commit 5c5ed49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
6 changes: 6 additions & 0 deletions acl/acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-FileCopyrightText: 2024 caixw
//
// SPDX-License-Identifier: MIT

// Package acl 权限控制
package acl
22 changes: 6 additions & 16 deletions health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT

// Package health API 状态检测
// Package health API 状态统计
package health

import (
Expand Down Expand Up @@ -54,9 +54,7 @@ func newState(route, method, path string) *State {
}

// New 声明 [Health] 实例
func New(store Store) *Health {
return &Health{Enabled: true, store: store}
}
func New(store Store) *Health { return &Health{Enabled: true, store: store} }

// Register 注册一条路由项
//
Expand Down Expand Up @@ -97,14 +95,9 @@ func (h *Health) States() []*State { return h.store.All() }
// Middleware 将当前中间件应用于 next
func (h *Health) Middleware(next web.HandlerFunc) web.HandlerFunc {
return func(ctx *web.Context) web.Responser {
if !h.Enabled {
return next(ctx)
if h.Enabled {
ctx.OnExit(func(c *web.Context, status int) { h.save(c, status) })
}

ctx.OnExit(func(c *web.Context, status int) {
h.save(c, status)
})

return next(ctx)
}
}
Expand All @@ -128,11 +121,8 @@ func (h *Health) save(ctx *web.Context, status int) {
state.Min = dur
state.Max = dur
} else {
if state.Min > dur {
state.Min = dur
} else if state.Max < dur {
state.Max = dur
}
state.Min = min(state.Min, dur)
state.Max = max(state.Max, dur)
}

h.store.Save(state)
Expand Down

0 comments on commit 5c5ed49

Please sign in to comment.