Skip to content

Commit 6d4b71b

Browse files
author
liuyongkui
committed
优化网络超时接口,进行对时间有效性校验
1 parent 22fd2c5 commit 6d4b71b

File tree

2 files changed

+70
-18
lines changed

2 files changed

+70
-18
lines changed

novate/src/main/java/com/tamic/novate/Novate.java

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public final class Novate {
121121
private Map<Object, Observable<ResponseBody>> downMaps = new HashMap<Object, Observable<ResponseBody>>() {
122122
};
123123
private Observable.Transformer exceptTransformer = null;
124+
private static final int DEFAULT_TIMEOUT = 15;
125+
private static final int DEFAULT_MAXIDLE_CONNECTIONS = 5;
126+
private static final long DEFAULT_KEEP_ALIVEDURATION = 8;
127+
private static final long DEFAULT_CACHEMAXSIZE = 10 * 1024 * 1024;
124128
public static final String TAG = "Novate";
125129

126130
/**
@@ -207,9 +211,6 @@ public <T> T rxGet(final String url, final Map<String, Object> maps, ResponseCal
207211
return rxGet(url, url, maps, callBack);
208212
}
209213

210-
211-
212-
213214
/**
214215
* Novate execute get request
215216
* @param tag request tag
@@ -1437,12 +1438,12 @@ public Builder newBuilder() {
14371438
* Mandatory Builder for the Builder
14381439
*/
14391440
public static final class Builder {
1440-
1441-
private static final int DEFAULT_TIMEOUT = 15;
1442-
private static final int DEFAULT_MAXIDLE_CONNECTIONS = 5;
1443-
private static final long DEFAULT_KEEP_ALIVEDURATION = 8;
1444-
private static final long caheMaxSize = 10 * 1024 * 1024;
1445-
1441+
private int connectTimeout = DEFAULT_TIMEOUT;
1442+
private int writeTimeout = DEFAULT_TIMEOUT;
1443+
private int readTimeout = DEFAULT_TIMEOUT;
1444+
private int default_maxidle_connections = DEFAULT_MAXIDLE_CONNECTIONS;
1445+
private long default_keep_aliveduration = DEFAULT_MAXIDLE_CONNECTIONS;
1446+
private long caheMaxSize = DEFAULT_CACHEMAXSIZE;
14461447
private okhttp3.Call.Factory callFactory;
14471448
private String baseUrl;
14481449
private Boolean isLog = false;
@@ -1537,6 +1538,15 @@ public Builder writeTimeout(int timeout) {
15371538
return writeTimeout(timeout, TimeUnit.SECONDS);
15381539
}
15391540

1541+
1542+
/**
1543+
* Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise
1544+
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
1545+
*/
1546+
public Builder readTimeout(int timeout) {
1547+
return readTimeout(timeout, TimeUnit.SECONDS);
1548+
}
1549+
15401550
/**
15411551
* Attaches {@code tag} to the request. It can be used later to cancel the request. If the tag
15421552
* is unspecified or null, the request is canceled by using the request itself as the tag.
@@ -1589,12 +1599,26 @@ public Builder proxy(Proxy proxy) {
15891599
* Sets the default write timeout for new connections. A value of 0 means no timeout,
15901600
* otherwise values must be between 1 and {@link TimeUnit #MAX_VALUE} when converted to
15911601
* milliseconds.
1602+
* TimeUnit {@link TimeUnit}
15921603
*/
15931604
public Builder writeTimeout(int timeout, TimeUnit unit) {
1605+
this.writeTimeout = Utils.checkDuration("timeout", timeout, unit);
15941606
if (timeout != -1) {
15951607
okhttpBuilder.writeTimeout(timeout, unit);
1596-
} else {
1597-
okhttpBuilder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
1608+
}
1609+
return this;
1610+
}
1611+
1612+
1613+
/**
1614+
* Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise
1615+
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
1616+
* TimeUnit {@link TimeUnit}
1617+
*/
1618+
public Builder readTimeout(int timeout, TimeUnit unit) {
1619+
this.readTimeout = Utils.checkDuration("timeout", timeout, unit);
1620+
if (timeout != -1) {
1621+
okhttpBuilder.readTimeout(readTimeout, unit);
15981622
}
15991623
return this;
16001624
}
@@ -1603,6 +1627,10 @@ public Builder writeTimeout(int timeout, TimeUnit unit) {
16031627
* Sets the connection pool used to recycle HTTP and HTTPS connections.
16041628
* <p>
16051629
* <p>If unset, a new connection pool will be used.
1630+
* <p>
1631+
* connectionPool =
1632+
* new ConnectionPool(DEFAULT_MAXIDLE_CONNECTIONS, DEFAULT_KEEP_ALIVEDURATION, TimeUnit.SECONDS);
1633+
* <p>
16061634
*/
16071635
public Builder connectionPool(ConnectionPool connectionPool) {
16081636
if (connectionPool == null) throw new NullPointerException("connectionPool == null");
@@ -1614,12 +1642,12 @@ public Builder connectionPool(ConnectionPool connectionPool) {
16141642
* Sets the default connect timeout for new connections. A value of 0 means no timeout,
16151643
* otherwise values must be between 1 and {@link TimeUnit #MAX_VALUE} when converted to
16161644
* milliseconds.
1645+
* TimeUnit {@link TimeUnit}
16171646
*/
16181647
public Builder connectTimeout(int timeout, TimeUnit unit) {
1648+
this.readTimeout = Utils.checkDuration("timeout", timeout, unit);;
16191649
if (timeout != -1) {
1620-
okhttpBuilder.connectTimeout(timeout, unit);
1621-
} else {
1622-
okhttpBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
1650+
okhttpBuilder.connectTimeout(readTimeout, unit);
16231651
}
16241652
return this;
16251653
}
@@ -1867,7 +1895,6 @@ public Novate build() {
18671895
if (cache == null) {
18681896
cache = new Cache(httpCacheDirectory, caheMaxSize);
18691897
}
1870-
18711898
addCache(cache);
18721899

18731900
} catch (Exception e) {
@@ -1881,15 +1908,30 @@ public Novate build() {
18811908
if (cache != null) {
18821909
okhttpBuilder.cache(cache);
18831910
}
1911+
/**
1912+
* Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise
1913+
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
1914+
*/
18841915

1916+
//okhttpBuilder.writeTimeout(writeTimeout, TimeUnit.SECONDS);
1917+
/**
1918+
* Sets the default connect timeout for new connections. A value of 0 means no timeout,
1919+
* otherwise values must be between 1 and {@link Integer#MAX_VALUE} when converted to
1920+
* milliseconds.
1921+
*/
1922+
//okhttpBuilder.connectTimeout(connectTimeout, TimeUnit.SECONDS);
1923+
/**
1924+
* Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise
1925+
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
1926+
*/
1927+
//okhttpBuilder.readTimeout(readTimeout, TimeUnit.SECONDS);
18851928
/**
18861929
* Sets the connection pool used to recycle HTTP and HTTPS connections.
18871930
*
18881931
* <p>If unset, a new connection pool will be used.
18891932
*/
18901933
if (connectionPool == null) {
1891-
1892-
connectionPool = new ConnectionPool(DEFAULT_MAXIDLE_CONNECTIONS, DEFAULT_KEEP_ALIVEDURATION, TimeUnit.SECONDS);
1934+
connectionPool = new ConnectionPool(default_maxidle_connections, default_maxidle_connections, TimeUnit.SECONDS);
18931935
}
18941936
okhttpBuilder.connectionPool(connectionPool);
18951937

@@ -1898,7 +1940,7 @@ public Novate build() {
18981940
* precedence over {@link #proxySelector}, which is only honored when this proxy is null (which
18991941
* it is by default). To disable proxy use completely, call {@code setProxy(Proxy.NO_PROXY)}.
19001942
*/
1901-
if (proxy == null) {
1943+
if (proxy != null) {
19021944
okhttpBuilder.proxy(proxy);
19031945
}
19041946

novate/src/main/java/com/tamic/novate/util/Utils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Iterator;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.concurrent.TimeUnit;
2122

2223
import okhttp3.MediaType;
2324
import okhttp3.MultipartBody;
@@ -305,4 +306,13 @@ public static <T> List<T> jsonToList(String json, Class<T> clazz) {
305306
}
306307
return new Gson().fromJson(json, new TypeToken<T>(){}.getType());
307308
}
309+
310+
public static int checkDuration(String name, long duration, TimeUnit unit) {
311+
if (duration < 0) throw new IllegalArgumentException(name + " < 0");
312+
if (unit == null) throw new NullPointerException("unit == null");
313+
long millis = unit.toMillis(duration);
314+
if (millis > Integer.MAX_VALUE) throw new IllegalArgumentException(name + " too large.");
315+
if (millis == 0 && duration > 0) throw new IllegalArgumentException(name + " too small.");
316+
return (int) millis;
317+
}
308318
}

0 commit comments

Comments
 (0)