mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-03 11:53:43 +02:00
internal/server/server.go: Make context timeout configurable.
This commit is contained in:
parent
6030981ba5
commit
d73187ec38
1 changed files with 15 additions and 3 deletions
|
|
@ -26,6 +26,9 @@ type Server struct {
|
||||||
|
|
||||||
// Time between each call to OnTick()
|
// Time between each call to OnTick()
|
||||||
tick_interval time.Duration
|
tick_interval time.Duration
|
||||||
|
|
||||||
|
// API Check timeout
|
||||||
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*Server)
|
type Option func(*Server)
|
||||||
|
|
@ -48,6 +51,12 @@ func WithTick(interval time.Duration) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithTimeout(duration time.Duration) Option {
|
||||||
|
return func(s *Server) {
|
||||||
|
s.timeout = duration
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// OnBoot callback function
|
// OnBoot callback function
|
||||||
//
|
//
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
@ -121,9 +130,12 @@ func (s *Server) OnTraffic(c gnet.Conn) gnet.Action {
|
||||||
// gnet library does not like blocking calls.
|
// gnet library does not like blocking calls.
|
||||||
// as we do a blocking http call here, we need to wrap it in a goroutine.
|
// as we do a blocking http call here, we need to wrap it in a goroutine.
|
||||||
go func() {
|
go func() {
|
||||||
// Make a context with 30 sec timeout per default. Should be "enough" for most cases.
|
ctx := context.Background()
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
if s.timeout > 0 {
|
||||||
defer cancel()
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, s.timeout)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
status, msg := healthCheckApi.Call(ctx)
|
status, msg := healthCheckApi.Call(ctx)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue