Skip to content

Commit

Permalink
add option WithNoAsyncHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
negasus committed Oct 17, 2024
1 parent a160547 commit 138b0c7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v1.9.0 (2024-10-17)

- IMPORTANT! New option `WithNotAsyncHandlers` allows to run handlers in the main goroutine. Now, by default, all handlers are run in separate goroutines.

## v1.8.4 (2024-10-10)

- add method GetBusinessConnection (#123)
Expand Down
1 change: 1 addition & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Bot struct {
webhookSecretToken string
testEnvironment bool
workers int
notAsyncHandlers bool

defaultHandlerFunc HandlerFunc

Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,10 @@ func UseTestEnvironment() Option {
b.testEnvironment = true
}
}

// WithNotAsyncHandlers allows to run handlers in the main goroutine
func WithNotAsyncHandlers() Option {
return func(b *Bot) {
b.notAsyncHandlers = true
}
}
13 changes: 8 additions & 5 deletions process_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ func applyMiddlewares(h HandlerFunc, m ...Middleware) HandlerFunc {

// ProcessUpdate allows you to process update
func (b *Bot) ProcessUpdate(ctx context.Context, upd *models.Update) {
h := b.defaultHandlerFunc
h := b.findHandler(upd)

defer func() {
applyMiddlewares(h, b.middlewares...)(ctx, b, upd)
}()
r := applyMiddlewares(h, b.middlewares...)

h = b.findHandler(upd)
if b.notAsyncHandlers {
r(ctx, b, upd)
return
}

go r(ctx, b, upd)
}

func (b *Bot) findHandler(upd *models.Update) HandlerFunc {
Expand Down
3 changes: 3 additions & 0 deletions process_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestProcessUpdate(t *testing.T) {
bot := &Bot{
defaultHandlerFunc: h,
middlewares: []Middleware{},
notAsyncHandlers: true,
}

ctx := context.Background()
Expand All @@ -63,6 +64,7 @@ func TestProcessUpdate_WithMiddlewares(t *testing.T) {
bot := &Bot{
defaultHandlerFunc: h,
middlewares: []Middleware{middleware},
notAsyncHandlers: true,
}

ctx := context.Background()
Expand All @@ -88,6 +90,7 @@ func TestProcessUpdate_WithMatchTypeFunc(t *testing.T) {

bot := &Bot{
defaultHandlerFunc: h1,
notAsyncHandlers: true,
}

bot.RegisterHandlerMatchFunc(m, h2)
Expand Down

0 comments on commit 138b0c7

Please sign in to comment.