Merge pull request #540 from harshavardhana/pr_out_remove_connection_limit_handle_throttling_outisde_in_iptables

master
Harshavardhana 10 years ago
commit 50d871c2db
  1. 17
      pkg/api/quota/conn_limit.go
  2. 16
      pkg/api/quota/errors.go

@ -20,6 +20,8 @@ import (
"net" "net"
"net/http" "net/http"
"sync" "sync"
"github.com/minio-io/minio/pkg/utils/log"
) )
// requestLimitHandler // requestLimitHandler
@ -30,6 +32,10 @@ type connLimit struct {
limit int limit int
} }
func (c *connLimit) GetUsed(ip uint32) int {
return c.connections[ip]
}
func (c *connLimit) TestAndAdd(ip uint32) bool { func (c *connLimit) TestAndAdd(ip uint32) bool {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
@ -58,12 +64,13 @@ func (c *connLimit) Remove(ip uint32) {
func (c *connLimit) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (c *connLimit) ServeHTTP(w http.ResponseWriter, req *http.Request) {
host, _, _ := net.SplitHostPort(req.RemoteAddr) host, _, _ := net.SplitHostPort(req.RemoteAddr)
longIP := longIP{net.ParseIP(host)}.IptoUint32() longIP := longIP{net.ParseIP(host)}.IptoUint32()
if c.TestAndAdd(longIP) { if !c.TestAndAdd(longIP) {
defer c.Remove(longIP) hosts, _ := net.LookupAddr(uint32ToIP(longIP).String())
c.handler.ServeHTTP(w, req) log.Debug.Printf("Offending Host: %s, ConnectionsUSED: %d\n", hosts, c.GetUsed(longIP))
} else { writeErrorResponse(w, req, ConnectionLimitExceeded, req.URL.Path)
writeErrorResponse(w, req, ConnectionLimitExceeded, req.RequestURI)
} }
defer c.Remove(longIP)
c.handler.ServeHTTP(w, req)
} }
// ConnectionLimit limits the number of concurrent connections // ConnectionLimit limits the number of concurrent connections

@ -46,8 +46,8 @@ const (
RequestTimeTooSkewed = iota RequestTimeTooSkewed = iota
BandWidthQuotaExceeded BandWidthQuotaExceeded
BandWidthInsufficientToProceed BandWidthInsufficientToProceed
SlowDown
ConnectionLimitExceeded ConnectionLimitExceeded
SlowDown
) )
// Golang http doesn't implement these // Golang http doesn't implement these
@ -77,12 +77,17 @@ func writeErrorHeaders(w http.ResponseWriter) {
var errorCodeResponse = map[int]Error{ var errorCodeResponse = map[int]Error{
BandWidthQuotaExceeded: { BandWidthQuotaExceeded: {
Code: "BandwidthQuotaExceeded", Code: "BandwidthQuotaExceeded",
Description: "Bandwidth Quota Exceeded", Description: "Bandwidth Quota Exceeded.",
HTTPStatusCode: StatusTooManyRequests, HTTPStatusCode: StatusTooManyRequests,
}, },
BandWidthInsufficientToProceed: { BandWidthInsufficientToProceed: {
Code: "BandwidthQuotaWillBeExceeded", Code: "BandwidthQuotaWillBeExceeded",
Description: "Bandwidth quota will be exceeded with this request", Description: "Bandwidth quota will be exceeded with this request.",
HTTPStatusCode: StatusTooManyRequests,
},
ConnectionLimitExceeded: {
Code: "ConnectionLimitExceeded",
Description: "Connections Limit Exceeded.",
HTTPStatusCode: StatusTooManyRequests, HTTPStatusCode: StatusTooManyRequests,
}, },
SlowDown: { SlowDown: {
@ -90,11 +95,6 @@ var errorCodeResponse = map[int]Error{
Description: "Reduce your request rate.", Description: "Reduce your request rate.",
HTTPStatusCode: StatusTooManyRequests, HTTPStatusCode: StatusTooManyRequests,
}, },
ConnectionLimitExceeded: {
Code: "ConnectionLimit",
Description: "Connection Limit Met",
HTTPStatusCode: StatusTooManyRequests,
},
} }
// Write error response headers // Write error response headers

Loading…
Cancel
Save