mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-06-16 04:44:55 +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()
|
||||
tick_interval time.Duration
|
||||
|
||||
// API Check timeout
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
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
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
|
@ -121,9 +130,12 @@ func (s *Server) OnTraffic(c gnet.Conn) gnet.Action {
|
|||
// gnet library does not like blocking calls.
|
||||
// as we do a blocking http call here, we need to wrap it in a goroutine.
|
||||
go func() {
|
||||
// Make a context with 30 sec timeout per default. Should be "enough" for most cases.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
||||
defer cancel()
|
||||
ctx := context.Background()
|
||||
if s.timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
t := time.Now()
|
||||
status, msg := healthCheckApi.Call(ctx)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue