debouncer

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 29, 2024 License: Apache-2.0, MIT Imports: 8 Imported by: 0

README

Debouncer

Library for suppress duplicated calls in distributed systems.

Supported distributed cache and locker

  • Redis
  • Memcached
  • Tarantool(coming soon)

Usage

package main

import (
	"time"

	"github.com/moeryomenko/debouncer"
	cache "github.com/moeryomenko/ttlcache"
)

func main() {
	// create distributed suppressor.
	redisCache, redisLocker := adapters.NewRedisDriver(redis.NewClient(&redis.Options{Addr: "<ip:port>"}))

	suppressor, err := debouncer.NewDebouncer(debouncer.Config{
		Local: {
			TTL:      time.Second,
			Caceh:    cache.NewCache(100, cache.ARC),
		},
		Distributed: {
			Cache:  redisCache,
			Locker: redisLocker,
			Retry:  20 * time.Millisecond,
			TTL:    3 * time.Second,
		},
	})
	if err != nil {
		panic("could not create suppressor")
	}

	...

	result, err := suppressor.Do(key /* token for acquire fn */, fn)
	if err != nil {
		panic("something gone wrong")
	}

	...
}

License

Debouncer is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and/or LICENSE-MIT for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Closure

type Closure[V any] func(context.Context) (V, error)

type Config added in v0.2.0

type Config[V any] struct {
	Local[V]
	Distributed[V]
}

type Debouncer

type Debouncer[V any] struct {
	// contains filtered or unexported fields
}

Debouncer represents distributed suppressor duplicated calls.

func NewDebouncer

func NewDebouncer[V any](cfg Config[V]) (*Debouncer[V], error)

NewDebouncer returns new instance of Debouncer.

func (*Debouncer[V]) Do

func (d *Debouncer[V]) Do(ctx context.Context, key string, closure Closure[V]) (V, error)

Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in from same instance, the duplicate caller waits for the original to complete and receives the same results. The return a channel that will receive the results when they are ready.

type Distributed added in v0.2.0

type Distributed[V any] struct {
	Locker     adapters.LockFactory
	Cache      adapters.Cache
	Retry      time.Duration
	TTL        time.Duration
	Serializer Serializer[V]
}

type DistributedGroup

type DistributedGroup[V any] struct {
	// contains filtered or unexported fields
}

DistributedGroup suppress duplicated calls.

func (*DistributedGroup[V]) Do

func (g *DistributedGroup[V]) Do(ctx context.Context, key string, closure Closure[V]) (V, error)

Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in from intances, the duplicate caller waits for the only once instance to complete and receives the same results. The return a channel that will receive the results when they are ready.

type GobSerializer added in v1.4.0

type GobSerializer[V any] struct {
	// contains filtered or unexported fields
}

func NewGobSerializer added in v1.4.0

func NewGobSerializer[V any]() *GobSerializer[V]

func (*GobSerializer[V]) Deserilize added in v1.4.0

func (s *GobSerializer[V]) Deserilize(data []byte) (V, error)

func (*GobSerializer[V]) Serialize added in v1.4.0

func (s *GobSerializer[V]) Serialize(value V) ([]byte, error)

type JSONSerializer added in v1.4.0

type JSONSerializer[V any] struct{}

func (JSONSerializer[V]) Deserilize added in v1.4.0

func (JSONSerializer[V]) Deserilize(data []byte) (V, error)

func (JSONSerializer[V]) Serialize added in v1.4.0

func (JSONSerializer[V]) Serialize(value V) ([]byte, error)

type Local added in v0.2.0

type Local[V any] struct {
	TTL   time.Duration
	Cache suppressor.Cache[string, V]
}

type Serializer added in v0.3.0

type Serializer[V any] interface {
	Serialize(V) ([]byte, error)
	Deserilize([]byte) (V, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL