Skip to content

Commit

Permalink
Package Status:
Browse files Browse the repository at this point in the history
- add strictly health function to check status with OK only
- change healthy function to check status not KO only (so warn or OK)

Bump dependencies
  • Loading branch information
nabbar committed Jul 29, 2023
1 parent ec30365 commit aae7b9e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ require (
github.com/xanzy/go-gitlab v0.88.0
github.com/xhit/go-simple-mail v2.2.2+incompatible
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691
golang.org/x/net v0.12.0
golang.org/x/oauth2 v0.10.0
golang.org/x/sync v0.3.0
Expand Down Expand Up @@ -120,7 +120,7 @@ require (
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20230705174524-200ffdc848b8 // indirect
github.com/google/pprof v0.0.0-20230728192033-2ba5b33183c6 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down
26 changes: 19 additions & 7 deletions status/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ package status
import (
"sync/atomic"
"time"

monsts "github.com/nabbar/golib/monitor/status"
)

const timeCache = 3 * time.Second

type ch struct {
m *atomic.Int32
t *atomic.Value
c *atomic.Bool
f func() bool
c *atomic.Int64
f func() monsts.Status
}

func (o *ch) Max() time.Duration {
if m := o.m.Load(); m > 0 {
return time.Duration(m) * time.Second
} else {
return timeCache
}
}

func (o *ch) Time() time.Time {
Expand All @@ -47,20 +58,21 @@ func (o *ch) Time() time.Time {
}
}

func (o *ch) IsCache() bool {
if t := o.Time(); !t.IsZero() && time.Since(t) < timeCache {
return o.c.Load()
func (o *ch) IsCache() monsts.Status {
if t := o.Time(); !t.IsZero() && time.Since(t) < o.Max() {
r := o.c.Load()
return monsts.NewFromInt(r)
}

if o.f != nil {
c := o.f()
o.c.Store(c)
o.c.Store(c.Int())

t := time.Now()
o.t.Store(t)

return c
}

return false
return monsts.KO
}
14 changes: 11 additions & 3 deletions status/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"sync"
"sync/atomic"

monsts "github.com/nabbar/golib/monitor/status"

ginsdk "github.com/gin-gonic/gin"
libctx "github.com/nabbar/golib/context"
liberr "github.com/nabbar/golib/errors"
Expand Down Expand Up @@ -60,8 +62,12 @@ type Status interface {
Pool

SetConfig(cfg Config)

IsHealthy(name ...string) bool
IsStrictlyHealthy(name ...string) bool

IsCacheHealthy() bool
IsCacheStrictlyHealthy() bool
}

func New(ctx libctx.FuncContext) Status {
Expand All @@ -71,8 +77,9 @@ func New(ctx libctx.FuncContext) Status {
r: nil,
x: libctx.NewConfig[string](ctx),
c: ch{
m: new(atomic.Int32),
t: new(atomic.Value),
c: new(atomic.Bool),
c: new(atomic.Int64),
f: nil,
},
fn: nil,
Expand All @@ -81,8 +88,9 @@ func New(ctx libctx.FuncContext) Status {
fd: nil,
}

s.c.f = func() bool {
return s.IsHealthy()
s.c.f = func() monsts.Status {
r, _ := s.getStatus()
return r
}

return s
Expand Down
11 changes: 10 additions & 1 deletion status/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,19 @@ func (o *sts) checkFunc() bool {
}

func (o *sts) IsCacheHealthy() bool {
return o.c.IsCache()
return o.c.IsCache() >= monsts.Warn
}

func (o *sts) IsCacheStrictlyHealthy() bool {
return o.c.IsCache() == monsts.OK
}

func (o *sts) IsHealthy(name ...string) bool {
s, _ := o.getStatus(name...)
return s >= monsts.Warn
}

func (o *sts) IsStrictlyHealthy(name ...string) bool {
s, _ := o.getStatus(name...)
return s == monsts.OK
}
Expand Down

0 comments on commit aae7b9e

Please sign in to comment.