Skip to content

Commit

Permalink
Add rate-limiting support for the public proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
adewes committed Aug 18, 2021
1 parent 6238646 commit 1170e2b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 36 deletions.
4 changes: 3 additions & 1 deletion http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ func (s *HTTPServer) Start() error {
if s.listener == nil {
if listener, err := net.Listen("tcp", s.settings.BindAddress); err != nil {
return err
} else {
} else if s.settings.TCPRateLimits != nil {
s.listener = epsNet.MakeRateLimitedListener(listener, s.settings.TCPRateLimits)
} else {
s.listener = listener
}
}

Expand Down
1 change: 1 addition & 0 deletions net/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type RateLimitedListener struct {
}

func MakeRateLimitedListener(listener net.Listener, rateLimits []*RateLimit) *RateLimitedListener {
eps.Log.Tracef("Creating rate-limited network listener...")
rates := make([]map[string]int64, len(rateLimits))
for i, _ := range rateLimits {
rates[i] = make(map[string]int64)
Expand Down
30 changes: 0 additions & 30 deletions net/time_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,33 +144,3 @@ func (t *TimeWindow) Copy() TimeWindow {
To: t.To,
}
}

func (t *TimeWindow) IncreaseBy(n int64) {
from := time.Unix(t.From/1e9, t.From%1e9).UTC()
to := time.Unix(t.To/1e9, t.To%1e9).UTC()
switch t.Type {
case "second":
from = from.Add(time.Second * time.Duration(n))
to = to.Add(time.Second * time.Duration(n))
case "minute":
from = from.Add(time.Minute * time.Duration(n))
to = to.Add(time.Minute * time.Duration(n))
case "quarterHour":
from = from.Add(time.Minute * time.Duration(15*n))
to = to.Add(time.Minute * time.Duration(15*n))
case "hour":
from = from.Add(time.Hour * time.Duration(n))
to = to.Add(time.Hour * time.Duration(n))
case "day":
from = from.AddDate(0, 0, int(n))
to = to.AddDate(0, 0, int(n))
case "week":
from = from.AddDate(0, 0, 7*int(n))
to = to.AddDate(0, 0, 7*int(n))
case "month":
from = from.AddDate(0, int(n), 0)
to = to.AddDate(0, int(n), 0)
}
t.From = from.UnixNano()
t.To = to.UnixNano()
}
2 changes: 2 additions & 0 deletions proxy/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
epsForms "github.com/iris-connect/eps/forms"
"github.com/iris-connect/eps/jsonrpc"
"github.com/iris-connect/eps/net"
"github.com/iris-connect/eps/tls"
"github.com/kiprotect/go-helpers/forms"
"regexp"
Expand Down Expand Up @@ -184,6 +185,7 @@ var PublicSettingsForm = forms.Form{
forms.IsString{},
},
},
net.TCPRateLimitsField,
{
Name: "tls_bind_address",
Validators: []forms.Validator{
Expand Down
21 changes: 16 additions & 5 deletions proxy/public_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
epsForms "github.com/iris-connect/eps/forms"
"github.com/iris-connect/eps/helpers"
"github.com/iris-connect/eps/jsonrpc"
epsNet "github.com/iris-connect/eps/net"
"github.com/iris-connect/eps/tls"
"github.com/kiprotect/go-helpers/forms"
"net"
Expand Down Expand Up @@ -571,14 +572,24 @@ func (s *PublicServer) listenForInternalConnections() {

}

func (s *PublicServer) makeListener(address string) (net.Listener, error) {
if listener, err := net.Listen("tcp", address); err != nil {
return nil, err
} else if s.settings.TCPRateLimits != nil {
return epsNet.MakeRateLimitedListener(listener, s.settings.TCPRateLimits), nil
} else {
return listener, nil
}

}

func (s *PublicServer) Start() error {
var err error
s.tlsListener, err = net.Listen("tcp", s.settings.TLSBindAddress)
if err != nil {
return err
}

s.tlsListener, err = s.makeListener(s.settings.TLSBindAddress)
go s.listenForTlsConnections()
s.internalListener, err = net.Listen("tcp", s.settings.InternalBindAddress)

s.internalListener, err = s.makeListener(s.settings.InternalBindAddress)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions proxy/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package proxy
import (
"github.com/iris-connect/eps"
"github.com/iris-connect/eps/jsonrpc"
"github.com/iris-connect/eps/net"
"github.com/iris-connect/eps/tls"
"time"
)
Expand Down Expand Up @@ -47,6 +48,7 @@ type PublicServerSettings struct {
InternalEndpoint string `json:"internal_endpoint"`
JSONRPCClient *jsonrpc.JSONRPCClientSettings `json:"jsonrpc_client"`
JSONRPCServer *jsonrpc.JSONRPCServerSettings `json:"jsonrpc_server`
TCPRateLimits []*net.RateLimit `json:"tcp_rate_limits"`
}

type PublicAnnouncement struct {
Expand Down
3 changes: 3 additions & 0 deletions settings/dev/roles/public-proxy-1/001_default.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
public:
name: public-proxy-1.ga
tcp_rate_limits:
- type: minute
limit: 10
datastore:
type: redis
settings:
Expand Down

0 comments on commit 1170e2b

Please sign in to comment.