mirror of
https://github.com/makayabou/asg-server.git
synced 2026-05-02 17:43:36 +02:00
Added: debounce time configuration
This commit is contained in:
parent
26bc24e08a
commit
789a6867df
@ -14,6 +14,8 @@ fcm:
|
||||
{
|
||||
...
|
||||
}
|
||||
timeout_seconds: 1
|
||||
debounce_seconds: 1
|
||||
tasks:
|
||||
hashing:
|
||||
interval_seconds: 15
|
||||
|
||||
@ -23,6 +23,8 @@ type Database struct {
|
||||
|
||||
type FCMConfig struct {
|
||||
CredentialsJSON string `yaml:"credentials_json"`
|
||||
DebounceSeconds uint16 `yaml:"debounce_seconds"`
|
||||
TimeoutSeconds uint16 `yaml:"timeout_seconds"`
|
||||
}
|
||||
|
||||
type Tasks struct {
|
||||
|
||||
@ -42,6 +42,8 @@ var Module = fx.Module(
|
||||
fx.Provide(func(cfg Config) services.PushServiceConfig {
|
||||
return services.PushServiceConfig{
|
||||
CredentialsJSON: cfg.FCM.CredentialsJSON,
|
||||
Debounce: time.Duration(cfg.FCM.DebounceSeconds) * time.Second,
|
||||
Timeout: time.Duration(cfg.FCM.TimeoutSeconds) * time.Second,
|
||||
}
|
||||
}),
|
||||
fx.Provide(func(cfg Config) tasks.HashingTaskConfig {
|
||||
|
||||
@ -33,9 +33,11 @@ type PushService struct {
|
||||
|
||||
type PushServiceConfig struct {
|
||||
CredentialsJSON string
|
||||
Debounce time.Duration
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// NewPushService creates a new PushService.
|
||||
func NewPushService(params PushServiceParams) *PushService {
|
||||
if params.Config.Timeout == 0 {
|
||||
params.Config.Timeout = time.Second
|
||||
@ -48,7 +50,7 @@ func NewPushService(params PushServiceParams) *PushService {
|
||||
}
|
||||
}
|
||||
|
||||
// init
|
||||
// init initializes the FCM client.
|
||||
func (s *PushService) init(ctx context.Context) (err error) {
|
||||
s.mux.Lock()
|
||||
defer s.mux.Unlock()
|
||||
@ -71,6 +73,7 @@ func (s *PushService) init(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// sendAll sends messages to all targets from the cache after initializing the service.
|
||||
func (s *PushService) sendAll(ctx context.Context) {
|
||||
if err := s.init(ctx); err != nil {
|
||||
s.Logger.Error("Can't init push service", zap.Error(err))
|
||||
@ -92,6 +95,7 @@ func (s *PushService) sendAll(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// sendSingle sends a single message to the specified token
|
||||
func (s *PushService) sendSingle(ctx context.Context, token string, data map[string]string) error {
|
||||
_, err := s.client.Send(ctx, &messaging.Message{
|
||||
Data: data,
|
||||
@ -104,8 +108,13 @@ func (s *PushService) sendSingle(ctx context.Context, token string, data map[str
|
||||
return err
|
||||
}
|
||||
|
||||
// Run runs the service with the provided context if a debounce is set.
|
||||
func (s *PushService) Run(ctx context.Context) {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
if s.Config.Debounce == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(s.Config.Debounce)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
@ -118,8 +127,13 @@ func (s *PushService) Run(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Enqueue adds the data to the cache and immediately sends all messages if the debounce is 0.
|
||||
func (s *PushService) Enqueue(ctx context.Context, token string, data map[string]string) error {
|
||||
s.cache.Set(token, data)
|
||||
|
||||
if s.Config.Debounce == 0 {
|
||||
s.sendAll(ctx)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user