server: optional speed limit

This commit is contained in:
mappu 2017-10-08 15:06:46 +13:00
parent 2345447d59
commit 7f18a3bac0
2 changed files with 12 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/mxk/go-flowrate/flowrate"
) )
var SERVER_HEADER string = `contented/0.1` var SERVER_HEADER string = `contented/0.1`
@ -21,6 +22,7 @@ type ServerPublicProperties struct {
type ServerOptions struct { type ServerOptions struct {
DataDirectory string DataDirectory string
DBPath string DBPath string
BandwidthLimit int64
ServerPublicProperties ServerPublicProperties
} }
@ -119,6 +121,9 @@ func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if this.opts.MaxUploadBytes > 0 { if this.opts.MaxUploadBytes > 0 {
r.Body = http.MaxBytesReader(w, r.Body, this.opts.MaxUploadBytes) 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/`) { if r.Method == "GET" && strings.HasPrefix(r.URL.Path, `/view/`) {
err := this.handleView(w, r, r.URL.Path[6:]) err := this.handleView(w, r, r.URL.Path[6:])

View File

@ -17,11 +17,14 @@ func main() {
dbPath := flag.String("db", "contented.db", "Path for metadata database") dbPath := flag.String("db", "contented.db", "Path for metadata database")
appTitle := flag.String("title", "contented", "Title used in web interface") 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)") 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() flag.Parse()
svr, err := contented.NewServer(&contented.ServerOptions{ svr, err := contented.NewServer(&contented.ServerOptions{
DataDirectory: *dataDir, DataDirectory: *dataDir,
DBPath: *dbPath, DBPath: *dbPath,
BandwidthLimit: int64(*maxUploadSpeed),
ServerPublicProperties: contented.ServerPublicProperties{ ServerPublicProperties: contented.ServerPublicProperties{
AppTitle: *appTitle, AppTitle: *appTitle,
MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024, MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024,