Skip to content

Commit dca7d94

Browse files
committed
make fields exportable
1 parent da76ef3 commit dca7d94

6 files changed

Lines changed: 34 additions & 34 deletions

File tree

redis_store.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
type RedigoStore struct {
10-
pool *redis.Pool
11-
script *redis.Script
10+
Pool *redis.Pool
11+
Script *redis.Script
1212
}
1313

1414
func newRedisPool(address string) *redis.Pool {
@@ -27,32 +27,32 @@ func NewRedigoStore(pool *redis.Pool) RedigoStore {
2727
conn := pool.Get()
2828
defer conn.Close()
2929

30-
var script = redis.NewScript(1, tokenBucketScript)
30+
var script = redis.NewScript(1, TokenBucketScript)
3131
err := script.Load(conn)
3232
if err != nil {
3333
panic(err)
3434
}
35-
return RedigoStore{pool: pool, script: script}
35+
return RedigoStore{Pool: pool, Script: script}
3636
}
3737

3838
func NewRedigoSWStore(pool *redis.Pool) RedigoStore {
3939
// we will initialise with the script
4040
conn := pool.Get()
4141
defer conn.Close()
4242

43-
var script = redis.NewScript(1, slidingWindowScript)
43+
var script = redis.NewScript(1, SlidingWindowScript)
4444
err := script.Load(conn)
4545
if err != nil {
4646
panic(err)
4747
}
48-
return RedigoStore{pool: pool, script: script}
48+
return RedigoStore{Pool: pool, Script: script}
4949
}
5050

5151
func (s *RedigoStore) inc(key string, rate, windowSize, now int) (map[string]int, error) {
52-
conn := s.pool.Get()
52+
conn := s.Pool.Get()
5353
defer conn.Close()
5454

55-
r, err := redis.IntMap(s.script.Do(conn, key, rate, windowSize, now))
55+
r, err := redis.IntMap(s.Script.Do(conn, key, rate, windowSize, now))
5656
if err != nil {
5757
return nil, err
5858
}

sliding_window.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
)
77

88
type SlidingWindow struct {
9-
identifier string
10-
rate int
11-
windowSize int
12-
store Store
9+
Identifier string
10+
Rate int
11+
WindowSize int
12+
Store Store
1313
}
1414

1515
// The sliding window algorithm
@@ -33,16 +33,16 @@ func (sw *SlidingWindow) Allow(key string) (bool, error) {
3333
}
3434

3535
func (sw *SlidingWindow) AllowWithStatus(key string) (Status, error) {
36-
userKey := fmt.Sprintf("%s:%s", sw.identifier, key)
37-
res, err := sw.store.Inc(userKey, sw.rate, sw.windowSize, int(TimeMillis(timeNow())))
36+
userKey := fmt.Sprintf("%s:%s", sw.Identifier, key)
37+
res, err := sw.Store.Inc(userKey, sw.Rate, sw.WindowSize, int(TimeMillis(timeNow())))
3838
if err != nil {
3939
return Status{}, err
4040
}
4141
timeElapsed := timeNow().Sub(res.LastRefill)
4242
s := Status{
4343
Allowed: res.Allowed,
44-
Remaining: sw.rate - res.Counter,
45-
NextRefresh: timeElapsed + (time.Duration(sw.windowSize) * time.Millisecond),
44+
Remaining: sw.Rate - res.Counter,
45+
NextRefresh: timeElapsed + (time.Duration(sw.WindowSize) * time.Millisecond),
4646
}
4747
return s, nil
4848
}

sliding_window_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ func TestWindow_AllowWithStatus(t *testing.T) {
1212
key := "test"
1313

1414
sw := &SlidingWindow{
15-
identifier: "marketing_campaigns",
16-
rate: rate,
17-
windowSize: 1000,
18-
store: &store,
15+
Identifier: "marketing_campaigns",
16+
Rate: rate,
17+
WindowSize: 1000,
18+
Store: &store,
1919
}
2020
// nextRefresh := time.Duration(b.windowSize) * time.Millisecond
2121
for i := 1; i <= rate; i++ {

token_bucket.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
var timeNow = time.Now
99

1010
type Bucket struct {
11-
identifier string
12-
rate int
13-
windowSize int
14-
store Store
11+
Identifier string
12+
Rate int
13+
WindowSize int
14+
Store Store
1515
}
1616

1717
type Status struct {
@@ -47,16 +47,16 @@ func (b *Bucket) Allow(key string) (bool, error) {
4747
}
4848

4949
func (b *Bucket) AllowWithStatus(key string) (Status, error) {
50-
userKey := fmt.Sprintf("%s:%s", b.identifier, key)
51-
res, err := b.store.Inc(userKey, b.rate, b.windowSize, int(TimeMillis(timeNow())))
50+
userKey := fmt.Sprintf("%s:%s", b.Identifier, key)
51+
res, err := b.Store.Inc(userKey, b.Rate, b.WindowSize, int(TimeMillis(timeNow())))
5252
if err != nil {
5353
return Status{}, err
5454
}
5555
timeElapsed := timeNow().Sub(res.LastRefill)
5656
s := Status{
5757
Allowed: res.Allowed,
58-
Remaining: b.rate - res.Counter,
59-
NextRefresh: timeElapsed + (time.Duration(b.windowSize) * time.Millisecond),
58+
Remaining: b.Rate - res.Counter,
59+
NextRefresh: timeElapsed + (time.Duration(b.WindowSize) * time.Millisecond),
6060
}
6161
return s, nil
6262
}

token_bucket_script.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ratelimit
22

3-
var tokenBucketScript = `
3+
var TokenBucketScript = `
44
local key = KEYS[1]
55
local rate = tonumber(ARGV[1])
66
local window = tonumber(ARGV[2])
@@ -64,7 +64,7 @@ end
6464
return run()
6565
`
6666

67-
var slidingWindowScript = `
67+
var SlidingWindowScript = `
6868
local key = KEYS[1]
6969
local rate = tonumber(ARGV[1])
7070
local window = tonumber(ARGV[2])

token_bucket_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ func TestBucket_AllowWithStatus(t *testing.T) {
1717
}
1818

1919
b := &Bucket{
20-
identifier: "marketing_campaigns",
21-
rate: rate,
22-
windowSize: 1000,
23-
store: &store,
20+
Identifier: "marketing_campaigns",
21+
Rate: rate,
22+
WindowSize: 1000,
23+
Store: &store,
2424
}
2525
// nextRefresh := time.Duration(b.windowSize) * time.Millisecond
2626
for i := 1; i <= rate; i++ {

0 commit comments

Comments
 (0)