2015-04-24 23:45:44 -04:00
|
|
|
/*
|
|
|
|
* 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 (
|
2015-04-26 22:25:48 -04:00
|
|
|
"bytes"
|
2015-04-25 21:13:49 -04:00
|
|
|
"encoding/binary"
|
2015-04-26 22:25:48 -04:00
|
|
|
"encoding/xml"
|
2015-04-25 16:02:01 -04:00
|
|
|
"net"
|
2015-04-26 22:25:48 -04:00
|
|
|
"net/http"
|
2015-04-24 23:45:44 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// map[minute][address] = current quota
|
|
|
|
type quotaMap struct {
|
|
|
|
sync.RWMutex
|
2015-04-26 17:20:24 -04:00
|
|
|
data map[int64]map[uint32]int64
|
|
|
|
limit int64
|
|
|
|
duration time.Duration
|
|
|
|
segmentSize time.Duration
|
2015-04-24 23:45:44 -04:00
|
|
|
}
|
|
|
|
|
2015-04-26 17:20:24 -04:00
|
|
|
func (q *quotaMap) Add(ip uint32, size int64) {
|
2015-04-25 21:01:52 -04:00
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
2015-04-26 17:20:24 -04:00
|
|
|
q.clean()
|
|
|
|
currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds()
|
2015-04-24 23:45:44 -04:00
|
|
|
if _, ok := q.data[currentMinute]; !ok {
|
2015-04-26 17:20:24 -04:00
|
|
|
q.data[currentMinute] = make(map[uint32]int64)
|
2015-04-24 23:45:44 -04:00
|
|
|
}
|
|
|
|
currentData, _ := q.data[currentMinute][ip]
|
2015-04-25 21:01:52 -04:00
|
|
|
proposedDataSize := currentData + size
|
|
|
|
q.data[currentMinute][ip] = proposedDataSize
|
2015-04-24 23:45:44 -04:00
|
|
|
}
|
|
|
|
|
2015-04-26 17:20:24 -04:00
|
|
|
func (q *quotaMap) IsQuotaMet(ip uint32) bool {
|
|
|
|
q.clean()
|
2015-04-26 18:49:46 -04:00
|
|
|
if q.GetQuotaUsed(ip) >= q.limit {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quotaMap) GetQuotaUsed(ip uint32) (total int64) {
|
2015-04-26 17:20:24 -04:00
|
|
|
currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds()
|
|
|
|
if _, ok := q.data[currentMinute]; !ok {
|
|
|
|
q.data[currentMinute] = make(map[uint32]int64)
|
|
|
|
}
|
|
|
|
for _, segment := range q.data {
|
|
|
|
if used, ok := segment[ip]; ok {
|
|
|
|
total += used
|
|
|
|
}
|
|
|
|
}
|
2015-04-26 18:49:46 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quotaMap) WillExceedQuota(ip uint32, size int64) (result bool) {
|
|
|
|
return q.GetQuotaUsed(ip)+size > q.limit
|
2015-04-26 17:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 23:45:44 -04:00
|
|
|
}
|
|
|
|
|
2015-04-25 16:02:01 -04:00
|
|
|
type longIP struct {
|
|
|
|
net.IP
|
|
|
|
}
|
|
|
|
|
|
|
|
// []byte to uint32 representation
|
2015-04-25 21:01:52 -04:00
|
|
|
func (p longIP) IptoUint32() (result uint32) {
|
2015-04-25 16:02:01 -04:00
|
|
|
ip := p.To4()
|
|
|
|
if ip == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2015-04-25 21:13:49 -04:00
|
|
|
return binary.BigEndian.Uint32(ip)
|
2015-04-25 16:02:01 -04:00
|
|
|
}
|
2015-04-26 22:25:48 -04:00
|
|
|
|
|
|
|
// copied from api, no cyclic deps allowed
|
|
|
|
|
|
|
|
// ErrorResponse - error response format
|
|
|
|
type ErrorResponse struct {
|
|
|
|
XMLName xml.Name `xml:"Error" json:"-"`
|
|
|
|
Code string
|
|
|
|
Message string
|
|
|
|
Resource string
|
|
|
|
RequestID string
|
|
|
|
HostID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeError(w http.ResponseWriter, req *http.Request, errorResponse ErrorResponse, status int) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
encoder := xml.NewEncoder(&buf)
|
|
|
|
w.WriteHeader(status)
|
|
|
|
encoder.Encode(errorResponse)
|
|
|
|
encoder.Flush()
|
|
|
|
w.Write(buf.Bytes())
|
|
|
|
}
|