fix: fixed webhook lifecycle

This commit is contained in:
abelith committed 2026-10-08 16:45:37 +03:00
1 parent 5f03d4d832
commit d7ef71dc7c
1 file changed
+18 -19
+18 -19
View File
@@ -29,6 +29,7 @@ type LongPollerOption interface {
type Webhook struct { type Webhook struct {
srv *http.Server srv *http.Server
ch chan model.Update ch chan model.Update
wg *sync.WaitGroup
} }
type LongPoller struct { type LongPoller struct {
@@ -99,38 +100,36 @@ func NewWebhookPoller(api *maxbot.Api, secret string, options ...WebhookOption)
return wh return wh
} }
func (w *Webhook) Start(_ context.Context) (<-chan model.Update, error) { func (w *Webhook) Start(ctx context.Context) (<-chan model.Update, error) {
errCh := make(chan error, 1) l, err := net.Listen("tcp", w.srv.Addr)
defer close(errCh) if err != nil {
return nil, err
}
go func() { go func() {
var err error 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 { if w.srv.TLSConfig == nil {
err = w.srv.ListenAndServe() err = w.srv.Serve(l)
return } 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 { func (w *Webhook) Stop(ctx context.Context) error {
close(w.ch) defer close(w.ch)
return w.srv.Shutdown(ctx)
if err := w.srv.Shutdown(ctx); err != nil {
return err
}
return nil
} }
func NewLongPoller(api *maxbot.Api, options ...LongPollerOption) *LongPoller { func NewLongPoller(api *maxbot.Api, options ...LongPollerOption) *LongPoller {