Merge pull request #506 from harshavardhana/pr_out_enhance_error_responses_for_request_limit_and_bring_some_code_from_api_errors

This commit is contained in:
Harshavardhana 2015-04-26 22:12:21 -07:00
commit f20515b4ed
4 changed files with 126 additions and 44 deletions

View File

@ -18,11 +18,12 @@ package quota
import (
"errors"
"github.com/minio-io/minio/pkg/iodine"
"io"
"net"
"net/http"
"time"
"github.com/minio-io/minio/pkg/iodine"
)
// bandwidthQuotaHandler
@ -31,28 +32,12 @@ type bandwidthQuotaHandler struct {
quotas *quotaMap
}
var bandwidthQuotaExceeded = ErrorResponse{
Code: "BandwithQuotaExceeded",
Message: "Bandwidth Quota Exceeded",
Resource: "",
RequestID: "",
HostID: "",
}
var bandwidthInsufficientToProceed = ErrorResponse{
Code: "BandwidthQuotaWillBeExceeded",
Message: "Bandwidth quota will be exceeded with this request",
Resource: "",
RequestID: "",
HostID: "",
}
// ServeHTTP is an http.Handler ServeHTTP method
func (h *bandwidthQuotaHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
host, _, _ := net.SplitHostPort(req.RemoteAddr)
longIP := longIP{net.ParseIP(host)}.IptoUint32()
if h.quotas.WillExceedQuota(longIP, req.ContentLength) {
writeError(w, req, bandwidthInsufficientToProceed, 429)
writeErrorResponse(w, req, BandWidthInsufficientToProceed, req.URL.Path)
return
}
req.Body = &quotaReader{
@ -98,7 +83,7 @@ func (q *quotaReader) Read(b []byte) (int, error) {
}
if q.quotas.IsQuotaMet(q.ip) {
q.err = true
writeError(q.w, q.req, bandwidthQuotaExceeded, 429)
writeErrorResponse(q.w, q.req, BandWidthQuotaExceeded, q.req.URL.Path)
return 0, iodine.New(errors.New("Quota Met"), nil)
}
n, err := q.ReadCloser.Read(b)

121
pkg/api/quota/errors.go Normal file
View File

@ -0,0 +1,121 @@
/*
* 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 (
"bytes"
"encoding/xml"
"net/http"
)
// copied from api, no cyclic deps allowed
// Error structure
type Error struct {
Code string
Description string
HTTPStatusCode int
}
// ErrorResponse - error response format
type ErrorResponse struct {
XMLName xml.Name `xml:"Error" json:"-"`
Code string
Message string
Resource string
RequestID string
HostID string
}
// Quota standard errors non exhaustive list
const (
RequestTimeTooSkewed = iota
BandWidthQuotaExceeded
BandWidthInsufficientToProceed
SlowDown
)
// Golang http doesn't implement these
const (
StatusTooManyRequests = 429
)
func writeErrorResponse(w http.ResponseWriter, req *http.Request, errorType int, resource string) {
error := getErrorCode(errorType)
errorResponse := getErrorResponse(error, resource)
// set headers
writeErrorHeaders(w)
w.WriteHeader(error.HTTPStatusCode)
// write body
encodedErrorResponse := encodeErrorResponse(errorResponse)
w.Write(encodedErrorResponse)
}
func writeErrorHeaders(w http.ResponseWriter) {
w.Header().Set("Server", "Minio")
w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Content-Type", "application/xml")
w.Header().Set("Connection", "close")
}
// Error code to Error structure map
var errorCodeResponse = map[int]Error{
BandWidthQuotaExceeded: {
Code: "BandwidthQuotaExceeded",
Description: "Bandwidth Quota Exceeded",
HTTPStatusCode: StatusTooManyRequests,
},
BandWidthInsufficientToProceed: {
Code: "BandwidthQuotaWillBeExceeded",
Description: "Bandwidth quota will be exceeded with this request",
HTTPStatusCode: StatusTooManyRequests,
},
SlowDown: {
Code: "SlowDown",
Description: "Reduce your request rate.",
HTTPStatusCode: StatusTooManyRequests,
},
}
// Write error response headers
func encodeErrorResponse(response interface{}) []byte {
var bytesBuffer bytes.Buffer
encoder := xml.NewEncoder(&bytesBuffer)
encoder.Encode(response)
return bytesBuffer.Bytes()
}
// errorCodeError provides errorCode to Error. It returns empty if the code provided is unknown
func getErrorCode(code int) Error {
return errorCodeResponse[code]
}
// getErrorResponse gets in standard error and resource value and
// provides a encodable populated response values
func getErrorResponse(err Error, resource string) ErrorResponse {
var data = ErrorResponse{}
data.Code = err.Code
data.Message = err.Description
if resource != "" {
data.Resource = resource
}
// TODO implement this in future
data.RequestID = "3L137"
data.HostID = "3L137"
return data
}

View File

@ -17,11 +17,8 @@
package quota
import (
"bytes"
"encoding/binary"
"encoding/xml"
"net"
"net/http"
"sync"
"time"
)
@ -95,24 +92,3 @@ func (p longIP) IptoUint32() (result uint32) {
}
return binary.BigEndian.Uint32(ip)
}
// 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())
}

View File

@ -33,7 +33,7 @@ func (h *requestLimitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request
host, _, _ := net.SplitHostPort(req.RemoteAddr)
longIP := longIP{net.ParseIP(host)}.IptoUint32()
if h.quotas.IsQuotaMet(longIP) {
return
writeErrorResponse(w, req, SlowDown, req.URL.Path)
}
h.quotas.Add(longIP, 1)
h.handler.ServeHTTP(w, req)