File: goredisstore_test.go

package info (click to toggle)
golang-github-throttled-throttled 2.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 172 kB
  • sloc: makefile: 23
file content (94 lines) | stat: -rw-r--r-- 2,119 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package goredisstore_test

import (
	"log"
	"testing"
	"time"

	"github.com/go-redis/redis"
	"github.com/throttled/throttled"
	"github.com/throttled/throttled/store/goredisstore"
	"github.com/throttled/throttled/store/storetest"
)

const (
	redisTestDB     = 1
	redisTestPrefix = "throttled-go-redis:"
)

// Demonstrates that how to initialize a RateLimiter with redis
// using go-redis library.
func ExampleNew() {
	// import "github.com/go-redis/redis"

	// Initialize a redis client using go-redis
	client := redis.NewClient(&redis.Options{
		PoolSize:    10, // default
		IdleTimeout: 30 * time.Second,
		Addr:        "localhost:6379",
		Password:    "", // no password set
		DB:          0,  // use default DB
	})

	// Setup store
	store, err := goredisstore.New(client, "throttled:")
	if err != nil {
		log.Fatal(err)
	}

	// Setup quota
	quota := throttled.RateQuota{MaxRate: throttled.PerMin(20), MaxBurst: 5}

	// Then, use store and quota as arguments for NewGCRARateLimiter()
	throttled.NewGCRARateLimiter(store, quota)
}

func TestRedisStore(t *testing.T) {
	c, st := setupRedis(t, 0)
	defer c.Close()
	defer clearRedis(c)

	clearRedis(c)
	storetest.TestGCRAStore(t, st)
	storetest.TestGCRAStoreTTL(t, st)
}

func BenchmarkRedisStore(b *testing.B) {
	c, st := setupRedis(b, 0)
	defer c.Close()
	defer clearRedis(c)

	storetest.BenchmarkGCRAStore(b, st)
}

func clearRedis(c *redis.Client) error {
	keys, err := c.Keys(redisTestPrefix + "*").Result()
	if err != nil {
		return err
	}

	return c.Del(keys...).Err()
}

func setupRedis(tb testing.TB, ttl time.Duration) (*redis.Client, *goredisstore.GoRedisStore) {
	client := redis.NewClient(&redis.Options{
		PoolSize:    10, // default
		IdleTimeout: 30 * time.Second,
		Addr:        "localhost:6379",
		Password:    "",          // no password set
		DB:          redisTestDB, // use default DB
	})

	if err := client.Ping().Err(); err != nil {
		client.Close()
		tb.Skip("redis server not available on localhost port 6379")
	}

	st, err := goredisstore.New(client, redisTestPrefix)
	if err != nil {
		client.Close()
		tb.Fatal(err)
	}

	return client, st
}