1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-api-healthcheck synced 2026-07-04 12:03:43 +02:00

internal/server/server.go: Make context timeout configurable.

This commit is contained in:
Henrik Hautakoski 2023-02-07 09:21:10 +01:00
parent 6030981ba5
commit d73187ec38
No known key found for this signature in database
GPG key ID: 217490840C18A5D9

View file

@ -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 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, s.timeout)
defer cancel() defer cancel()
}
t := time.Now() t := time.Now()
status, msg := healthCheckApi.Call(ctx) status, msg := healthCheckApi.Call(ctx)