diff --git a/v1/core/pollers.go b/v1/core/pollers.go index 1fdd5c1..82955c1 100644 --- a/v1/core/pollers.go +++ b/v1/core/pollers.go @@ -29,6 +29,7 @@ type LongPollerOption interface { type Webhook struct { srv *http.Server ch chan model.Update + wg *sync.WaitGroup } type LongPoller struct { @@ -99,38 +100,36 @@ func NewWebhookPoller(api *maxbot.Api, secret string, options ...WebhookOption) return wh } -func (w *Webhook) Start(_ context.Context) (<-chan model.Update, error) { - errCh := make(chan error, 1) - defer close(errCh) +func (w *Webhook) Start(ctx context.Context) (<-chan model.Update, error) { + l, err := net.Listen("tcp", w.srv.Addr) + if err != nil { + return nil, err + } go func() { var err error - defer func() { - errCh <- err - if err != nil && !errors.Is(err, http.ErrServerClosed) { - log.Fatal().Err(err).Msg("webhook server error") - } - }() if w.srv.TLSConfig == nil { - err = w.srv.ListenAndServe() - return + err = w.srv.Serve(l) + } else { + err = w.srv.ServeTLS(l, "", "") } - err = w.srv.ListenAndServeTLS("", "") + if err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Ctx(ctx).Error().Err(err).Msg("failed to start webhook server") + } }() - select { - case err := <-errCh: - return nil, err - case <-time.After(defaultWhStartTimeout): - return w.ch, nil - } + return w.ch, nil } func (w *Webhook) Stop(ctx context.Context) error { - close(w.ch) - return w.srv.Shutdown(ctx) + defer close(w.ch) + + if err := w.srv.Shutdown(ctx); err != nil { + return err + } + return nil } func NewLongPoller(api *maxbot.Api, options ...LongPollerOption) *LongPoller {