-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bstrausser
committed
Mar 5, 2024
1 parent
80627b2
commit 4e9f68e
Showing
6 changed files
with
130 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package desync | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"golang.org/x/time/rate" | ||
) | ||
|
||
type ThrottleOptions struct { | ||
eventRate float64 | ||
burstRate int | ||
timeout time.Duration | ||
} | ||
|
||
type RateLimitedLocalStore struct { | ||
|
||
wrappedStore WriteStore | ||
|
||
limiter *rate.Limiter | ||
|
||
options ThrottleOptions | ||
|
||
} | ||
|
||
func NewRateLimitedLocalStore(s WriteStore, options ThrottleOptions) *RateLimitedLocalStore { | ||
|
||
limiter := rate.NewLimiter(rate.Limit(options.eventRate), options.burstRate) | ||
return &RateLimitedLocalStore{wrappedStore: s,limiter: limiter } | ||
} | ||
|
||
func (s RateLimitedLocalStore) GetChunk(id ChunkID) (*Chunk, error) { | ||
|
||
return s.wrappedStore.GetChunk(id) | ||
} | ||
|
||
func (s RateLimitedLocalStore) HasChunk(id ChunkID) (bool, error) { | ||
|
||
|
||
return s.wrappedStore.HasChunk(id) | ||
} | ||
|
||
|
||
func (s RateLimitedLocalStore) StoreChunk(chunk *Chunk) error { | ||
ctx, cancel := context.WithTimeout(context.Background(), s.options.timeout) | ||
defer cancel() | ||
// This isn't ideal because what I'm really interested is in size over the wire. | ||
_, err := chunk.Data() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//size := len(b) | ||
err = s.limiter.WaitN(ctx,1) | ||
if err != nil { | ||
|
||
fmt.Println("Rate limit context error:", err) | ||
} | ||
|
||
return s.wrappedStore.StoreChunk(chunk) | ||
|
||
} |
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,58 @@ | ||
package desync | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
func TestLimiter(t *testing.T){ | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*1000) | ||
defer cancel() | ||
|
||
limiter := rate.NewLimiter(rate.Limit(50), 50) | ||
err := limiter.WaitN(ctx,1) | ||
require.Nil(t, err) | ||
|
||
limiter = rate.NewLimiter(rate.Limit(2), 1) | ||
_ = limiter.WaitN(ctx,1) | ||
require.Nil(t, err) | ||
err = limiter.WaitN(ctx,1) | ||
require.Nil(t, err) | ||
|
||
limiter = rate.NewLimiter(rate.Limit(1), 1) | ||
_ = limiter.WaitN(ctx,1) | ||
require.Nil(t, err) | ||
err = limiter.WaitN(ctx,1) | ||
require.NotNil(t, err) | ||
|
||
|
||
|
||
|
||
} | ||
|
||
func TestCopyWithNoLimit(t *testing.T) { | ||
|
||
src_store_dir := t.TempDir() | ||
|
||
src_store, err := NewLocalStore(src_store_dir, StoreOptions{}) | ||
require.NoError(t, err) | ||
throttleOptions := ThrottleOptions{100,100,time.Millisecond*10000} | ||
throttledStore := NewRateLimitedLocalStore(src_store, throttleOptions) | ||
|
||
chunk_data := []byte("some data") | ||
chunk := NewChunk(chunk_data) | ||
chunk_id := chunk.ID() | ||
|
||
err = throttledStore.StoreChunk(chunk) | ||
require.NotNil(t,err) | ||
hasChunk, err := throttledStore.HasChunk(chunk_id) | ||
require.NotNil(t,err) | ||
require.True(t,hasChunk) | ||
|
||
|
||
} |