mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	middleware/secondary: add jitter (#841)
randomize the times when doing transfers and checks. Fixes #840
This commit is contained in:
		| @@ -2,6 +2,7 @@ package file | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"math/rand" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/miekg/dns" | ||||
| @@ -146,6 +147,9 @@ Restart: | ||||
| 			if !retryActive { | ||||
| 				break | ||||
| 			} | ||||
|  | ||||
| 			time.Sleep(jitter(2000)) // 2s randomize | ||||
|  | ||||
| 			ok, err := z.shouldTransfer() | ||||
| 			if err != nil && ok { | ||||
| 				if err := z.TransferIn(); err != nil { | ||||
| @@ -161,6 +165,9 @@ Restart: | ||||
| 			} | ||||
|  | ||||
| 		case <-refreshTicker.C: | ||||
|  | ||||
| 			time.Sleep(jitter(5000)) // 5s randomize | ||||
|  | ||||
| 			ok, err := z.shouldTransfer() | ||||
| 			retryActive = err != nil | ||||
| 			if err != nil && ok { | ||||
| @@ -180,6 +187,13 @@ Restart: | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // jitter returns a random duration between [0,n) * time.Millisecond | ||||
| func jitter(n int) time.Duration { | ||||
| 	r := rand.Intn(n) | ||||
| 	return time.Duration(r) * time.Millisecond | ||||
|  | ||||
| } | ||||
|  | ||||
| // MaxSerialIncrement is the maximum difference between two serial numbers. If the difference between | ||||
| // two serials is greater than this number, the smaller one is considered greater. | ||||
| const MaxSerialIncrement uint32 = 2147483647 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user