Skip to content

Commit

Permalink
add example with retry
Browse files Browse the repository at this point in the history
  • Loading branch information
da440dil committed Aug 8, 2019
1 parent f49012b commit 05e7287
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ if v, err := c.Count("key"); err != nil {

- [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)
- [example](./examples/counter-gateway-redis/main.go) usage with [Redis](https://redis.io) [gateway](./gateway/redis/redis.go)
- [example](./examples/counter-with-retry/main.go) usage with [retry](https://github.com/da440dil/go-runner)
53 changes: 53 additions & 0 deletions examples/counter-with-retry/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"time"

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

func main() {
// Create counter
c, err := counter.New(2, time.Millisecond*20)
if err != nil {
panic(err)
}
// Create runner
r, err := runner.New(
// Set maximum number of retries
runner.WithRetryCount(1),
// Set delay between retries
runner.WithRetryDelay(time.Millisecond*40),
)
if err != nil {
panic(err)
}
// Create retriable function
fn := func(n int) func() (bool, error) {
return func() (bool, error) {
v, err := c.Count("key")
if err == nil {
fmt.Printf("Counter #%v has counted the key, remainder %v\n", n, v)
return true, nil // Success
}
if e, ok := err.(counter.TTLError); ok {
fmt.Printf("Counter #%v has reached the limit, retry after %v\n", n, e.TTL())
return false, nil // Failure
}
return false, err // Error
}
}
for i := 1; i < 4; i++ {
// Run function
if err = r.Run(fn(i)); err != nil {
panic(err)
}
}
// Output:
// Counter #1 has counted the key, remainder 1
// Counter #2 has counted the key, remainder 0
// Counter #3 has reached the limit, retry after 20ms
// Counter #3 has counted the key, remainder 1
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/da440dil/go-counter
go 1.12

require (
github.com/da440dil/go-runner v0.0.3 // indirect
github.com/go-redis/redis v6.15.2+incompatible
github.com/golang/protobuf v1.3.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/da440dil/go-runner v0.0.3 h1:WWdU7wk1isE3lxuxK1nHc2miO3kUxe0qM+K/8eoCSfU=
github.com/da440dil/go-runner v0.0.3/go.mod h1:iMGPIQI7VouRpfZZbhckqdjJKtPBntxGrvzObqG/oII=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down

0 comments on commit 05e7287

Please sign in to comment.