Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
da440dil committed Aug 7, 2019
1 parent 49fbde7 commit 87f8eed
Showing 1 changed file with 18 additions and 39 deletions.
57 changes: 18 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,25 @@

Distributed rate limiting with pluggable storage to store a counters state.

## Example
## Basic usage

```go
package main

import (
"fmt"
"sync"
"time"

"github.com/da440dil/go-counter"
)

func main() {
c, err := counter.New(2, time.Millisecond*100)
if err != nil {
panic(err)
}
key := "key"
var wg sync.WaitGroup
count := func(n int) {
wg.Add(1)
go func() {
v, err := c.Count(key)
if err == nil {
fmt.Printf("Counter #%v has counted the key, remainder %v\n", n, v)
} else {
if e, ok := err.(counter.TTLError); ok {
fmt.Printf("Counter #%v has reached the limit, retry after %v\n", n, e.TTL())
} else {
panic(err)
}
}
wg.Done()
}()
// Create new Counter
c, _ := counter.New(1, time.Millisecond*100)
// Increment counter and get remainder
if v, err := c.Count(key); err != nil {
if e, ok := err.(locker.TTLError); ok {
// Use e.TTL() if need
}

count(1) // Counter #1 has counted the key, remainder 1
count(2) // Counter #2 has counted the key, remainder 0
count(3) // Counter #3 has reached the limit, retry after 100ms
wg.Wait()
} else {
// Counter value equals 1
// Remainder (v) equals 0
// Next c.Count(key) call will return TTLError
}
```
```

## Example usage

- [example](./examples/counter-gateway-default/main.go) usage with default [gateway](./gateway/memory/memory.go)
- [example](./examples/counter-gateway-memory/main.go) usage with memory [gateway](./gateway/memory/memory.go)
- [example](./examples/counter-gateway-redis/main.go) usage with [Redis](https://redis.io) [gateway](./gateway/redis/redis.go)

0 comments on commit 87f8eed

Please sign in to comment.