| 
									
										
										
										
											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-09-18 17:03:02 +08:00
										 |  |  | 	"github.com/minio/minio/cmd/logger" | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 	"github.com/minio/minio/pkg/sys" | 
					
						
							| 
									
										
										
										
											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-09-24 00:14:33 +08:00
										 |  |  | 	clusterDeadline  time.Duration | 
					
						
							| 
									
										
										
										
											2020-11-03 09:21:56 +08:00
										 |  |  | 	listQuorum       int | 
					
						
							| 
									
										
										
										
											2020-11-06 03:49:56 +08:00
										 |  |  | 	extendListLife   time.Duration | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	corsAllowOrigins []string | 
					
						
							| 
									
										
										
										
											2021-01-23 04:09:24 +08:00
										 |  |  | 	// total drives per erasure set across pools.
 | 
					
						
							| 
									
										
										
										
											2021-02-02 19:15:06 +08:00
										 |  |  | 	totalDriveCount    int | 
					
						
							|  |  |  | 	replicationWorkers int | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-23 04:09:24 +08:00
										 |  |  | func (t *apiConfig) init(cfg api.Config, setDriveCounts []int) { | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	t.mu.Lock() | 
					
						
							|  |  |  | 	defer t.mu.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | 	t.clusterDeadline = cfg.ClusterDeadline | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 	t.corsAllowOrigins = cfg.CorsAllowOrigin | 
					
						
							| 
									
										
										
										
											2021-01-23 04:09:24 +08:00
										 |  |  | 	for _, setDriveCount := range setDriveCounts { | 
					
						
							|  |  |  | 		t.totalDriveCount += setDriveCount | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 	var apiRequestsMaxPerNode int | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 	if cfg.RequestsMax <= 0 { | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 		stats, err := sys.GetStats() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2020-09-18 17:03:02 +08:00
										 |  |  | 			logger.LogIf(GlobalContext, err) | 
					
						
							|  |  |  | 			// Default to 16 GiB, not critical.
 | 
					
						
							|  |  |  | 			stats.TotalRAM = 16 << 30 | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		// max requests per node is calculated as
 | 
					
						
							|  |  |  | 		// total_ram / ram_per_request
 | 
					
						
							| 
									
										
										
										
											2021-01-23 04:09:24 +08:00
										 |  |  | 		// ram_per_request is 1MiB * driveCount + 2 * 10MiB (default erasure block size)
 | 
					
						
							|  |  |  | 		apiRequestsMaxPerNode = int(stats.TotalRAM / uint64(t.totalDriveCount*(blockSizeLarge+blockSizeSmall)+blockSizeV1*2)) | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 		apiRequestsMaxPerNode = cfg.RequestsMax | 
					
						
							| 
									
										
										
										
											2020-09-05 10:37:37 +08:00
										 |  |  | 		if len(globalEndpoints.Hostnames()) > 0 { | 
					
						
							|  |  |  | 			apiRequestsMaxPerNode /= len(globalEndpoints.Hostnames()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-05 01:32:35 +08:00
										 |  |  | 	if cap(t.requestsPool) < apiRequestsMaxPerNode { | 
					
						
							|  |  |  | 		// Only replace if needed.
 | 
					
						
							|  |  |  | 		// Existing requests will use the previous limit,
 | 
					
						
							|  |  |  | 		// but new requests will use the new limit.
 | 
					
						
							|  |  |  | 		// There will be a short overlap window,
 | 
					
						
							|  |  |  | 		// but this shouldn't last long.
 | 
					
						
							|  |  |  | 		t.requestsPool = make(chan struct{}, apiRequestsMaxPerNode) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 	t.requestsDeadline = cfg.RequestsDeadline | 
					
						
							| 
									
										
										
										
											2020-11-03 09:21:56 +08:00
										 |  |  | 	t.listQuorum = cfg.GetListQuorum() | 
					
						
							| 
									
										
										
										
											2020-11-06 03:49:56 +08:00
										 |  |  | 	t.extendListLife = cfg.ExtendListLife | 
					
						
							| 
									
										
										
										
											2021-02-02 19:15:06 +08:00
										 |  |  | 	t.replicationWorkers = cfg.ReplicationWorkers | 
					
						
							| 
									
										
										
										
											2020-11-03 09:21:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) getListQuorum() int { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.listQuorum | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 03:49:56 +08:00
										 |  |  | func (t *apiConfig) getExtendListLife() time.Duration { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.extendListLife | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | func (t *apiConfig) getCorsAllowOrigins() []string { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 14:03:08 +08:00
										 |  |  | 	corsAllowOrigins := make([]string, len(t.corsAllowOrigins)) | 
					
						
							|  |  |  | 	copy(corsAllowOrigins, t.corsAllowOrigins) | 
					
						
							|  |  |  | 	return corsAllowOrigins | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | func (t *apiConfig) getClusterDeadline() time.Duration { | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | 	if t.clusterDeadline == 0 { | 
					
						
							| 
									
										
										
										
											2020-06-05 05:58:34 +08:00
										 |  |  | 		return 10 * time.Second | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 00:14:33 +08:00
										 |  |  | 	return t.clusterDeadline | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | func (t *apiConfig) getRequestsPool() (chan struct{}, time.Duration) { | 
					
						
							| 
									
										
										
										
											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-12-04 11:23:19 +08:00
										 |  |  | 		return nil, time.Duration(0) | 
					
						
							| 
									
										
										
										
											2020-11-05 00:25:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 	return t.requestsPool, t.requestsDeadline | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // maxClients throttles the S3 API calls
 | 
					
						
							|  |  |  | func maxClients(f http.HandlerFunc) http.HandlerFunc { | 
					
						
							|  |  |  | 	return func(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 		pool, deadline := globalAPIConfig.getRequestsPool() | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 		if pool == nil { | 
					
						
							|  |  |  | 			f.ServeHTTP(w, r) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 		deadlineTimer := time.NewTimer(deadline) | 
					
						
							|  |  |  | 		defer deadlineTimer.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 		select { | 
					
						
							|  |  |  | 		case pool <- struct{}{}: | 
					
						
							|  |  |  | 			defer func() { <-pool }() | 
					
						
							|  |  |  | 			f.ServeHTTP(w, r) | 
					
						
							| 
									
										
										
										
											2020-12-04 11:23:19 +08:00
										 |  |  | 		case <-deadlineTimer.C: | 
					
						
							| 
									
										
										
										
											2020-04-15 03:46:37 +08:00
										 |  |  | 			// Send a http timeout message
 | 
					
						
							|  |  |  | 			writeErrorResponse(r.Context(), w, | 
					
						
							|  |  |  | 				errorCodes.ToAPIErr(ErrOperationMaxedOut), | 
					
						
							|  |  |  | 				r.URL, guessIsBrowserReq(r)) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		case <-r.Context().Done(): | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-02-02 19:15:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (t *apiConfig) getReplicationWorkers() int { | 
					
						
							|  |  |  | 	t.mu.RLock() | 
					
						
							|  |  |  | 	defer t.mu.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t.replicationWorkers | 
					
						
							|  |  |  | } |