diff --git a/block.go b/block.go index c8f4794..6191545 100644 --- a/block.go +++ b/block.go @@ -22,7 +22,7 @@ func agentBlock(next http.HandlerFunc) http.HandlerFunc { } { if strings.Contains(ua, bua) { // log.Debug().Str("ua", ua).Msg("user-agent blocked") - http.Error(w, "Forbidden", http.StatusForbidden) + http.Error(w, "", http.StatusForbidden) return } } @@ -69,7 +69,7 @@ func ipBlock(next http.HandlerFunc) http.HandlerFunc { for _, ipnet := range ranges { if ipnet.Contains(ip) { log.Debug().Stringer("ip", ip).Msg("ip blocked") - http.Error(w, "Forbidden", http.StatusForbidden) + http.Error(w, "", http.StatusForbidden) return } } diff --git a/http_middleware.go b/http_middleware.go index e20cfba..e21f42a 100644 --- a/http_middleware.go +++ b/http_middleware.go @@ -43,7 +43,7 @@ func queueMiddleware(next http.HandlerFunc) http.HandlerFunc { } willQueue := false - for _, prefix := range []string{"/nevent1", "/image", "/naddr1", "/npub1", "/nprofile1", "/note1", "/embed"} { + for _, prefix := range []string{"/njump", "/nevent1", "/image", "/naddr1", "/npub1", "/nprofile1", "/note1", "/embed"} { if strings.HasPrefix(r.URL.Path, prefix) { willQueue = true break @@ -56,11 +56,19 @@ func queueMiddleware(next http.HandlerFunc) http.HandlerFunc { } qidx := stupidHash(r.URL.Path) - curr := concurrentRequests[qidx].Load() - isFirst := curr == 0 + // add 1 + count := concurrentRequests[qidx].Add(1) + isFirst := count == 1 + if count > 2 { + log.Debug().Uint32("count", count).Int("qidx", qidx).Msg("too many concurrent requests") - // add 1 and lock (or wait for the lock) - concurrentRequests[qidx].Add(1) + if count > 4 { + http.Error(w, "", http.StatusTooManyRequests) + return + } + } + + // lock (or wait for the lock) queue[qidx].Lock() if isFirst {