| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2016 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | var errTooManyRequests = errors.New("Too many clients in the waiting list") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | // rateLimit - represents datatype of the functionality implemented to
 | 
					
						
							|  |  |  | // limit the number of concurrent http requests.
 | 
					
						
							|  |  |  | type rateLimit struct { | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | 	handler   http.Handler | 
					
						
							|  |  |  | 	workQueue chan struct{} | 
					
						
							|  |  |  | 	waitQueue chan struct{} | 
					
						
							| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // acquire and release implement a way to send and receive from the
 | 
					
						
							|  |  |  | // channel this is in-turn used to rate limit incoming connections in
 | 
					
						
							|  |  |  | // ServeHTTP() http.Handler method.
 | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | func (c *rateLimit) acquire() error { | 
					
						
							| 
									
										
										
										
											2016-09-15 02:27:37 +08:00
										 |  |  | 	//attempt to enter the waitQueue. If no slot is immediately
 | 
					
						
							|  |  |  | 	//available return error.
 | 
					
						
							|  |  |  | 	select { | 
					
						
							|  |  |  | 	case c.waitQueue <- struct{}{}: | 
					
						
							|  |  |  | 		//entered wait queue
 | 
					
						
							|  |  |  | 		break | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		//no slot available for waiting
 | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | 		return errTooManyRequests | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 02:27:37 +08:00
										 |  |  | 	//block attempting to enter the workQueue. If the workQueue is
 | 
					
						
							|  |  |  | 	//full, there can be at most cap(waitQueue) == 4*globalMaxConn
 | 
					
						
							|  |  |  | 	//goroutines waiting here because of the select above.
 | 
					
						
							|  |  |  | 	select { | 
					
						
							|  |  |  | 	case c.workQueue <- struct{}{}: | 
					
						
							|  |  |  | 		//entered workQueue - so remove one waiter. This step
 | 
					
						
							|  |  |  | 		//does not block as the waitQueue cannot be empty.
 | 
					
						
							|  |  |  | 		<-c.waitQueue | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-13 06:53:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Release one element from workQueue to serve a new client
 | 
					
						
							|  |  |  | // in the waiting list
 | 
					
						
							|  |  |  | func (c *rateLimit) release() { | 
					
						
							|  |  |  | 	<-c.workQueue | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // ServeHTTP is an http.Handler ServeHTTP method, implemented to rate
 | 
					
						
							|  |  |  | // limit incoming HTTP requests.
 | 
					
						
							|  |  |  | func (c *rateLimit) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 	// Acquire the connection if queue is not full, otherwise
 | 
					
						
							|  |  |  | 	// code path waits here until the previous case is true.
 | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | 	if err := c.acquire(); err != nil { | 
					
						
							|  |  |  | 		w.WriteHeader(http.StatusTooManyRequests) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Serves the request.
 | 
					
						
							|  |  |  | 	c.handler.ServeHTTP(w, r) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | 	// Release
 | 
					
						
							|  |  |  | 	c.release() | 
					
						
							| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // setRateLimitHandler limits the number of concurrent http requests based on MINIO_MAXCONN.
 | 
					
						
							|  |  |  | func setRateLimitHandler(handler http.Handler) http.Handler { | 
					
						
							|  |  |  | 	if globalMaxConn == 0 { | 
					
						
							|  |  |  | 		return handler | 
					
						
							|  |  |  | 	} // else proceed to rate limiting.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// For max connection limit of > '0' we initialize rate limit handler.
 | 
					
						
							|  |  |  | 	return &rateLimit{ | 
					
						
							| 
									
										
										
										
											2016-07-27 05:17:11 +08:00
										 |  |  | 		handler:   handler, | 
					
						
							|  |  |  | 		workQueue: make(chan struct{}, globalMaxConn), | 
					
						
							|  |  |  | 		waitQueue: make(chan struct{}, globalMaxConn*4), | 
					
						
							| 
									
										
										
										
											2016-06-26 18:18:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |