Skip to content

Commit

Permalink
Fixed Bot Handler stop logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mymmrac committed Nov 27, 2023
1 parent ea72c2d commit 9f7edd9
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions telegohandler/bot_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (h *BotHandler) Start() {
return
case update, ok := <-h.updates:
if !ok {
h.Stop()
go h.Stop()
return
}

Expand Down Expand Up @@ -123,8 +123,8 @@ func (h *BotHandler) IsRunning() bool {
return h.running
}

// Stop stops handling of updates, will block until all updates has been processes or on timeout. If timeout set to 0,
// bot handler will not wait for all handlers to complete processing.
// Stop stops handling of updates, will block until all updates has been processes or on timeout.
// If timeout set to 0 (default), bot handler will stop immediately.
// Note: Calling [BotHandler.Stop] method multiple times does nothing. Calling before [BotHandler.Start] method does
// nothing.
func (h *BotHandler) Stop() {
Expand All @@ -136,15 +136,24 @@ func (h *BotHandler) Stop() {

close(h.stop)

if h.stopTimeout <= 0 {
h.running = false
return
}

wait := make(chan struct{})
go func() {
h.handledUpdates.Wait()
wait <- struct{}{}
close(wait)
}()

waiter := time.NewTimer(h.stopTimeout)
select {
case <-time.After(h.stopTimeout):
case <-waiter.C:
// Wait for timeout
case <-wait:
// Wait for handler to complete
waiter.Stop()
}

h.running = false
Expand Down

0 comments on commit 9f7edd9

Please sign in to comment.