feat: replaced middleware implementations with generic ones, added logger field to dispatcher

This commit is contained in:
abelith committed 2026-10-09 10:38:20 +03:00
1 parent c4e06aeecd
commit 7bb08f60f3
2 files changed
+30 -32

No files matched your search

+16 -7
View File
@@ -10,7 +10,7 @@ import (
"github.com/google/uuid"
maxbot "github.com/max-messenger/max-bot-api-client-go/v2"
"github.com/max-messenger/max-bot-api-client-go/v2/model"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog"
"sync"
"time"
)
@@ -52,6 +52,7 @@ type Dispatcher struct {
fsmStorage FSMStorage
eventIsolation EventIsolation
rt *Router
logger zerolog.Logger
wg sync.WaitGroup
}
@@ -59,6 +60,7 @@ type dispatcherOptions struct {
poller Poller
fsmStorage FSMStorage
eventIsolation EventIsolation
logger zerolog.Logger
}
type dispatcherOptionWrapper struct {
@@ -122,7 +124,7 @@ func (d *Dispatcher) handleUpdates(ch <-chan model.Update) {
UserID: upd.UserID,
ChatID: upd.ChatID,
}
l := log.With().
l := d.logger.With().
Str("update_id", key.UpdateID.String()).
Int64("user_id", key.UserID).
Int64("chat_id", key.ChatID).
@@ -132,7 +134,7 @@ func (d *Dispatcher) handleUpdates(ch <-chan model.Update) {
defer cancel()
if err := d.eventIsolation.Lock(lockCtx, key); err != nil {
log.Error().
d.logger.Error().
Err(err).
Int64("user_id", key.UserID).
Int64("chat_id", key.ChatID).
@@ -164,7 +166,7 @@ func (d *Dispatcher) handleUpdate(ctx context.Context, upd model.Update) {
snapshot, err := d.fsmStorage.Get(ctx, isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID})
if err != nil && !errors.Is(err, state.ErrStateNotFound) {
log.Error().Err(err).Send()
d.logger.Error().Err(err).Send()
return
}
if errors.Is(err, state.ErrStateNotFound) {
@@ -176,20 +178,20 @@ func (d *Dispatcher) handleUpdate(ctx context.Context, upd model.Update) {
c := mcontext.New(ctx, upd, st, d.api)
if err := d.rt.HandleUpdate(c); err != nil {
log.Error().Err(err).Send()
d.logger.Error().Err(err).Send()
return
}
st = c.State()
if st.IsEmpty() {
if err := d.fsmStorage.Delete(ctx, isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID}); err != nil {
log.Error().Err(err).Send()
d.logger.Error().Err(err).Send()
}
return
}
if err := d.fsmStorage.Put(ctx, st.Snapshot()); err != nil {
log.Error().Err(err).Send()
d.logger.Error().Err(err).Send()
}
}
@@ -211,11 +213,18 @@ func WithEventIsolation(eventIsolation EventIsolation) DispatcherOption {
})
}
func WithZerolog(logger zerolog.Logger) DispatcherOption {
return newOption(func(options *dispatcherOptions) {
options.logger = logger
})
}
func newDispatcherOptions(api *maxbot.Api) *dispatcherOptions {
return &dispatcherOptions{
poller: NewLongPoller(api),
fsmStorage: memory.NewFSMStorage(),
eventIsolation: memory.NewEventIsolation(defaultTTL),
logger: zerolog.Nop(),
}
}