chore: long poller configuration options
This commit is contained in:
1 parent
caacccec56
commit
b1bd827574
2 files changed
+34
-4
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
mcontext "github.com/abelith/gomax/v1/context"
|
||||
"github.com/abelith/gomax/v1/core/pollers"
|
||||
"github.com/abelith/gomax/v1/fsm/memory"
|
||||
"github.com/abelith/gomax/v1/fsm/state"
|
||||
"github.com/abelith/gomax/v1/isolation"
|
||||
@@ -258,7 +259,7 @@ func WithZerolog(logger zerolog.Logger) DispatcherOption {
|
||||
|
||||
func newDispatcherOptions(api *maxbot.Api) *dispatcherOptions {
|
||||
return &dispatcherOptions{
|
||||
poller: NewLongPoller(api),
|
||||
poller: pollers.NewLongPoller(api),
|
||||
fsmStorage: memory.NewFSMStorage(),
|
||||
eventIsolation: memory.NewEventIsolation(defaultTTL),
|
||||
logger: zerolog.Nop(),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package core
|
||||
package pollers
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
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"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -35,6 +36,7 @@ type Webhook struct {
|
||||
type LongPoller struct {
|
||||
api *maxbot.Api
|
||||
pollInterval time.Duration
|
||||
logger zerolog.Logger
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
@@ -57,6 +59,11 @@ type webhookOptions struct {
|
||||
|
||||
type longPollerOptions struct {
|
||||
pollInterval time.Duration
|
||||
logger zerolog.Logger
|
||||
}
|
||||
|
||||
type longPollerOptionWrapper struct {
|
||||
fn func(opts *longPollerOptions)
|
||||
}
|
||||
|
||||
type webhookOptionWrapper struct {
|
||||
@@ -143,6 +150,7 @@ func NewLongPoller(api *maxbot.Api, options ...LongPollerOption) *LongPoller {
|
||||
}
|
||||
|
||||
lp.pollInterval = opts.pollInterval
|
||||
lp.logger = opts.logger
|
||||
|
||||
return lp
|
||||
}
|
||||
@@ -201,6 +209,18 @@ func (tp *TestPoller) Stop(_ context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func WithPollInterval(interval time.Duration) LongPollerOption {
|
||||
return newLpOption(func(opts *longPollerOptions) {
|
||||
opts.pollInterval = interval
|
||||
})
|
||||
}
|
||||
|
||||
func WithZerolog(logger zerolog.Logger) LongPollerOption {
|
||||
return newLpOption(func(opts *longPollerOptions) {
|
||||
opts.logger = logger
|
||||
})
|
||||
}
|
||||
|
||||
func WithAddress(addr string) WebhookOption {
|
||||
return newWhOption(func(opts *webhookOptions) {
|
||||
opts.addr = addr
|
||||
@@ -271,9 +291,9 @@ func (lp *LongPoller) poll(ctx context.Context, ch chan model.Update) {
|
||||
updates, marker, err = lp.api.Subscriptions.GetUpdates(ctx, marker)
|
||||
latency := time.Since(start)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
log.Error().Err(err).Send()
|
||||
lp.logger.Error().Err(err).Send()
|
||||
}
|
||||
log.Ctx(ctx).Debug().
|
||||
lp.logger.Debug().
|
||||
Int("received", len(updates)).
|
||||
Dur("latency", latency).
|
||||
Msg("received updates")
|
||||
@@ -290,6 +310,7 @@ func (lp *LongPoller) poll(ctx context.Context, ch chan model.Update) {
|
||||
func newLongPollerOptions() *longPollerOptions {
|
||||
return &longPollerOptions{
|
||||
pollInterval: defaultInterval,
|
||||
logger: zerolog.Nop(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,10 +320,18 @@ func newWebhookOptions() *webhookOptions {
|
||||
}
|
||||
}
|
||||
|
||||
func newLpOption(fn func(opts *longPollerOptions)) LongPollerOption {
|
||||
return longPollerOptionWrapper{fn: fn}
|
||||
}
|
||||
|
||||
func newWhOption(fn func(options *webhookOptions)) WebhookOption {
|
||||
return webhookOptionWrapper{fn: fn}
|
||||
}
|
||||
|
||||
func (low longPollerOptionWrapper) apply(opts *longPollerOptions) {
|
||||
low.fn(opts)
|
||||
}
|
||||
|
||||
func (wow webhookOptionWrapper) apply(opts *webhookOptions) {
|
||||
wow.fn(opts)
|
||||
}
|
||||
Reference in new issue
Block a user