diff --git a/rate_limit.go b/rate_limit.go deleted file mode 100644 index 3f416b7..0000000 --- a/rate_limit.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "sync" - "time" - - "github.com/labstack/echo" -) - -const ( - rateLimit = 20 // times per rateLimitInterval - rateLimitInterval = 1 * time.Hour -) - -var accesses = &sync.Map{} - -type access struct { - count int - timestamp time.Time -} - -func legitAccess(c echo.Context) bool { - ip := c.Request().RemoteAddr - aRaw, found := accesses.Load(ip) - var a *access - if found { - a, _ = aRaw.(*access) - } else { - a = &access{} - } - a.count++ - a.timestamp = time.Now() - accesses.Store(ip, a) - return a.count < rateLimit -} - -func cleanAccessRegistry(logger echo.Logger) { - for { - time.Sleep(rateLimitInterval) - t, e := 0, 0 - accesses.Range(func(ip, aRaw interface{}) bool { - t++ - a, _ := aRaw.(*access) - if a.timestamp.Add(rateLimitInterval).Before(time.Now()) { - accesses.Delete(ip) - e++ - } - return true - }) - if e > 0 { - logger.Infof("cleaned up %d/%d outdated accesses", e, t) - } - } -} diff --git a/render.go b/render.go index 3b34410..728d195 100644 --- a/render.go +++ b/render.go @@ -20,7 +20,6 @@ var ( 403: "Forbidden", 404: "Not found", 412: "Precondition failed", - 429: "Too many requests", 503: "Service unavailable", } diff --git a/server.go b/server.go index 46a73bd..633b6d7 100644 --- a/server.go +++ b/server.go @@ -51,7 +51,6 @@ func main() { } go persistStats(e.Logger, db) - go cleanAccessRegistry(e.Logger) e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))} @@ -110,11 +109,6 @@ func main() { code := http.StatusForbidden return c.Render(code, "Note", responsePage(code)) } - if !legitAccess(c) { - code := http.StatusTooManyRequests - c.Logger().Errorf("rate limit exceeded for %s", c.Request().RemoteAddr) - return c.Render(code, "Note", responsePage(code)) - } if c.FormValue("tos") != "on" { code := http.StatusPreconditionFailed c.Logger().Errorf("POST /note error: %d", code) @@ -152,7 +146,7 @@ func main() { e.POST("/:id/report", func(c echo.Context) error { report := c.FormValue("report") - if legitAccess(c) && report != "" { + if report != "" { id := c.Param("id") if err := email(id, report); err != nil { c.Logger().Errorf("couldn't send email: %v", err)