fix: major fixes
This commit is contained in:
1 parent
9d7a6a46c9
commit
4b94fcb920
6 files changed
+90
-35
No files matched your search
+42
-16
@@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
mcontext "github.com/abelith/gomax/v1/context"
|
||||
"github.com/abelith/gomax/v1/isolation"
|
||||
"github.com/abelith/gomax/v1/state"
|
||||
"github.com/abelith/gomax/v1/state/memory"
|
||||
"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"
|
||||
@@ -14,12 +16,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTTL = 5 * time.Second
|
||||
defaultTTL = 30 * time.Second
|
||||
defaultLockTimeout = 10 * time.Second
|
||||
defaultHandlerTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
type EventIsolation interface {
|
||||
Lock(ctx context.Context, userID, chatID int) error
|
||||
Unlock(ctx context.Context, userID, chatID int) error
|
||||
Lock(ctx context.Context, key isolation.Key) error
|
||||
Unlock(ctx context.Context, key isolation.Key) error
|
||||
}
|
||||
|
||||
type FSMStorage interface {
|
||||
@@ -78,8 +82,8 @@ func NewDispatcher(api *maxbot.Api, options ...DispatcherOption) *Dispatcher {
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Dispatcher) Handle(handler UpdateHandler, filters func(c *mcontext.Context) bool) {
|
||||
d.rt.Handle(handler, filters)
|
||||
func (d *Dispatcher) Handle(handler UpdateHandler, filters ...func(c *mcontext.Context) bool) {
|
||||
d.rt.Handle(handler, filters...)
|
||||
}
|
||||
|
||||
func (d *Dispatcher) HandleFunc(handler func(c *mcontext.Context) error, filters ...func(c *mcontext.Context) bool) {
|
||||
@@ -101,7 +105,6 @@ func (d *Dispatcher) Start(ctx context.Context) error {
|
||||
}
|
||||
|
||||
d.handleUpdates(ch)
|
||||
<-ctx.Done()
|
||||
d.wg.Wait()
|
||||
return nil
|
||||
}
|
||||
@@ -109,21 +112,44 @@ func (d *Dispatcher) Start(ctx context.Context) error {
|
||||
func (d *Dispatcher) handleUpdates(ch <-chan model.Update) {
|
||||
for upd := range ch {
|
||||
d.wg.Go(func() {
|
||||
ctx := context.Background()
|
||||
key := isolation.Key{
|
||||
UpdateID: uuid.New(),
|
||||
UserID: upd.UserID,
|
||||
ChatID: upd.ChatID,
|
||||
}
|
||||
l := log.With().
|
||||
Str("update_id", key.UpdateID.String()).
|
||||
Int64("user_id", key.UserID).
|
||||
Int64("chat_id", key.ChatID).
|
||||
Logger()
|
||||
|
||||
_ = d.eventIsolation.Lock(ctx, int(upd.UserID), int(upd.ChatID)) // todo: handle error somehow (retry or smth)
|
||||
lockCtx, cancel := context.WithTimeout(context.Background(), defaultLockTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := d.eventIsolation.Lock(lockCtx, key); err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Int64("user_id", key.UserID).
|
||||
Int64("chat_id", key.ChatID).
|
||||
Msg("failed to lock on update")
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
err := d.eventIsolation.Unlock(ctx, int(upd.UserID), int(upd.ChatID))
|
||||
if err != nil {
|
||||
log.Warn().
|
||||
Err(err).
|
||||
Int64("user_id", upd.UserID).
|
||||
Int64("chat_id", upd.ChatID).
|
||||
Msg("failed to unlock on update")
|
||||
unlockCtx, unlockCancel := context.WithTimeout(context.Background(), defaultLockTimeout)
|
||||
defer unlockCancel()
|
||||
|
||||
err := d.eventIsolation.Unlock(unlockCtx, key)
|
||||
switch {
|
||||
case errors.Is(err, isolation.ErrNotOwner):
|
||||
l.Warn().Msg("lock expired before handler finished")
|
||||
case err != nil:
|
||||
l.Warn().Err(err).Msg("failed to unlock on update")
|
||||
}
|
||||
}()
|
||||
|
||||
d.handleUpdate(ctx, upd)
|
||||
handleCtx, cancel := context.WithTimeout(context.Background(), defaultHandlerTimeout)
|
||||
defer cancel()
|
||||
d.handleUpdate(handleCtx, upd)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -128,6 +128,7 @@ func (w *Webhook) Start(_ context.Context) (<-chan model.Update, error) {
|
||||
}
|
||||
|
||||
func (w *Webhook) Stop(ctx context.Context) error {
|
||||
close(w.ch)
|
||||
return w.srv.Shutdown(ctx)
|
||||
}
|
||||
|
||||
@@ -176,8 +177,6 @@ func (lp *LongPoller) Start(ctx context.Context) (<-chan model.Update, error) {
|
||||
|
||||
for _, update := range updates {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case ch <- update:
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user