diff --git a/internal/server/server.go b/internal/server/server.go index 8cb1490..a9a8208 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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)