-
Notifications
You must be signed in to change notification settings - Fork 344
/
RetryWithDelay.kt
52 lines (43 loc) · 1.79 KB
/
RetryWithDelay.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.cxz.wanandroid.http.function
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import io.reactivex.functions.Function
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
/**
* @author chenxz
* @date 2018/8/21
* @desc 请求重连
*/
class RetryWithDelay : Function<Observable<out Throwable>, Observable<*>> {
private var maxRetryCount = 3 // 可重试次数
private var retryDelayMillis: Long = 3000 // 重试等待时间
constructor() {}
constructor(retryDelayMillis: Long) {
this.retryDelayMillis = retryDelayMillis
}
constructor(maxRetryCount: Int, retryDelayMillis: Long) {
this.maxRetryCount = maxRetryCount
this.retryDelayMillis = retryDelayMillis
}
@Throws(Exception::class)
override fun apply(observable: Observable<out Throwable>): Observable<*> {
return observable
.zipWith(Observable.range(1, maxRetryCount + 1),
BiFunction<Throwable, Int, Wrapper> { t1, t2 -> Wrapper(t2, t1) })
.flatMap { wrapper ->
val t = wrapper.throwable
if ((t is ConnectException
|| t is SocketTimeoutException
|| t is TimeoutException
|| t is HttpException)
&& wrapper.index < maxRetryCount + 1) {
Observable.timer(retryDelayMillis * wrapper.index, TimeUnit.MILLISECONDS)
} else Observable.error<Any>(wrapper.throwable)
}
}
private inner class Wrapper(val index: Int, val throwable: Throwable)
}