Add rate limiter instead of connection limit for now

This commit is contained in:
Harshavardhana
2015-06-06 17:01:39 -07:00
parent 996c1a5c28
commit 371651bde6
7 changed files with 86 additions and 74 deletions

View File

@@ -16,74 +16,35 @@
package quota
import (
"net"
"net/http"
"sync"
"github.com/minio/minio/pkg/utils/log"
)
import "net/http"
// requestLimitHandler
type connLimit struct {
sync.RWMutex
handler http.Handler
connections map[uint32]int
limit int
handler http.Handler
connectionQueue chan bool
}
func (c *connLimit) IsLimitExceeded(ip uint32) bool {
if c.connections[ip] >= c.limit {
return true
}
return false
}
func (c *connLimit) GetUsed(ip uint32) int {
return c.connections[ip]
}
func (c *connLimit) Add(ip uint32) {
c.Lock()
defer c.Unlock()
count := c.connections[ip]
count = count + 1
c.connections[ip] = count
func (c *connLimit) Add() {
c.connectionQueue <- true
return
}
func (c *connLimit) Remove(ip uint32) {
c.Lock()
defer c.Unlock()
count, _ := c.connections[ip]
count = count - 1
if count <= 0 {
delete(c.connections, ip)
return
}
c.connections[ip] = count
func (c *connLimit) Remove() {
<-c.connectionQueue
return
}
// ServeHTTP is an http.Handler ServeHTTP method
func (c *connLimit) ServeHTTP(w http.ResponseWriter, req *http.Request) {
host, _, _ := net.SplitHostPort(req.RemoteAddr)
longIP := longIP{net.ParseIP(host)}.IptoUint32()
if c.IsLimitExceeded(longIP) {
hosts, _ := net.LookupAddr(uint32ToIP(longIP).String())
log.Debug.Printf("Connection limit reached - Host: %s, Total Connections: %d\n", hosts, c.GetUsed(longIP))
writeErrorResponse(w, req, ConnectionLimitExceeded, req.URL.Path)
return
}
c.Add(longIP)
defer c.Remove(longIP)
c.Add()
c.handler.ServeHTTP(w, req)
c.Remove()
}
// ConnectionLimit limits the number of concurrent connections
func ConnectionLimit(h http.Handler, limit int) http.Handler {
return &connLimit{
handler: h,
connections: make(map[uint32]int),
limit: limit,
handler: h,
connectionQueue: make(chan bool, limit),
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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 "net/http"
// rateLimit
type rateLimit struct {
handler http.Handler
rateQueue chan bool
}
func (c *rateLimit) Add() {
c.rateQueue <- true // fill in the queue
return
}
func (c *rateLimit) Remove() {
<-c.rateQueue // invalidate the queue, after the request is served
return
}
// ServeHTTP is an http.Handler ServeHTTP method
func (c *rateLimit) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c.Add() // add
c.handler.ServeHTTP(w, req) // serve
c.Remove() // remove
}
// RateLimit limits the number of concurrent http requests
func RateLimit(handle http.Handler, limit int) http.Handler {
return &rateLimit{
handler: handle,
rateQueue: make(chan bool, limit),
}
}