mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-02 18:47:22 -05:00 
			
		
		
		
	caddymain: fix setCPU silently ignoring small percent values (#1969)
* caddymain: fix setCPU silently ignoring small percent values the percent value is resolved in a GOMAXPROCS relative number by simple division, thus rounding down the non-integer quotient. If zero, the call to runtime.GOMAXPROCS is silently ignored. We decide here to exceptionally round up the CPU cap in case of percent values that are too small. * caddymain: gofmt -s
This commit is contained in:
		
							parent
							
								
									fc1509eed4
								
							
						
					
					
						commit
						c296d7e7e0
					
				@ -221,6 +221,8 @@ func setVersion() {
 | 
				
			|||||||
// setCPU parses string cpu and sets GOMAXPROCS
 | 
					// setCPU parses string cpu and sets GOMAXPROCS
 | 
				
			||||||
// according to its value. It accepts either
 | 
					// according to its value. It accepts either
 | 
				
			||||||
// a number (e.g. 3) or a percent (e.g. 50%).
 | 
					// a number (e.g. 3) or a percent (e.g. 50%).
 | 
				
			||||||
 | 
					// If the percent resolves to less than a single
 | 
				
			||||||
 | 
					// GOMAXPROCS, it rounds it up to GOMAXPROCS=1.
 | 
				
			||||||
func setCPU(cpu string) error {
 | 
					func setCPU(cpu string) error {
 | 
				
			||||||
	var numCPU int
 | 
						var numCPU int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -236,6 +238,9 @@ func setCPU(cpu string) error {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		percent = float32(pctInt) / 100
 | 
							percent = float32(pctInt) / 100
 | 
				
			||||||
		numCPU = int(float32(availCPU) * percent)
 | 
							numCPU = int(float32(availCPU) * percent)
 | 
				
			||||||
 | 
							if numCPU < 1 {
 | 
				
			||||||
 | 
								numCPU = 1
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		// Number
 | 
							// Number
 | 
				
			||||||
		num, err := strconv.Atoi(cpu)
 | 
							num, err := strconv.Atoi(cpu)
 | 
				
			||||||
 | 
				
			|||||||
@ -41,6 +41,7 @@ func TestSetCPU(t *testing.T) {
 | 
				
			|||||||
		{"invalid input", currentCPU, true},
 | 
							{"invalid input", currentCPU, true},
 | 
				
			||||||
		{"invalid input%", currentCPU, true},
 | 
							{"invalid input%", currentCPU, true},
 | 
				
			||||||
		{"9999", maxCPU, false}, // over available CPU
 | 
							{"9999", maxCPU, false}, // over available CPU
 | 
				
			||||||
 | 
							{"1%", 1, false},        // under a single CPU; assume maxCPU < 100
 | 
				
			||||||
	} {
 | 
						} {
 | 
				
			||||||
		err := setCPU(test.input)
 | 
							err := setCPU(test.input)
 | 
				
			||||||
		if test.shouldErr && err == nil {
 | 
							if test.shouldErr && err == nil {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user