-
Notifications
You must be signed in to change notification settings - Fork 523
Limit number of simultaneous REST connections #3326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c5985f5
3ad93cf
af9989e
49f3f25
0a4571e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (C) 2019-2022 Algorand, Inc. | ||
| // This file is part of go-algorand | ||
| // | ||
| // go-algorand is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // go-algorand is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package middlewares_test | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/labstack/echo/v4" | ||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/algorand/go-algorand/daemon/algod/api/server/lib/middlewares" | ||
| "github.com/algorand/go-algorand/test/partitiontest" | ||
| ) | ||
|
|
||
| func TestConnectionLimiterBasic(t *testing.T) { | ||
| partitiontest.PartitionTest(t) | ||
|
|
||
| e := echo.New() | ||
|
|
||
| handlerCh := make(chan struct{}) | ||
| limit := 5 | ||
| handler := func(c echo.Context) error { | ||
| <-handlerCh | ||
| return c.String(http.StatusOK, "test") | ||
| } | ||
| middleware := middlewares.MakeConnectionLimiter(uint64(limit)) | ||
|
|
||
| numConnections := 13 | ||
| for i := 0; i < 3; i++ { | ||
| var recorders []*httptest.ResponseRecorder | ||
| doneCh := make(chan int) | ||
| errCh := make(chan error) | ||
|
|
||
| for index := 0; index < numConnections; index++ { | ||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| rec := httptest.NewRecorder() | ||
| ctx := e.NewContext(req, rec) | ||
|
|
||
| recorders = append(recorders, rec) | ||
|
|
||
| go func(index int) { | ||
| err := middleware(handler)(ctx) | ||
| doneCh <- index | ||
| errCh <- err | ||
| }(index) | ||
| } | ||
|
|
||
| // Check http 429 code. | ||
| for j := 0; j < numConnections-limit; j++ { | ||
| index := <-doneCh | ||
| assert.Equal(t, http.StatusTooManyRequests, recorders[index].Code) | ||
| } | ||
|
|
||
| // Let handlers finish. | ||
| for j := 0; j < limit; j++ { | ||
| handlerCh <- struct{}{} | ||
| } | ||
|
|
||
| // All other connections must return 200. | ||
| for j := 0; j < limit; j++ { | ||
| index := <-doneCh | ||
| assert.Equal(t, http.StatusOK, recorders[index].Code) | ||
| } | ||
|
|
||
| // Check that no errors were returned by the middleware. | ||
| for i := 0; i < numConnections; i++ { | ||
| assert.NoError(t, <-errCh) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestConnectionLimiterForwardsError(t *testing.T) { | ||
| partitiontest.PartitionTest(t) | ||
|
|
||
| handlerError := errors.New("handler error") | ||
| handler := func(c echo.Context) error { | ||
| return handlerError | ||
| } | ||
| middleware := middlewares.MakeConnectionLimiter(1) | ||
|
|
||
| err := middleware(handler)(nil) | ||
| assert.ErrorIs(t, err, handlerError) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ package algod | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net" | ||
|
|
@@ -35,10 +36,13 @@ import ( | |
| "github.com/algorand/go-algorand/config" | ||
| apiServer "github.com/algorand/go-algorand/daemon/algod/api/server" | ||
| "github.com/algorand/go-algorand/daemon/algod/api/server/lib" | ||
| "github.com/algorand/go-algorand/data/basics" | ||
| "github.com/algorand/go-algorand/data/bookkeeping" | ||
| "github.com/algorand/go-algorand/logging" | ||
| "github.com/algorand/go-algorand/logging/telemetryspec" | ||
| "github.com/algorand/go-algorand/network/limitlistener" | ||
| "github.com/algorand/go-algorand/node" | ||
| "github.com/algorand/go-algorand/util" | ||
| "github.com/algorand/go-algorand/util/metrics" | ||
| "github.com/algorand/go-algorand/util/tokens" | ||
| ) | ||
|
|
@@ -84,6 +88,34 @@ func (s *Server) Initialize(cfg config.Local, phonebookAddresses []string, genes | |
| s.log.SetLevel(logging.Level(cfg.BaseLoggerDebugLevel)) | ||
| setupDeadlockLogger() | ||
|
|
||
| // Check some config parameters. | ||
| if cfg.RestConnectionsSoftLimit > cfg.RestConnectionsHardLimit { | ||
| s.log.Warnf( | ||
| "RestConnectionsSoftLimit %d exceeds RestConnectionsHardLimit %d", | ||
| cfg.RestConnectionsSoftLimit, cfg.RestConnectionsHardLimit) | ||
| cfg.RestConnectionsSoftLimit = cfg.RestConnectionsHardLimit | ||
| } | ||
| if cfg.IncomingConnectionsLimit < 0 { | ||
| return fmt.Errorf( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will break algod startup for people who have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but for security reasons we need to limit this value. If it breaks somebody's setup, I think it's ok.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that it's reasonable to "break" a configuration that should not have existed to start with. |
||
| "Initialize() IncomingConnectionsLimit %d must be non-negative", | ||
| cfg.IncomingConnectionsLimit) | ||
| } | ||
|
|
||
| // Set large enough soft file descriptors limit. | ||
| var ot basics.OverflowTracker | ||
| fdRequired := ot.Add( | ||
| cfg.ReservedFDs, | ||
| ot.Add(uint64(cfg.IncomingConnectionsLimit), cfg.RestConnectionsHardLimit)) | ||
| if ot.Overflowed { | ||
| return errors.New( | ||
| "Initialize() overflowed when adding up ReservedFDs, IncomingConnectionsLimit " + | ||
| "RestConnectionsHardLimit; decrease them") | ||
| } | ||
| err = util.SetFdSoftLimit(fdRequired) | ||
| if err != nil { | ||
| return fmt.Errorf("Initialize() err: %w", err) | ||
tsachiherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // configure the deadlock detector library | ||
| switch { | ||
| case cfg.DeadlockDetection > 0: | ||
|
|
@@ -192,11 +224,12 @@ func (s *Server) Start() { | |
| } | ||
|
|
||
| listener, err := makeListener(addr) | ||
|
|
||
| if err != nil { | ||
| fmt.Printf("Could not start node: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| listener = limitlistener.RejectingLimitListener( | ||
| listener, cfg.RestConnectionsHardLimit, s.log) | ||
|
|
||
| addr = listener.Addr().String() | ||
| server = http.Server{ | ||
|
|
@@ -205,9 +238,9 @@ func (s *Server) Start() { | |
| WriteTimeout: time.Duration(cfg.RestWriteTimeoutSeconds) * time.Second, | ||
| } | ||
|
|
||
| tcpListener := listener.(*net.TCPListener) | ||
|
|
||
| e := apiServer.NewRouter(s.log, s.node, s.stopping, apiToken, adminAPIToken, tcpListener) | ||
| e := apiServer.NewRouter( | ||
| s.log, s.node, s.stopping, apiToken, adminAPIToken, listener, | ||
| cfg.RestConnectionsSoftLimit) | ||
|
|
||
| // Set up files for our PID and our listening address | ||
| // before beginning to listen to prevent 'goal node start' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Copyright 2014 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| //go:build !aix && !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris && !windows | ||
| // +build !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!windows | ||
|
|
||
| package limitlistener_test | ||
|
|
||
| func maxOpenFiles() int { | ||
| return defaultMaxOpenFiles | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright 2015 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris | ||
| // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris | ||
|
|
||
| package limitlistener_test | ||
|
|
||
| import "syscall" | ||
|
|
||
| func maxOpenFiles() int { | ||
| var rlim syscall.Rlimit | ||
| if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil { | ||
| return defaultMaxOpenFiles | ||
| } | ||
| return int(rlim.Cur) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Copyright 2015 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package limitlistener_test | ||
|
|
||
| func maxOpenFiles() int { | ||
| return 4 * defaultMaxOpenFiles /* actually it's 16581375 */ | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.