-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for rate-limiting using Redis (#4)
* feat: add cache support using redis storage * feat: add rate-limiting support for APIs
- Loading branch information
Praveen Yadav
committed
Feb 13, 2024
1 parent
31ce151
commit 08dcbfd
Showing
25 changed files
with
586 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
package v1 | ||
|
||
import "github.com/missingstudio/studio/backend/internal/ingester" | ||
import ( | ||
"github.com/missingstudio/studio/backend/internal/ingester" | ||
"github.com/missingstudio/studio/backend/internal/ratelimiter" | ||
) | ||
|
||
type Deps struct { | ||
ingester ingester.Ingester | ||
ingester ingester.Ingester | ||
ratelimiter *ratelimiter.RateLimiter | ||
} | ||
|
||
func NewDeps(ingester ingester.Ingester) *Deps { | ||
func NewDeps(ingester ingester.Ingester, ratelimiter *ratelimiter.RateLimiter) *Deps { | ||
return &Deps{ | ||
ingester: ingester, | ||
ingester: ingester, | ||
ratelimiter: ratelimiter, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/missingstudio/studio/backend/internal/encoding" | ||
) | ||
|
||
type store interface { | ||
Get(key string) ([]byte, error) | ||
Set(key string, value any, ttl time.Duration) error | ||
} | ||
|
||
type Cache struct { | ||
store store | ||
} | ||
|
||
func NewCache(s store) *Cache { | ||
return &Cache{ | ||
store: s, | ||
} | ||
} | ||
|
||
func (c *Cache) ComputeHashKey(value string) string { | ||
return encoding.Encode(value) | ||
} | ||
|
||
func (c *Cache) SetValue(key string, value []byte, ttl time.Duration) error { | ||
return c.store.Set(c.ComputeHashKey(key), value, ttl) | ||
} | ||
|
||
func (c *Cache) GetValue(key string) ([]byte, error) { | ||
return c.store.Get(c.ComputeHashKey(key)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cache_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/missingstudio/studio/backend/internal/cache" | ||
"github.com/missingstudio/studio/backend/internal/mock" | ||
) | ||
|
||
func TestCache_SetValue(t *testing.T) { | ||
// Create a new instance of the MockStore | ||
mockStore := mock.NewMockStore() | ||
|
||
// Create a new Cache with the MockStore | ||
cache := cache.NewCache(mockStore) | ||
|
||
key := "key" | ||
value := []byte("value") | ||
ttl := 5 * time.Second | ||
|
||
// Set the value in the cache | ||
err := cache.SetValue(key, value, ttl) | ||
if err != nil { | ||
t.Errorf("Error setting value in cache: %v", err) | ||
} | ||
|
||
// Verify that the value is correctly stored in the MockStore | ||
storedValue, err := mockStore.Get(cache.ComputeHashKey(key)) | ||
if err != nil { | ||
t.Errorf("Error getting value from store: %v", err) | ||
} | ||
|
||
if storedValue == nil { | ||
t.Error("Expected value in store, but got nil") | ||
} else if string(storedValue) != string(value) { | ||
t.Errorf("Expected value '%s' in store, but got '%s'", value, storedValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package encoding | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
) | ||
|
||
func Encode(secret string) string { | ||
h := sha256.New() | ||
h.Write([]byte(secret)) | ||
|
||
return hex.EncodeToString(h.Sum(nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package mock | ||
|
||
import "time" | ||
|
||
type MockStore struct { | ||
data map[string][]byte | ||
} | ||
|
||
func NewMockStore() *MockStore { | ||
return &MockStore{ | ||
data: make(map[string][]byte), | ||
} | ||
} | ||
|
||
func (m *MockStore) Get(key string) ([]byte, error) { | ||
value, exists := m.data[key] | ||
if !exists { | ||
return nil, nil | ||
} | ||
return value, nil | ||
} | ||
|
||
func (m *MockStore) Set(key string, value any, ttl time.Duration) error { | ||
m.data[key] = value.([]byte) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ratelimiter | ||
|
||
type Config struct { | ||
Type string `yaml:"type" json:"type,omitempty" mapstructure:"type" default:"sliding_window"` | ||
DurationInSecond int `yaml:"duration_in_seconds" json:"duration_in_seconds,omitempty" mapstructure:"duration_in_seconds" default:"1"` | ||
NumberOfRequests int `yaml:"number_of_requests" json:"number_of_requests,omitempty" mapstructure:"number_of_requests" default:"20"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ratelimiter | ||
|
||
type RateLimiterProvider interface { | ||
Validate(key string) bool | ||
} |
Oops, something went wrong.