Add some logging with debug

This commit is contained in:
Harshavardhana 2015-04-27 19:23:37 -07:00
parent 7bdcf4bbf3
commit 605edbc307
4 changed files with 34 additions and 13 deletions

View File

@ -18,6 +18,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/user" "os/user"
"path" "path"
@ -264,6 +265,8 @@ func main() {
globalDebugFlag = c.GlobalBool("debug") globalDebugFlag = c.GlobalBool("debug")
if globalDebugFlag { if globalDebugFlag {
app.ExtraInfo = getSystemData() app.ExtraInfo = getSystemData()
} else {
log.Debug = log.New(ioutil.Discard, "", 0)
} }
return nil return nil
} }

View File

@ -23,9 +23,10 @@ import (
"net/http" "net/http"
"time" "time"
"sync"
"github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log" "github.com/minio-io/minio/pkg/utils/log"
"sync"
) )
// bandwidthQuotaHandler // bandwidthQuotaHandler
@ -39,6 +40,8 @@ func (h *bandwidthQuotaHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
host, _, _ := net.SplitHostPort(req.RemoteAddr) host, _, _ := net.SplitHostPort(req.RemoteAddr)
longIP := longIP{net.ParseIP(host)}.IptoUint32() longIP := longIP{net.ParseIP(host)}.IptoUint32()
if h.quotas.WillExceedQuota(longIP, req.ContentLength) { if h.quotas.WillExceedQuota(longIP, req.ContentLength) {
hosts, _ := net.LookupAddr(uint32ToIP(longIP).String())
log.Debug.Printf("Offending Host: %s, BandwidthUsed: %d", hosts, h.quotas.GetQuotaUsed(longIP))
writeErrorResponse(w, req, BandWidthInsufficientToProceed, req.URL.Path) writeErrorResponse(w, req, BandWidthInsufficientToProceed, req.URL.Path)
return return
} }
@ -94,6 +97,8 @@ func (q *quotaReader) Read(b []byte) (int, error) {
if q.err == false && q.quotas.IsQuotaMet(q.ip) { if q.err == false && q.quotas.IsQuotaMet(q.ip) {
defer q.lock.Unlock() defer q.lock.Unlock()
q.err = true q.err = true
hosts, _ := net.LookupAddr(uint32ToIP(q.ip).String())
log.Debug.Printf("Offending Host: %s, BandwidthUsed: %d", hosts, q.quotas.GetQuotaUsed(q.ip))
writeErrorResponse(q.w, q.req, BandWidthQuotaExceeded, q.req.URL.Path) writeErrorResponse(q.w, q.req, BandWidthQuotaExceeded, q.req.URL.Path)
return 0, iodine.New(errors.New("Quota Met"), nil) return 0, iodine.New(errors.New("Quota Met"), nil)
} }

View File

@ -32,10 +32,21 @@ type quotaMap struct {
segmentSize time.Duration segmentSize time.Duration
} }
func (q *quotaMap) CanExpire() {
currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds()
// divide by segmentSize, otherwise expiredQuotas will always be negative
expiredQuotas := currentMinute - (q.duration.Nanoseconds() / q.segmentSize.Nanoseconds())
for time := range q.data {
if time < expiredQuotas {
delete(q.data, time)
}
}
}
func (q *quotaMap) Add(ip uint32, size int64) { func (q *quotaMap) Add(ip uint32, size int64) {
q.Lock() q.Lock()
defer q.Unlock() defer q.Unlock()
q.clean() q.CanExpire()
currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds() currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds()
if _, ok := q.data[currentMinute]; !ok { if _, ok := q.data[currentMinute]; !ok {
q.data[currentMinute] = make(map[uint32]int64) q.data[currentMinute] = make(map[uint32]int64)
@ -46,7 +57,7 @@ func (q *quotaMap) Add(ip uint32, size int64) {
} }
func (q *quotaMap) IsQuotaMet(ip uint32) bool { func (q *quotaMap) IsQuotaMet(ip uint32) bool {
q.clean() q.CanExpire()
if q.GetQuotaUsed(ip) >= q.limit { if q.GetQuotaUsed(ip) >= q.limit {
return true return true
} }
@ -70,16 +81,6 @@ func (q *quotaMap) WillExceedQuota(ip uint32, size int64) (result bool) {
return q.GetQuotaUsed(ip)+size > q.limit return q.GetQuotaUsed(ip)+size > q.limit
} }
func (q *quotaMap) clean() {
currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds()
expiredQuotas := currentMinute - q.duration.Nanoseconds()
for time := range q.data {
if time < expiredQuotas {
delete(q.data, time)
}
}
}
type longIP struct { type longIP struct {
net.IP net.IP
} }

View File

@ -17,9 +17,12 @@
package quota package quota
import ( import (
"encoding/binary"
"net" "net"
"net/http" "net/http"
"time" "time"
"github.com/minio-io/minio/pkg/utils/log"
) )
// requestLimitHandler // requestLimitHandler
@ -28,11 +31,20 @@ type requestLimitHandler struct {
quotas *quotaMap quotas *quotaMap
} }
//convert a uint32 to an ipv4
func uint32ToIP(ip uint32) net.IP {
addr := net.IP{0, 0, 0, 0}
binary.BigEndian.PutUint32(addr, ip)
return addr
}
// ServeHTTP is an http.Handler ServeHTTP method // ServeHTTP is an http.Handler ServeHTTP method
func (h *requestLimitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (h *requestLimitHandler) 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 h.quotas.IsQuotaMet(longIP) { if h.quotas.IsQuotaMet(longIP) {
hosts, _ := net.LookupAddr(uint32ToIP(longIP).String())
log.Debug.Printf("Offending Host: %s, RequestUSED: %d\n", hosts, h.quotas.GetQuotaUsed(longIP))
writeErrorResponse(w, req, SlowDown, req.URL.Path) writeErrorResponse(w, req, SlowDown, req.URL.Path)
} }
h.quotas.Add(longIP, 1) h.quotas.Add(longIP, 1)