Browse Source

Optimization

Volodymyr Tkach 2 years ago
parent
commit
d43b05700c
2 changed files with 7 additions and 4 deletions
  1. 6 0
      utils/http/servauth/requests.go
  2. 1 4
      utils/http/servauth/servauth.go

+ 6 - 0
utils/http/servauth/requests.go

@@ -49,3 +49,9 @@ func (r *Requests) Cleanup() {
 	r.lastTime = map[string]int64{}
 	r.lastTime = map[string]int64{}
 	r.cleanTime = time.Now().UTC().Unix()
 	r.cleanTime = time.Now().UTC().Unix()
 }
 }
+
+func (r *Requests) CleanupHourly() {
+	if (time.Now().UTC().Unix() - r.cleanTime) > 3600 {
+		r.Cleanup()
+	}
+}

+ 1 - 4
utils/http/servauth/servauth.go

@@ -17,10 +17,7 @@ var mRequests = &Requests{
 func BasicAuth(handler http.Handler, username, password, realm string) http.Handler {
 func BasicAuth(handler http.Handler, username, password, realm string) http.Handler {
 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		if username != "" {
 		if username != "" {
-			// Cleanup every hour
-			if (time.Now().UTC().Unix() - mRequests.cleanTime) > 3600 {
-				mRequests.Cleanup()
-			}
+			mRequests.CleanupHourly()
 
 
 			ip := helpers.ClientIP(r)
 			ip := helpers.ClientIP(r)
 			reqs := mRequests.Count(ip)
 			reqs := mRequests.Count(ip)