minio/pkg/api/quota/conn_limit.go

51 lines
1.2 KiB
Go
Raw Normal View History

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
import "net/http"
2015-04-26 19:14:51 -04:00
// requestLimitHandler
type connLimit struct {
handler http.Handler
connectionQueue chan bool
2015-04-26 19:14:51 -04:00
}
func (c *connLimit) Add() {
c.connectionQueue <- true
return
2015-04-26 19:14:51 -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) {
c.Add()
c.handler.ServeHTTP(w, req)
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{
handler: h,
connectionQueue: make(chan bool, limit),
2015-04-26 19:14:51 -04:00
}
}