Skip to content

Commit

Permalink
[req] Make 'Limiter' public
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 13, 2024
1 parent 88d78a1 commit 96ae9b0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### [13.11.1](https://kaos.sh/ek/13.11.1)

* `[req]` Make `Limiter` public

### [13.11.0](https://kaos.sh/ek/13.11.0)

* `[req]` Added request limiter
Expand Down
12 changes: 6 additions & 6 deletions req/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import "time"

// ////////////////////////////////////////////////////////////////////////////////// //

// limiter is request limiter
type limiter struct {
// Limiter is request limiter
type Limiter struct {
lastCall time.Time
delay time.Duration
}

// ////////////////////////////////////////////////////////////////////////////////// //

// createLimiter creates new limiter
func createLimiter(rps float64) *limiter {
// NewLimiter creates a new limiter. If rps is less than or equal to 0, it returns nil.
func NewLimiter(rps float64) *Limiter {
if rps <= 0 {
return nil
}

return &limiter{
return &Limiter{
delay: time.Duration(float64(time.Second) / rps),
}
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Wait blocks current goroutine execution until next time slot become available
func (l *limiter) Wait() {
func (l *Limiter) Wait() {
if l == nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions req/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ type Engine struct {
Transport *http.Transport // Transport is default transport struct
Client *http.Client // Client is default client struct

limiter *limiter // Request limiter
limiter *Limiter // Request limiter
dialTimeout float64 // dialTimeout is dial timeout in seconds
requestTimeout float64 // requestTimeout is request timeout in seconds

Expand Down Expand Up @@ -437,7 +437,7 @@ func (e *Engine) SetLimit(rps float64) {
return
}

e.limiter = createLimiter(rps)
e.limiter = NewLimiter(rps)
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
4 changes: 2 additions & 2 deletions req/req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ func (s *ReqSuite) TestQueryEncoding(c *C) {
}

func (s *ReqSuite) TestLimiter(c *C) {
var l *limiter
var l *Limiter

c.Assert(createLimiter(0.0), IsNil)
c.Assert(NewLimiter(0.0), IsNil)

l.Wait()
}
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package ek
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "13.11.0"
const VERSION = "13.11.1"

0 comments on commit 96ae9b0

Please sign in to comment.