2015-04-26 19:14:51 -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
|
|
|
|
|
2015-06-06 20:01:39 -04:00
|
|
|
import "net/http"
|
2015-04-26 19:14:51 -04:00
|
|
|
|
|
|
|
// requestLimitHandler
|
|
|
|
type connLimit struct {
|
2015-06-06 20:01:39 -04:00
|
|
|
handler http.Handler
|
|
|
|
connectionQueue chan bool
|
2015-04-26 19:14:51 -04:00
|
|
|
}
|
|
|
|
|
2015-06-06 20:01:39 -04:00
|
|
|
func (c *connLimit) Add() {
|
|
|
|
c.connectionQueue <- true
|
2015-04-29 21:01:49 -04:00
|
|
|
return
|
2015-04-26 19:14:51 -04:00
|
|
|
}
|
|
|
|
|
2015-06-06 20:01:39 -04:00
|
|
|
func (c *connLimit) Remove() {
|
|
|
|
<-c.connectionQueue
|
|
|
|
return
|
2015-04-26 19:14:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP is an http.Handler ServeHTTP method
|
|
|
|
func (c *connLimit) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2015-06-06 20:01:39 -04:00
|
|
|
c.Add()
|
2015-04-29 20:15:54 -04:00
|
|
|
c.handler.ServeHTTP(w, req)
|
2015-06-06 20:01:39 -04:00
|
|
|
c.Remove()
|
2015-04-26 19:14:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionLimit limits the number of concurrent connections
|
|
|
|
func ConnectionLimit(h http.Handler, limit int) http.Handler {
|
|
|
|
return &connLimit{
|
2015-06-06 20:01:39 -04:00
|
|
|
handler: h,
|
|
|
|
connectionQueue: make(chan bool, limit),
|
2015-04-26 19:14:51 -04:00
|
|
|
}
|
|
|
|
}
|