From e2475925ead1d1529ba4e3ae68ddb47634dc4417 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Fri, 24 Apr 2015 20:45:44 -0700 Subject: [PATCH] quota handlers now log against ip properly against a duration --- pkg/api/api_generic_handlers.go | 13 ----- pkg/api/api_router.go | 3 +- pkg/api/quota/quota_handler.go | 84 +++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 pkg/api/quota/quota_handler.go diff --git a/pkg/api/api_generic_handlers.go b/pkg/api/api_generic_handlers.go index 8933047ec..f7707cd61 100644 --- a/pkg/api/api_generic_handlers.go +++ b/pkg/api/api_generic_handlers.go @@ -145,16 +145,3 @@ func ignoreUnImplementedObjectResources(req *http.Request) bool { } return false } - -type quotaHandler struct { - handler http.Handler -} - -func (h quotaHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - h.handler.ServeHTTP(w, req) -} - -// QuotaHandler implements quotas -func QuotaHandler(h http.Handler) http.Handler { - return quotaHandler{handler: h} -} diff --git a/pkg/api/api_router.go b/pkg/api/api_router.go index fa2fcef44..233656a1c 100644 --- a/pkg/api/api_router.go +++ b/pkg/api/api_router.go @@ -22,6 +22,7 @@ import ( router "github.com/gorilla/mux" "github.com/minio-io/minio/pkg/api/config" + "github.com/minio-io/minio/pkg/api/quota" "github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/storage/drivers" ) @@ -91,5 +92,5 @@ func HTTPHandler(domain string, driver drivers.Driver) http.Handler { h := validateHandler(conf, ignoreResourcesHandler(mux)) // quota handler is always last - return QuotaHandler(h) + return quota.Handler(h, int64(100*1024*1024)) } diff --git a/pkg/api/quota/quota_handler.go b/pkg/api/quota/quota_handler.go new file mode 100644 index 000000000..82d547a75 --- /dev/null +++ b/pkg/api/quota/quota_handler.go @@ -0,0 +1,84 @@ +/* + * Minimalist Object Storage, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package quota + +import ( + "log" + "net/http" + "strconv" + "strings" + "sync" + "time" +) + +// map[minute][address] = current quota +type quotaMap struct { + sync.RWMutex + data map[int64]map[uint32]uint64 + limit uint64 + duration int64 +} + +func (q *quotaMap) Add(ip uint32, size uint64) bool { + currentMinute := time.Now().Unix() / q.duration + expiredQuotas := (time.Now().Unix() / q.duration) - 5 + for time := range q.data { + if time < expiredQuotas { + delete(q.data, time) + } + } + log.Println(currentMinute) + if _, ok := q.data[currentMinute]; !ok { + q.data[currentMinute] = make(map[uint32]uint64) + } + currentData, _ := q.data[currentMinute][ip] + q.data[currentMinute][ip] = currentData + size + return false +} + +// HttpQuotaHandler +type httpQuotaHandler struct { + handler http.Handler + quotas *quotaMap +} + +// ServeHTTP is an http.Handler ServeHTTP method +func (h *httpQuotaHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + ipString := strings.Split(req.RemoteAddr, ":")[0] + ipSplit := strings.Split(ipString, ".") + q0, _ := strconv.Atoi(ipSplit[0]) + q1, _ := strconv.Atoi(ipSplit[1]) + q2, _ := strconv.Atoi(ipSplit[2]) + q3, _ := strconv.Atoi(ipSplit[3]) + longIP := uint32(q0)<<24 + uint32(q1)<<16 + uint32(q2)<<8 + uint32(q3) + h.quotas.Add(longIP, uint64(req.ContentLength)) + log.Println("quota called") + log.Println(h.quotas) + h.handler.ServeHTTP(w, req) +} + +// Handler implements quotas +func Handler(h http.Handler, limit int64) http.Handler { + return &httpQuotaHandler{ + handler: h, + quotas: "aMap{ + data: make(map[int64]map[uint32]uint64), + limit: uint64(limit), + duration: int64(60), + }, + } +}