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"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
mcontext "github.com/abelith/gomax/v1/context"
|
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/memory"
|
||||||
"github.com/abelith/gomax/v1/fsm/state"
|
"github.com/abelith/gomax/v1/fsm/state"
|
||||||
"github.com/abelith/gomax/v1/isolation"
|
"github.com/abelith/gomax/v1/isolation"
|
||||||
@@ -258,7 +259,7 @@ func WithZerolog(logger zerolog.Logger) DispatcherOption {
|
|||||||
|
|
||||||
func newDispatcherOptions(api *maxbot.Api) *dispatcherOptions {
|
func newDispatcherOptions(api *maxbot.Api) *dispatcherOptions {
|
||||||
return &dispatcherOptions{
|
return &dispatcherOptions{
|
||||||
poller: NewLongPoller(api),
|
poller: pollers.NewLongPoller(api),
|
||||||
fsmStorage: memory.NewFSMStorage(),
|
fsmStorage: memory.NewFSMStorage(),
|
||||||
eventIsolation: memory.NewEventIsolation(defaultTTL),
|
eventIsolation: memory.NewEventIsolation(defaultTTL),
|
||||||
logger: zerolog.Nop(),
|
logger: zerolog.Nop(),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package core
|
package pollers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
maxbot "github.com/max-messenger/max-bot-api-client-go/v2"
|
maxbot "github.com/max-messenger/max-bot-api-client-go/v2"
|
||||||
"github.com/max-messenger/max-bot-api-client-go/v2/model"
|
"github.com/max-messenger/max-bot-api-client-go/v2/model"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -35,6 +36,7 @@ type Webhook struct {
|
|||||||
type LongPoller struct {
|
type LongPoller struct {
|
||||||
api *maxbot.Api
|
api *maxbot.Api
|
||||||
pollInterval time.Duration
|
pollInterval time.Duration
|
||||||
|
logger zerolog.Logger
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,6 +59,11 @@ type webhookOptions struct {
|
|||||||
|
|
||||||
type longPollerOptions struct {
|
type longPollerOptions struct {
|
||||||
pollInterval time.Duration
|
pollInterval time.Duration
|
||||||
|
logger zerolog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
type longPollerOptionWrapper struct {
|
||||||
|
fn func(opts *longPollerOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
type webhookOptionWrapper struct {
|
type webhookOptionWrapper struct {
|
||||||
@@ -143,6 +150,7 @@ func NewLongPoller(api *maxbot.Api, options ...LongPollerOption) *LongPoller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lp.pollInterval = opts.pollInterval
|
lp.pollInterval = opts.pollInterval
|
||||||
|
lp.logger = opts.logger
|
||||||
|
|
||||||
return lp
|
return lp
|
||||||
}
|
}
|
||||||
@@ -201,6 +209,18 @@ func (tp *TestPoller) Stop(_ context.Context) error {
|
|||||||
return nil
|
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 {
|
func WithAddress(addr string) WebhookOption {
|
||||||
return newWhOption(func(opts *webhookOptions) {
|
return newWhOption(func(opts *webhookOptions) {
|
||||||
opts.addr = addr
|
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)
|
updates, marker, err = lp.api.Subscriptions.GetUpdates(ctx, marker)
|
||||||
latency := time.Since(start)
|
latency := time.Since(start)
|
||||||
if err != nil && !errors.Is(err, context.Canceled) {
|
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)).
|
Int("received", len(updates)).
|
||||||
Dur("latency", latency).
|
Dur("latency", latency).
|
||||||
Msg("received updates")
|
Msg("received updates")
|
||||||
@@ -290,6 +310,7 @@ func (lp *LongPoller) poll(ctx context.Context, ch chan model.Update) {
|
|||||||
func newLongPollerOptions() *longPollerOptions {
|
func newLongPollerOptions() *longPollerOptions {
|
||||||
return &longPollerOptions{
|
return &longPollerOptions{
|
||||||
pollInterval: defaultInterval,
|
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 {
|
func newWhOption(fn func(options *webhookOptions)) WebhookOption {
|
||||||
return webhookOptionWrapper{fn: fn}
|
return webhookOptionWrapper{fn: fn}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (low longPollerOptionWrapper) apply(opts *longPollerOptions) {
|
||||||
|
low.fn(opts)
|
||||||
|
}
|
||||||
|
|
||||||
func (wow webhookOptionWrapper) apply(opts *webhookOptions) {
|
func (wow webhookOptionWrapper) apply(opts *webhookOptions) {
|
||||||
wow.fn(opts)
|
wow.fn(opts)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user