mirror of
https://github.com/minio/minio.git
synced 2024-12-27 15:45:55 -05:00
Merge pull request #637 from harshavardhana/pr_out_put_back_the_older_conn_limit
This commit is contained in:
commit
964355206d
@ -16,35 +16,74 @@
|
|||||||
|
|
||||||
package quota
|
package quota
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/minio/minio/pkg/utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
// requestLimitHandler
|
// requestLimitHandler
|
||||||
type connLimit struct {
|
type connLimit struct {
|
||||||
|
sync.RWMutex
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
connectionQueue chan bool
|
connections map[uint32]int
|
||||||
|
limit int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connLimit) Add() {
|
func (c *connLimit) IsLimitExceeded(ip uint32) bool {
|
||||||
c.connectionQueue <- true
|
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
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connLimit) Remove() {
|
func (c *connLimit) Remove(ip uint32) {
|
||||||
<-c.connectionQueue
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
count, _ := c.connections[ip]
|
||||||
|
count = count - 1
|
||||||
|
if count <= 0 {
|
||||||
|
delete(c.connections, ip)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
c.connections[ip] = count
|
||||||
|
}
|
||||||
|
|
||||||
// ServeHTTP is an http.Handler ServeHTTP method
|
// ServeHTTP is an http.Handler ServeHTTP method
|
||||||
func (c *connLimit) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (c *connLimit) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
c.Add()
|
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.handler.ServeHTTP(w, req)
|
c.handler.ServeHTTP(w, req)
|
||||||
c.Remove()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConnectionLimit limits the number of concurrent connections
|
// ConnectionLimit limits the number of concurrent connections
|
||||||
func ConnectionLimit(h http.Handler, limit int) http.Handler {
|
func ConnectionLimit(h http.Handler, limit int) http.Handler {
|
||||||
return &connLimit{
|
return &connLimit{
|
||||||
handler: h,
|
handler: h,
|
||||||
connectionQueue: make(chan bool, limit),
|
connections: make(map[uint32]int),
|
||||||
|
limit: limit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user