dcounter

package module
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2019 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

DCounter

Build Status codecov MIT Licence

DCounter is a distributed counter library for golang.

Usage

  • Install
go get github.com/rashadansari/dcounter
  • Example
package main

import (
	"github.com/rashadansari/dcounter"

	"log"
	"time"

	"github.com/go-redis/redis"
)

type Counter struct {
	value int64
}

func (c *Counter) GetLastValue(key string) (int64, error) {
	return c.value, nil
}

func (c *Counter) SetLastValue(key string, count int64, payload interface{}) error {
	c.value = count
	return nil
}

func main() {
	redisOptions := &redis.Options{
		Addr: "127.0.0.1:6379",
	}
	redisClient := redis.NewClient(redisOptions)

	counter := &Counter{
		value: 0,
	}
	options := &dcounter.Options{
		Counter:     counter,
		RedisClient: redisClient,
	}
	dc := dcounter.New("name", options)

	key := "key"
	it := 100

	for i := 0; i < it; i++ {
		go func() {
			count, err := dc.Inc(key)
			if err != nil {
				log.Printf("inc err: %s", err)
			} else {
				log.Printf("inc value: %d", count)
			}
		}()
	}

	time.Sleep(20 * time.Second)
	log.Printf("counter value: %d", counter.value)

	for i := 0; i < it; i++ {
		go func() {
			count, err := dc.Dec(key)
			if err != nil {
				log.Printf("dec err: %s", err)
			} else {
				log.Printf("dec value: %d", count)
			}
		}()
	}

	time.Sleep(20 * time.Second)
	log.Printf("counter value: %d", counter.value)
}

Documentation

Index

Constants

View Source
const (
	RedisCounterPostfix = "-counter"
	RedisLockPostfix    = "-lock"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter interface {
	GetLastValue(key string) (int64, error)
	SetLastValue(key string, value int64, payload interface{}) error
}

type CounterResponse added in v1.0.3

type CounterResponse struct {
	Value int64
	Error error
}

type DCounter

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

func New

func New(name string, opt *Options) *DCounter

func (*DCounter) AsyncDec added in v1.0.3

func (dc *DCounter) AsyncDec(key string) (chan CounterResponse, error)

func (*DCounter) AsyncDecWithPayload added in v1.0.4

func (dc *DCounter) AsyncDecWithPayload(key string, payload interface{}) (chan CounterResponse, error)

func (*DCounter) AsyncInc added in v1.0.3

func (dc *DCounter) AsyncInc(key string) (chan CounterResponse, error)

func (*DCounter) AsyncIncWithPayload added in v1.0.4

func (dc *DCounter) AsyncIncWithPayload(key string, payload interface{}) (chan CounterResponse, error)

func (*DCounter) Dec

func (dc *DCounter) Dec(key string) (int64, error)

func (*DCounter) DecWithPayload added in v1.0.4

func (dc *DCounter) DecWithPayload(key string, payload interface{}) (int64, error)

func (*DCounter) Inc

func (dc *DCounter) Inc(key string) (int64, error)

func (*DCounter) IncWithPayload added in v1.0.4

func (dc *DCounter) IncWithPayload(key string, payload interface{}) (int64, error)

type Options

type Options struct {
	Counter          Counter
	RedisClient      *redis.Client
	RedisLock        *redislock.Client
	RedisLockTimeout time.Duration
	RedisLockOptions *redislock.Options
	CounterPoolSize  int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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