Skip to content

Commit

Permalink
Add request token sending counter
Browse files Browse the repository at this point in the history
Signed-off-by: ii2day <ji.li@daocloud.io>
  • Loading branch information
ii2day committed Nov 27, 2023
1 parent 2e624fc commit c8dc481
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/loadRequest/loadDns/dns_requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,23 @@ func (b *Work) Run() {

// Send qps number of tokens to the channel qosTokenBucket every second to the coroutine for execution
go func() {
// Request token counter to avoid issuing multiple tokens due to errors
requestRound := 0

c := time.After(time.Duration(b.RequestTimeSecond) * time.Second)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
// The request should be sent immediately at 0 seconds
for i := 0; i < b.QPS; i++ {
b.qosTokenBucket <- struct{}{}
}
requestRound++

b.Logger.Sugar().Debugf("request token channel len: %d", len(b.qosTokenBucket))
for {
select {
case <-c:
b.Logger.Sugar().Debugf("reach request duration time, stop request")
// Reach request duration stop request
if len(b.qosTokenBucket) > 0 {
b.Logger.Sugar().Errorf("request finish remaining number of tokens len: %d", len(b.qosTokenBucket))
Expand All @@ -118,10 +124,15 @@ func (b *Work) Run() {
b.Stop()
return
case <-ticker.C:
if requestRound >= b.RequestTimeSecond {
b.Logger.Sugar().Debugf("All request tokens have been sent and will not be sent again.")
continue

Check warning on line 129 in pkg/loadRequest/loadDns/dns_requester.go

View check run for this annotation

Codecov / codecov/patch

pkg/loadRequest/loadDns/dns_requester.go#L128-L129

Added lines #L128 - L129 were not covered by tests
}
b.Logger.Sugar().Debugf("request token channel len: %d", len(b.qosTokenBucket))
for i := 0; i < b.QPS; i++ {
b.qosTokenBucket <- struct{}{}
}
requestRound++
}
}
}()
Expand Down
12 changes: 12 additions & 0 deletions pkg/loadRequest/loadHttp/http_requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,24 @@ func (b *Work) Run() {

// Send qps number of tokens to the channel qosTokenBucket every second to the coroutine for execution
go func() {
// Request token counter to avoid issuing multiple tokens due to errors
requestRound := 0

c := time.After(time.Duration(b.RequestTimeSecond) * time.Second)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()

// The request should be sent immediately at 0 seconds
for i := 0; i < b.QPS; i++ {
b.qosTokenBucket <- struct{}{}
}
requestRound++

b.Logger.Sugar().Debugf("request token channel len: %d", len(b.qosTokenBucket))
for {
select {
case <-c:
b.Logger.Sugar().Debugf("reach request duration time, stop request")
// Wait for the last request to return
time.Sleep(time.Duration(b.Timeout) * time.Millisecond)
// Reach request duration stop request
Expand All @@ -203,10 +210,15 @@ func (b *Work) Run() {
b.Stop()
return
case <-ticker.C:
if requestRound >= b.RequestTimeSecond {
b.Logger.Sugar().Debugf("All request tokens have been sent and will not be sent again.")
continue

Check warning on line 215 in pkg/loadRequest/loadHttp/http_requester.go

View check run for this annotation

Codecov / codecov/patch

pkg/loadRequest/loadHttp/http_requester.go#L214-L215

Added lines #L214 - L215 were not covered by tests
}
b.Logger.Sugar().Debugf("request token channel len: %d", len(b.qosTokenBucket))
for i := 0; i < b.QPS; i++ {
b.qosTokenBucket <- struct{}{}
}
requestRound++
}
}
}()
Expand Down

0 comments on commit c8dc481

Please sign in to comment.