1*cc02d7e2SAndroid Build Coastguard WorkerGRPC Connection Backoff Protocol 2*cc02d7e2SAndroid Build Coastguard Worker================================ 3*cc02d7e2SAndroid Build Coastguard Worker 4*cc02d7e2SAndroid Build Coastguard WorkerWhen we do a connection to a backend which fails, it is typically desirable to 5*cc02d7e2SAndroid Build Coastguard Workernot retry immediately (to avoid flooding the network or the server with 6*cc02d7e2SAndroid Build Coastguard Workerrequests) and instead do some form of exponential backoff. 7*cc02d7e2SAndroid Build Coastguard Worker 8*cc02d7e2SAndroid Build Coastguard WorkerWe have several parameters: 9*cc02d7e2SAndroid Build Coastguard Worker 1. INITIAL_BACKOFF (how long to wait after the first failure before retrying) 10*cc02d7e2SAndroid Build Coastguard Worker 1. MULTIPLIER (factor with which to multiply backoff after a failed retry) 11*cc02d7e2SAndroid Build Coastguard Worker 1. JITTER (by how much to randomize backoffs). 12*cc02d7e2SAndroid Build Coastguard Worker 1. MAX_BACKOFF (upper bound on backoff) 13*cc02d7e2SAndroid Build Coastguard Worker 1. MIN_CONNECT_TIMEOUT (minimum time we're willing to give a connection to 14*cc02d7e2SAndroid Build Coastguard Worker complete) 15*cc02d7e2SAndroid Build Coastguard Worker 16*cc02d7e2SAndroid Build Coastguard Worker## Proposed Backoff Algorithm 17*cc02d7e2SAndroid Build Coastguard Worker 18*cc02d7e2SAndroid Build Coastguard WorkerExponentially back off the start time of connection attempts up to a limit of 19*cc02d7e2SAndroid Build Coastguard WorkerMAX_BACKOFF, with jitter. 20*cc02d7e2SAndroid Build Coastguard Worker 21*cc02d7e2SAndroid Build Coastguard Worker``` 22*cc02d7e2SAndroid Build Coastguard WorkerConnectWithBackoff() 23*cc02d7e2SAndroid Build Coastguard Worker current_backoff = INITIAL_BACKOFF 24*cc02d7e2SAndroid Build Coastguard Worker current_deadline = now() + INITIAL_BACKOFF 25*cc02d7e2SAndroid Build Coastguard Worker while (TryConnect(Max(current_deadline, now() + MIN_CONNECT_TIMEOUT)) 26*cc02d7e2SAndroid Build Coastguard Worker != SUCCESS) 27*cc02d7e2SAndroid Build Coastguard Worker SleepUntil(current_deadline) 28*cc02d7e2SAndroid Build Coastguard Worker current_backoff = Min(current_backoff * MULTIPLIER, MAX_BACKOFF) 29*cc02d7e2SAndroid Build Coastguard Worker current_deadline = now() + current_backoff + 30*cc02d7e2SAndroid Build Coastguard Worker UniformRandom(-JITTER * current_backoff, JITTER * current_backoff) 31*cc02d7e2SAndroid Build Coastguard Worker 32*cc02d7e2SAndroid Build Coastguard Worker``` 33*cc02d7e2SAndroid Build Coastguard Worker 34*cc02d7e2SAndroid Build Coastguard WorkerWith specific parameters of 35*cc02d7e2SAndroid Build Coastguard WorkerMIN_CONNECT_TIMEOUT = 20 seconds 36*cc02d7e2SAndroid Build Coastguard WorkerINITIAL_BACKOFF = 1 second 37*cc02d7e2SAndroid Build Coastguard WorkerMULTIPLIER = 1.6 38*cc02d7e2SAndroid Build Coastguard WorkerMAX_BACKOFF = 120 seconds 39*cc02d7e2SAndroid Build Coastguard WorkerJITTER = 0.2 40*cc02d7e2SAndroid Build Coastguard Worker 41*cc02d7e2SAndroid Build Coastguard WorkerImplementations with pressing concerns (such as minimizing the number of wakeups 42*cc02d7e2SAndroid Build Coastguard Workeron a mobile phone) may wish to use a different algorithm, and in particular 43*cc02d7e2SAndroid Build Coastguard Workerdifferent jitter logic. 44*cc02d7e2SAndroid Build Coastguard Worker 45*cc02d7e2SAndroid Build Coastguard WorkerAlternate implementations must ensure that connection backoffs started at the 46*cc02d7e2SAndroid Build Coastguard Workersame time disperse, and must not attempt connections substantially more often 47*cc02d7e2SAndroid Build Coastguard Workerthan the above algorithm. 48*cc02d7e2SAndroid Build Coastguard Worker 49*cc02d7e2SAndroid Build Coastguard Worker## Reset Backoff 50*cc02d7e2SAndroid Build Coastguard Worker 51*cc02d7e2SAndroid Build Coastguard WorkerThe back off should be reset to INITIAL_BACKOFF at some time point, so that the 52*cc02d7e2SAndroid Build Coastguard Workerreconnecting behavior is consistent no matter the connection is a newly started 53*cc02d7e2SAndroid Build Coastguard Workerone or a previously disconnected one. 54*cc02d7e2SAndroid Build Coastguard Worker 55*cc02d7e2SAndroid Build Coastguard WorkerWe choose to reset the Backoff when the SETTINGS frame is received, at that time 56*cc02d7e2SAndroid Build Coastguard Workerpoint, we know for sure that this connection was accepted by the server. 57