debouncer

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: Apache-2.0, MIT Imports: 4 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.
	suppressor, err := debouncer.NewDebouncer(
		debouncer.Redis,   // driver.
		"localhost:6379",  // host and port to redis.
		time.Second,       // expiration duration for instance cache.
		3*time.Second,     // expiration duration for Redis.
	)
	if err != nil {
		panic("could not create suppressor")
	}

	...

	result, err := suppressor.Do(key /* token for acquire fn */, 300 * time.Millisecond /* time of execute 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() ([]byte, 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) (interface{}, 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
}

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) ([]byte, 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
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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