debouncer

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: Apache-2.0, MIT Imports: 3 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() (interface{}, error)

type Debouncer

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

Debouncer represents distributed suppressor duplicated calls.

func NewDebouncer

func NewDebouncer(
	impl CacheDriver, dsn string,
	localCacheTTL, distributedCacheTTL time.Duration,
) (*Debouncer, error)

NewDebouncer returns new instance of Debouncer.

func (*Debouncer) Do

func (d *Debouncer) Do(key string, duration time.Duration, 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 DistributedGroup

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

DistributedGroup suppress duplicated calls.

func (*DistributedGroup) Do

func (g *DistributedGroup) Do(key string, duration time.Duration, 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 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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