Commit cf4047ce authored by Christopher Schinnerl's avatar Christopher Schinnerl

move global limits out of gateway

parent ef03dc42
Pipeline #40702977 passed with stages
in 28 minutes and 9 seconds
......@@ -163,9 +163,6 @@ type (
// supply the given RPC ID.
RegisterRPC(string, RPCFunc)
// SetGlobalRateLimits changes the global rate limits for siad.
SetGlobalRateLimits(downloadSpeed, uploadSpeed int64) error
// RateLimits returns the currently set bandwidth limits of the gateway.
RateLimits() (int64, int64)
......
......@@ -228,20 +228,6 @@ func (g *Gateway) RateLimits() (int64, int64) {
return g.persist.MaxDownloadSpeed, g.persist.MaxUploadSpeed
}
// SetGlobalRateLimits changes the rate limits for all connections of siad.
func (g *Gateway) SetGlobalRateLimits(downloadSpeed, uploadSpeed int64) error {
g.mu.Lock()
defer g.mu.Unlock()
// Set the limit in memory.
if err := setRateLimits(modules.GlobalRateLimits, downloadSpeed, uploadSpeed); err != nil {
return err
}
// Update the persistence struct.
g.persist.GlobalMaxDownloadSpeed = downloadSpeed
g.persist.GlobalMaxUploadSpeed = uploadSpeed
return g.saveSync()
}
// SetRateLimits changes the rate limits for the peer-connections of the
// gateway.
func (g *Gateway) SetRateLimits(downloadSpeed, uploadSpeed int64) error {
......@@ -327,9 +313,6 @@ func New(addr string, bootstrap bool, persistDir string) (*Gateway, error) {
if err := setRateLimits(g.rl, g.persist.MaxDownloadSpeed, g.persist.MaxUploadSpeed); err != nil {
return nil, err
}
if err := setRateLimits(modules.GlobalRateLimits, g.persist.GlobalMaxDownloadSpeed, g.persist.GlobalMaxUploadSpeed); err != nil {
return nil, err
}
// Spawn the thread to periodically save the gateway.
go g.threadedSaveLoop()
// Make sure that the gateway saves after shutdown.
......
......@@ -39,10 +39,8 @@ type (
RouterURL string
// rate limit settings
MaxDownloadSpeed int64
MaxUploadSpeed int64
GlobalMaxDownloadSpeed int64
GlobalMaxUploadSpeed int64
MaxDownloadSpeed int64
MaxUploadSpeed int64
}
)
......
package api
import (
"errors"
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
"gitlab.com/NebulousLabs/Sia/modules"
"gitlab.com/NebulousLabs/ratelimit"
)
// DaemonVersionGet contains information about the running daemon's version.
......@@ -55,11 +57,27 @@ func (api *API) daemonSettingsHandlerPOST(w http.ResponseWriter, req *http.Reque
}
maxUploadSpeed = uploadSpeed
}
// Try to set the new limits.
err := api.gateway.SetGlobalRateLimits(maxDownloadSpeed, maxUploadSpeed)
if err != nil {
WriteError(w, Error{"failed to set new global rate limit: " + err.Error()}, http.StatusBadRequest)
// Set the limit.
if err := setRateLimits(modules.GlobalRateLimits, maxDownloadSpeed, maxUploadSpeed); err != nil {
WriteError(w, Error{"unable to set limits: " + err.Error()}, http.StatusBadRequest)
return
}
WriteSuccess(w)
}
// setRateLimits sets the specified ratelimit after performing input
// validation without persisting them.
func setRateLimits(rl *ratelimit.RateLimit, downloadSpeed, uploadSpeed int64) error {
// Input validation.
if downloadSpeed < 0 || uploadSpeed < 0 {
return errors.New("download/upload rate can't be below 0")
}
// Check for sentinel "no limits" value.
if downloadSpeed == 0 && uploadSpeed == 0 {
rl.SetLimits(0, 0, 0)
} else {
rl.SetLimits(downloadSpeed, uploadSpeed, 4*4096)
}
// TODO persist settings.
return nil
}
......@@ -34,7 +34,6 @@ func (api *API) gatewayHandlerGET(w http.ResponseWriter, req *http.Request, _ ht
// gatewayHandlerPOST handles the API call changing gateway specific settings.
func (api *API) gatewayHandlerPOST(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
maxDownloadSpeed, maxUploadSpeed := api.gateway.RateLimits()
maxGlobalDownloadSpeed, maxGlobalUploadSpeed, _ := modules.GlobalRateLimits.Limits()
// Scan the download speed limit. (optional parameter)
if d := req.FormValue("maxdownloadspeed"); d != "" {
var downloadSpeed int64
......@@ -53,35 +52,12 @@ func (api *API) gatewayHandlerPOST(w http.ResponseWriter, req *http.Request, _ h
}
maxUploadSpeed = uploadSpeed
}
// Scan the download speed limit. (optional parameter)
if d := req.FormValue("maxglobaldownloadspeed"); d != "" {
var downloadSpeed int64
if _, err := fmt.Sscan(d, &downloadSpeed); err != nil {
WriteError(w, Error{"unable to parse downloadspeed: " + err.Error()}, http.StatusBadRequest)
return
}
maxGlobalDownloadSpeed = downloadSpeed
}
// Scan the upload speed limit. (optional parameter)
if u := req.FormValue("maxglobaluploadspeed"); u != "" {
var uploadSpeed int64
if _, err := fmt.Sscan(u, &uploadSpeed); err != nil {
WriteError(w, Error{"unable to parse uploadspeed: " + err.Error()}, http.StatusBadRequest)
return
}
maxGlobalUploadSpeed = uploadSpeed
}
// Try to set the limits.
err := api.gateway.SetRateLimits(maxDownloadSpeed, maxUploadSpeed)
if err != nil {
WriteError(w, Error{"failed to set new rate limit: " + err.Error()}, http.StatusBadRequest)
return
}
err = api.gateway.SetGlobalRateLimits(maxGlobalDownloadSpeed, maxGlobalUploadSpeed)
if err != nil {
WriteError(w, Error{"failed to set new global rate limit: " + err.Error()}, http.StatusBadRequest)
return
}
WriteSuccess(w)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment