| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * MinIO Cloud Storage, (C) 2020 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 cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/minio/minio/cmd/config/api" | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | type apiConfig struct { | 
					
						
							|  |  |  | 	mu sync.RWMutex | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	requestsDeadline time.Duration | 
					
						
							|  |  |  | 	requestsPool     chan struct{} | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	readyDeadline    time.Duration | 
					
						
							|  |  |  | 	corsAllowOrigins []string | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | func (t *apiConfig) init(cfg api.Config) { | 
					
						
							|  |  |  | 	t.mu.Lock() | 
					
						
							|  |  |  | 	defer t.mu.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.readyDeadline = cfg.APIReadyDeadline | 
					
						
							|  |  |  | 	t.corsAllowOrigins = cfg.APICorsAllowOrigin | 
					
						
							|  |  |  | 	if cfg.APIRequestsMax <= 0 { | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	apiRequestsMax := cfg.APIRequestsMax | 
					
						
							| 
									
										
										
										
											2020-07-21 03:28:48 +08:00
										 |  |  | 	if len(globalEndpoints.Hostnames()) > 0 { | 
					
						
							|  |  |  | 		apiRequestsMax /= len(globalEndpoints.Hostnames()) | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.requestsPool = make(chan struct{}, apiRequestsMax) | 
					
						
							|  |  |  | 	t.requestsDeadline = cfg.APIRequestsDeadline | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) getCorsAllowOrigins() []string { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.corsAllowOrigins | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) getReadyDeadline() time.Duration { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if t.readyDeadline == 0 { | 
					
						
							|  |  |  | 		return 10 * time.Second | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	return t.readyDeadline | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | func (t *apiConfig) getRequestsPool() (chan struct{}, <-chan time.Time) { | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	if t.requestsPool == nil { | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 		return nil, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.requestsPool, time.NewTimer(t.requestsDeadline).C | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // maxClients throttles the S3 API calls
 | 
					
						
							|  |  |  | func maxClients(f http.HandlerFunc) http.HandlerFunc { | 
					
						
							|  |  |  | 	return func(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 		pool, deadlineTimer := globalAPIConfig.getRequestsPool() | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 		if pool == nil { | 
					
						
							|  |  |  | 			f.ServeHTTP(w, r) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		select { | 
					
						
							|  |  |  | 		case pool <- struct{}{}: | 
					
						
							|  |  |  | 			defer func() { <-pool }() | 
					
						
							|  |  |  | 			f.ServeHTTP(w, r) | 
					
						
							|  |  |  | 		case <-deadlineTimer: | 
					
						
							|  |  |  | 			// Send a http timeout message
 | 
					
						
							|  |  |  | 			writeErrorResponse(r.Context(), w, | 
					
						
							|  |  |  | 				errorCodes.ToAPIErr(ErrOperationMaxedOut), | 
					
						
							|  |  |  | 				r.URL, guessIsBrowserReq(r)) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		case <-r.Context().Done(): | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |