-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
43 lines (36 loc) · 809 Bytes
/
factory.go
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
package rate
import (
"errors"
"net/url"
"strconv"
"time"
"github.com/go-redis/redis"
)
// NewSlideWindowStorageFromDSN creates a SlideWindowStorage based on a DSN.
// Example: redis://localhost:6379/0
func NewSlideWindowStorageFromDSN(raw string) (SlideWindowStorage, error) {
dsn, err := url.Parse(raw)
if err != nil {
return nil, err
}
var storage SlideWindowStorage
switch dsn.Scheme {
case "redis":
ops := &redis.Options{
Addr: dsn.Host,
DB: 0,
}
db, err := strconv.Atoi(dsn.Path)
if err == nil {
ops.DB = db
}
storage = NewRedisSlideWindowStorage(
redis.NewClient(ops),
)
case "inmemory":
storage = NewInMemorySlideWindowStorage(make(map[string][]time.Time))
default:
return nil, errors.New("invalid slide window storage")
}
return storage, nil
}