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
+19 -20
+19 -20
View File
@@ -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 {