diff options
author | Antoni Sawicki <tenox@google.com> | 2022-06-21 23:50:36 -0700 |
---|---|---|
committer | Antoni Sawicki <tenox@google.com> | 2022-06-21 23:50:36 -0700 |
commit | 080d67a953f72ed6b11768cb96e4ac867ae306ca (patch) | |
tree | 619618d029429fb081e61bdd7aa2687aee81dbda | |
parent | 1f6cd26f68602e2832df9316f86e92800317e8e8 (diff) | |
download | wfm-080d67a953f72ed6b11768cb96e4ac867ae306ca.tar.gz |
rate limit bucket is global
-rw-r--r-- | fileio.go | 8 | ||||
-rw-r--r-- | wfm.go | 4 |
2 files changed, 10 insertions, 2 deletions
@@ -16,6 +16,10 @@ import ( "github.com/juju/ratelimit" ) +var ( + rlBu *ratelimit.Bucket +) + func (r *wfmRequest) dispFile() { fp := r.uDir + "/" + r.uFbn s := strings.Split(fp, ".") @@ -99,7 +103,7 @@ func streamFile(w http.ResponseWriter, uFilePath string) { var r io.Reader = fi if *rateLim != 0 { - r = ratelimit.Reader(fi, ratelimit.NewBucketWithRate(float64(*rateLim<<20), 1<<10)) + r = ratelimit.Reader(fi, rlBu) } _, err = io.Copy(w, r) @@ -124,7 +128,7 @@ func (r *wfmRequest) uploadFile(h *multipart.FileHeader, f multipart.File) { var w io.Writer = fi if *rateLim != 0 { - w = ratelimit.Writer(fi, ratelimit.NewBucketWithRate(float64(*rateLim<<20), 1<<10)) + w = ratelimit.Writer(fi, rlBu) } oSize, err := io.Copy(w, f) @@ -15,6 +15,7 @@ import ( "syscall" _ "github.com/breml/rootcerts" + "github.com/juju/ratelimit" "golang.org/x/crypto/acme/autocert" ) @@ -157,6 +158,9 @@ func main() { } log.Printf("Setuid UID=%d GID=%d", os.Geteuid(), os.Getgid()) + // rate limit setup + rlBu = ratelimit.NewBucketWithRate(float64(*rateLim<<20), 1<<10) + // http stuff mux := http.NewServeMux() mux.HandleFunc(*wfmPfx, wfmMain) |