forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from longbridgeapp/feature/rate_limit
refactor rate limit
- Loading branch information
Showing
8 changed files
with
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,67 @@ | ||
package internal | ||
|
||
import ( | ||
"container/list" | ||
"sync" | ||
"time" | ||
|
||
"github.com/juju/ratelimit" | ||
) | ||
|
||
const ( | ||
OnceTakeCount = 1 | ||
DefaultFillInterval = time.Second | ||
) | ||
type token struct { | ||
enableTime time.Time | ||
} | ||
|
||
type RateLimiter struct { | ||
ratePerSecond int64 | ||
bucket *ratelimit.Bucket | ||
type LimitBucket struct { | ||
fillInterval time.Duration | ||
queue *list.List | ||
mutex sync.Mutex | ||
} | ||
|
||
func NewRateLimiter(ratePerSecond int64) *RateLimiter { | ||
rateLimiter := &RateLimiter{ | ||
ratePerSecond: ratePerSecond, | ||
func New(fillInterval time.Duration, limit uint64) *LimitBucket { | ||
bucket := &LimitBucket{ | ||
fillInterval: fillInterval, | ||
queue: list.New(), | ||
} | ||
if rateLimiter.RateLimitIsOpen() { | ||
rateLimiter.bucket = ratelimit.NewBucketWithQuantum(DefaultFillInterval, ratePerSecond, ratePerSecond) | ||
now := time.Now() | ||
for i := 0; i < int(limit); i++ { | ||
bucket.queue.PushBack(&token{ | ||
enableTime: now, | ||
}) | ||
} | ||
return rateLimiter | ||
return bucket | ||
} | ||
|
||
func (l *RateLimiter) RateLimitIsOpen() bool { | ||
return l.ratePerSecond > 0 | ||
func (bucket *LimitBucket) Wait() { | ||
for { | ||
if bucket.tryTakeToken() { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (bucket *LimitBucket) WaitForTimeout(timeout time.Duration) { | ||
begin := time.Now() | ||
for { | ||
if time.Now().After(begin.Add(timeout)) { | ||
return | ||
} | ||
if bucket.tryTakeToken() { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (l *RateLimiter) WaitRateLimit() { | ||
//if set ratePerSecond zero, not control send rate | ||
if !l.RateLimitIsOpen() { | ||
return | ||
func (bucket *LimitBucket) tryTakeToken() bool { | ||
bucket.mutex.Lock() | ||
defer bucket.mutex.Unlock() | ||
front := bucket.queue.Front() | ||
fristToken := front.Value.(*token) | ||
now := time.Now() | ||
if now.After(fristToken.enableTime) { | ||
bucket.queue.Remove(front) | ||
bucket.queue.PushBack(&token{ | ||
enableTime: now.Add(bucket.fillInterval), | ||
}) | ||
return true | ||
} | ||
l.bucket.Wait(OnceTakeCount) | ||
return false | ||
} |
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,103 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestWaitNormal(t *testing.T) { | ||
bucket := New(time.Second*1, 2) | ||
|
||
for i := 0; i < 10; i++ { | ||
bucket.Wait() | ||
t.Log(time.Now(), "|", i) | ||
} | ||
} | ||
|
||
func TestWaitForTimeout(t *testing.T) { | ||
bucket := New(time.Second*3, 3) | ||
|
||
for i := 0; i < 10; i++ { | ||
bucket.WaitForTimeout(time.Second) | ||
t.Log(time.Now(), "|", i) | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
bucket.WaitForTimeout(time.Second * 5) | ||
t.Log(time.Now(), "|", i) | ||
} | ||
} | ||
|
||
func TestWaitRandom(t *testing.T) { | ||
bucket := New(time.Second*1, 3) | ||
for i := 0; i < 10; i++ { | ||
bucket.Wait() | ||
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) | ||
t.Log(time.Now(), "|", i) | ||
} | ||
} | ||
|
||
// func TestWaitConcurrenyOld(t *testing.T) { | ||
// bucket := NewRateLimiter(8) | ||
|
||
// for i := 0; i < 5; i++ { | ||
// bucket.WaitRateLimit() | ||
// t.Log("1:", time.Now(), "|", i) | ||
// } | ||
// time.Sleep(time.Millisecond * 900) | ||
// go func() { | ||
// for i := 0; i < 55; i++ { | ||
// bucket.WaitRateLimit() | ||
// t.Log("2:", time.Now(), "|", i) | ||
// } | ||
// }() | ||
|
||
// time.Sleep(time.Second * 30) | ||
// } | ||
|
||
func TestWaitConcurreny1(t *testing.T) { | ||
bucket := New(time.Second, 8) | ||
|
||
for i := 0; i < 5; i++ { | ||
bucket.Wait() | ||
t.Log("1:", time.Now(), "|", i) | ||
} | ||
time.Sleep(time.Millisecond * 900) | ||
go func() { | ||
for i := 0; i < 55; i++ { | ||
bucket.Wait() | ||
t.Log("1:", time.Now(), "|", i) | ||
} | ||
}() | ||
|
||
time.Sleep(time.Second * 30) | ||
} | ||
|
||
func write(messageOut chan string) { | ||
for { | ||
text := <-messageOut | ||
fmt.Println(time.Now(), text) | ||
time.Sleep(time.Millisecond * time.Duration(100)) | ||
} | ||
} | ||
|
||
func TestWaitConcurreny2(t *testing.T) { | ||
bucket := New(time.Second*2, 5) | ||
messageOut := make(chan string) | ||
go write(messageOut) | ||
go func() { | ||
for i := 0; i < 20; i++ { | ||
bucket.Wait() | ||
messageOut <- fmt.Sprintf("No.1 Worker, task:%v", i) | ||
} | ||
}() | ||
go func() { | ||
for i := 0; i < 20; i++ { | ||
bucket.Wait() | ||
messageOut <- fmt.Sprintf("No.2 Worker, task:%v", i) | ||
} | ||
}() | ||
time.Sleep(time.Second * 30) | ||
} |
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