Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
henrod committed May 24, 2019
1 parent 342a214 commit 64c5eeb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ import (
"github.com/topfreegames/pitaya/metrics"
)

// RateLimitingConn wraps net.Conn by applying rate limiting
// RateLimiter wraps net.Conn by applying rate limiting
// and return empty if exceeded
type RateLimitingConn struct {
type RateLimiter struct {
net.Conn
limit int
interval time.Duration
times list.List
forceDisable bool
}

// NewRateLimitingConn returns an initialized *RateLimiting
func NewRateLimitingConn(
// NewRateLimiter returns an initialized *RateLimiting
func NewRateLimiter(
conn net.Conn,
limit int,
interval time.Duration,
forceDisable bool,
) *RateLimitingConn {
r := &RateLimitingConn{
) *RateLimiter {
r := &RateLimiter{
Conn: conn,
limit: limit,
interval: interval,
Expand All @@ -60,7 +60,7 @@ func NewRateLimitingConn(
return r
}

func (r *RateLimitingConn) Read(b []byte) (n int, err error) {
func (r *RateLimiter) Read(b []byte) (n int, err error) {
if r.forceDisable {
return r.Conn.Read(b)
}
Expand All @@ -85,7 +85,7 @@ func (r *RateLimitingConn) Read(b []byte) (n int, err error) {

// take saves the now as time taken or returns an error if
// in the limit of rate limiting
func (r *RateLimitingConn) take(now time.Time) error {
func (r *RateLimiter) take(now time.Time) error {
if r.times.Len() < r.limit {
r.times.PushBack(now)
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/topfreegames/pitaya/mocks"
)

func TestRateLimitingConnRead(t *testing.T) {
func TestRateLimiterRead(t *testing.T) {
t.Parallel()

var (
Expand All @@ -21,7 +21,7 @@ func TestRateLimitingConnRead(t *testing.T) {
errTest = errors.New("error")

mockConn *mocks.MockConn
r *RateLimitingConn
r *RateLimiter
)

tables := map[string]struct {
Expand Down Expand Up @@ -57,10 +57,13 @@ func TestRateLimitingConnRead(t *testing.T) {
assert.NoError(t, err)
}

// exceed after this call
mockConn.EXPECT().Read(buf).Return(10, nil)
// back to for begin, return error to leave for loop
mockConn.EXPECT().Read(buf).Return(0, errTest)
},
expected: 0, // exceed, so 0 even though mock read returned 10
err: nil,
expected: 0,
err: errTest,
},

"test_force_disable": {
Expand All @@ -85,7 +88,7 @@ func TestRateLimitingConnRead(t *testing.T) {
defer ctrl.Finish()
mockConn = mocks.NewMockConn(ctrl)

r = NewRateLimitingConn(mockConn, limit, interval, table.forceDisable)
r = NewRateLimiter(mockConn, limit, interval, table.forceDisable)

table.mock()
n, err := r.Read(buf)
Expand All @@ -95,14 +98,14 @@ func TestRateLimitingConnRead(t *testing.T) {
}
}

func TestRateLimitingConnTake(t *testing.T) {
func TestRateLimiterTake(t *testing.T) {
t.Parallel()

var (
limit = 3
interval = time.Second
now = time.Now()
r *RateLimitingConn
r *RateLimiter
)

tables := map[string]struct {
Expand Down Expand Up @@ -140,7 +143,7 @@ func TestRateLimitingConnTake(t *testing.T) {

for name, table := range tables {
t.Run(name, func(t *testing.T) {
r = NewRateLimitingConn(nil, limit, interval, false)
r = NewRateLimiter(nil, limit, interval, false)

table.before()
err := r.take(now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewRateLimitingWrapper(c *config.Config) *RateLimitingWrapper {
forceDisable = c.GetBool("pitaya.conn.ratelimiting.forcedisable")
)

return NewRateLimitingConn(conn, limit, interval, forceDisable)
return NewRateLimiter(conn, limit, interval, forceDisable)
})

return r
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ func TestNewRateLimitingWrapper(t *testing.T) {
}

rateLimitingWrapper := NewRateLimitingWrapper(getConfig())
expected := NewRateLimitingConn(nil, 20, time.Second, false)
expected := NewRateLimiter(nil, 20, time.Second, false)
assert.Equal(t, expected, rateLimitingWrapper.wrapConn(nil))
}

0 comments on commit 64c5eeb

Please sign in to comment.