debouncer

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: Apache-2.0, MIT Imports: 5 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"
)

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,
			Capacity: 100,
			Policy:   cache.LFU,
		},
		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 CacheDriver

type CacheDriver int
const (
	// Memcached indicates use Memcached like distributed cache and locker.
	Memcached CacheDriver = iota
	// Redis indicates use Redis like distributed cache and locker.
	Redis
)

type Closure

type Closure func() (any, error)

type Config added in v0.2.0

type Config struct {
	Local
	Distributed
}

type Debouncer

type Debouncer struct {
	// contains filtered or unexported fields
}

Debouncer represents distributed suppressor duplicated calls.

func NewDebouncer

func NewDebouncer(cfg Config) (*Debouncer, error)

NewDebouncer returns new instance of Debouncer.

func (*Debouncer) Do

func (d *Debouncer) Do(key string, closure Closure) (any, 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 struct {
	Locker     adapters.LockFactory
	Cache      adapters.Cache
	Retry      time.Duration
	TTL        time.Duration
	Serializer Serializer
}

type DistributedGroup

type DistributedGroup struct {
	// contains filtered or unexported fields
}

DistributedGroup suppress duplicated calls.

func (*DistributedGroup) Do

func (g *DistributedGroup) Do(key string, closure Closure) (any, 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 Local added in v0.2.0

type Local struct {
	TTL      time.Duration
	Capacity int
	Policy   cache.EvictionPolicy
}

type Serializer added in v0.3.0

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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