From 7f18a3bac0d977c654634bbd8cafd3673648196d Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 8 Oct 2017 15:06:46 +1300 Subject: [PATCH] server: optional speed limit --- Server.go | 9 +++++++-- cmd/contented/main.go | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Server.go b/Server.go index 8bb4a34..5f60e53 100644 --- a/Server.go +++ b/Server.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/boltdb/bolt" + "github.com/mxk/go-flowrate/flowrate" ) var SERVER_HEADER string = `contented/0.1` @@ -19,8 +20,9 @@ type ServerPublicProperties struct { } type ServerOptions struct { - DataDirectory string - DBPath string + DataDirectory string + DBPath string + BandwidthLimit int64 ServerPublicProperties } @@ -119,6 +121,9 @@ func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if this.opts.MaxUploadBytes > 0 { r.Body = http.MaxBytesReader(w, r.Body, this.opts.MaxUploadBytes) } + if this.opts.BandwidthLimit > 0 { + r.Body = flowrate.NewReader(r.Body, this.opts.BandwidthLimit) + } if r.Method == "GET" && strings.HasPrefix(r.URL.Path, `/view/`) { err := this.handleView(w, r, r.URL.Path[6:]) diff --git a/cmd/contented/main.go b/cmd/contented/main.go index eb91f95..d231351 100644 --- a/cmd/contented/main.go +++ b/cmd/contented/main.go @@ -17,11 +17,14 @@ func main() { dbPath := flag.String("db", "contented.db", "Path for metadata database") appTitle := flag.String("title", "contented", "Title used in web interface") maxUploadMb := flag.Int("max", 8, "Maximum size of uploaded files in MiB (set zero for unlimited)") + maxUploadSpeed := flag.Int("speed", 0, "Maximum upload speed in bytes/sec (set zero for unlimited)") + flag.Parse() svr, err := contented.NewServer(&contented.ServerOptions{ - DataDirectory: *dataDir, - DBPath: *dbPath, + DataDirectory: *dataDir, + DBPath: *dbPath, + BandwidthLimit: int64(*maxUploadSpeed), ServerPublicProperties: contented.ServerPublicProperties{ AppTitle: *appTitle, MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024,