Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
henrod committed May 23, 2019
1 parent 7ddaab8 commit 62740d5
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
67 changes: 67 additions & 0 deletions acceptor/rate_limiting_conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package acceptor

import (
"container/list"
"net"
"time"

"github.com/topfreegames/pitaya/constants"
"github.com/topfreegames/pitaya/errors"
)

// RateLimitingConn ...
type RateLimitingConn struct {
net.Conn
limit int
interval time.Duration
times list.List
}

// NewRateLimitingConn returns an initialized *RateLimiting
func NewRateLimitingConn(conn net.Conn, limit int, interval time.Duration) *RateLimitingConn {
r := &RateLimitingConn{
Conn: conn,
limit: limit,
interval: interval,
}

r.times.Init()

return r
}

func (r *RateLimitingConn) Read(b []byte) (n int, err error) {
n, err = r.Conn.Read(b)
if err != nil {
return n, err
}

now := time.Now()
err = r.take(now)
if err != nil {
// TODOhenrod: log here
return 0, nil
}

// TODOhenrod: rate limit here
return n, err
}

// 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 {
if r.times.Len() < r.limit {
r.times.PushBack(now)
return nil
}

front := r.times.Front()
if diff := now.Sub(front.Value.(time.Time)); diff < r.interval {
err := constants.ErrExceededRateLimiting
return errors.NewError(err, errors.ErrTooManyRequests)
}

front.Value = now
r.times.MoveToBack(front)
return nil
}
38 changes: 38 additions & 0 deletions acceptor/rate_limiting_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package acceptor

import (
"net"
"time"
)

// RateLimitingWrapper ...
type RateLimitingWrapper struct {
Acceptor
connChan chan net.Conn
}

// NewRateLimitingWrapper ...
func NewRateLimitingWrapper() *RateLimitingWrapper {
return &RateLimitingWrapper{
connChan: make(chan net.Conn),
}
}

// Wrap ...
func (r *RateLimitingWrapper) Wrap(a Acceptor) Acceptor {
r.Acceptor = a
return r
}

// GetConnChan ...
func (r *RateLimitingWrapper) GetConnChan() chan net.Conn {
// TODOhenrod: move this to a better place
go func() {
for conn := range r.Acceptor.GetConnChan() {
// TODOhenrod: get this from config
r.connChan <- NewRateLimitingConn(conn, 1, time.Minute)
}
}()

return r.connChan
}
28 changes: 28 additions & 0 deletions acceptor/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package acceptor

type Wrapper interface {
Wrap(Acceptor) Acceptor
}

// WithWrapper ...
func WithWrapper(a Acceptor) Acceptor { return nil }

0 comments on commit 62740d5

Please sign in to comment.