Skip to content

Commit

Permalink
use ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
da440dil committed Aug 28, 2019
1 parent 980fddc commit 33aff3b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 39 deletions.
32 changes: 0 additions & 32 deletions gateway/memory/cleaner.go

This file was deleted.

13 changes: 8 additions & 5 deletions gateway/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
package memory

import (
"context"
"runtime"
"sync"
"time"

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

// Gateway to memory storage.
Expand All @@ -14,19 +17,19 @@ type Gateway struct {

// New creates new Gateway.
func New(cleanupInterval time.Duration) *Gateway {
ctx, cancel := context.WithCancel(context.Background())
s := &storage{
items: make(map[string]*item),
cleaner: newCleaner(cleanupInterval),
cancel: cancel,
}
// See https://gist.github.com/da440dil/6469a37e649a560f528fd116fa2e874d
gw := &Gateway{s}
go s.cleaner.Run(s.deleteExpired)
go ticker.Run(ctx, s.deleteExpired, cleanupInterval)
runtime.SetFinalizer(gw, finalizer)
return gw
}

func finalizer(gw *Gateway) {
gw.cleaner.Stop()
gw.cancel()
}

type item struct {
Expand All @@ -37,7 +40,7 @@ type item struct {
type storage struct {
items map[string]*item
mutex sync.Mutex
cleaner *cleaner
cancel func()
}

func (s *storage) Incr(key string, ttl int) (int, int, error) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/da440dil/go-counter
go 1.12

require (
github.com/da440dil/go-ticker v0.0.0
github.com/da440dil/go-trier v0.0.1
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
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/da440dil/go-ticker v0.0.0 h1:sH/YeYf//tylK2fShGZi2IQRdtFd/YQgiCnOY2CnTNQ=
github.com/da440dil/go-ticker v0.0.0/go.mod h1:Ia+nQQeharuhnq/NYri4BrU0kVlU/nsmhHY8+65O3lE=
github.com/da440dil/go-trier v0.0.1 h1:plGC/S++x1ZlNjqI+uv8t/32P3TrNY6ddd57cIaxHdU=
github.com/da440dil/go-trier v0.0.1/go.mod h1:Xc70OohXdJvxPLfUDjJG3TeOiitY7K4jx+Q0tm5dIy0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down Expand Up @@ -32,6 +34,8 @@ github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down

0 comments on commit 33aff3b

Please sign in to comment.