2
2
3
3
import java .io .IOException ;
4
4
import java .net .InetSocketAddress ;
5
+ import java .net .SocketTimeoutException ;
5
6
import java .nio .channels .SocketChannel ;
6
7
7
- public abstract class BaseSocketChannelProvider implements SocketChannelProvider {
8
+ public abstract class BaseSocketChannelProvider implements ConfigurableSocketChannelProvider {
8
9
9
10
/**
10
11
* Limit of retries.
@@ -14,46 +15,33 @@ public abstract class BaseSocketChannelProvider implements SocketChannelProvider
14
15
/**
15
16
* Timeout to establish socket connection with an individual server.
16
17
*/
17
- private int timeout = NO_TIMEOUT ;
18
+ private int connectionTimeout = NO_TIMEOUT ;
18
19
19
20
/**
20
21
* Tries to establish a new connection to the Tarantool instances.
21
22
*
22
- * @param retryNumber number of current retry. Reset after successful connect.
23
+ * @param retryNumber number of current retry
23
24
* @param lastError the last error occurs when reconnecting
24
25
*
25
26
* @return connected socket channel
26
27
*
27
- * @throws CommunicationException if any I/O errors happen or there are
28
- * no addresses available
28
+ * @throws CommunicationException if number of retries or socket timeout are exceeded
29
+ * @throws SocketProviderTransientException if any I/O errors happen
29
30
*/
30
31
@ Override
31
32
public final SocketChannel get (int retryNumber , Throwable lastError ) {
32
33
if (areRetriesExhausted (retryNumber )) {
33
34
throw new CommunicationException ("Connection retries exceeded." , lastError );
34
35
}
35
36
36
- long deadline = System .currentTimeMillis () + timeout ;
37
- while (!Thread .currentThread ().isInterrupted ()) {
38
- try {
39
- InetSocketAddress address = getAddress (retryNumber , lastError );
40
- return openChannel (address );
41
- } catch (IOException e ) {
42
- checkTimeout (deadline , e );
43
- }
44
- }
45
- throw new CommunicationException ("Thread interrupted." , new InterruptedException ());
46
- }
47
-
48
- private void checkTimeout (long deadline , Exception e ) {
49
- long timeLeft = deadline - System .currentTimeMillis ();
50
- if (timeLeft <= 0 ) {
51
- throw new CommunicationException ("Connection time out." , e );
52
- }
53
37
try {
54
- Thread .sleep (timeLeft / 10 );
55
- } catch (InterruptedException ignored ) {
56
- Thread .currentThread ().interrupt ();
38
+ InetSocketAddress address = getAddress (retryNumber , lastError );
39
+ return openChannel (address );
40
+ } catch (SocketTimeoutException e ) {
41
+ throw new CommunicationException ("Connection timed out" , e );
42
+ } catch (IOException e ) {
43
+ System .out .println (e );
44
+ throw new SocketProviderTransientException ("Couldn't connect to server" , e );
57
45
}
58
46
}
59
47
@@ -79,6 +67,7 @@ private void checkTimeout(long deadline, Exception e) {
79
67
*
80
68
* @param retriesLimit Limit of retries to use.
81
69
*/
70
+ @ Override
82
71
public void setRetriesLimit (int retriesLimit ) {
83
72
this .retriesLimit = retriesLimit ;
84
73
}
@@ -111,7 +100,7 @@ protected SocketChannel openChannel(InetSocketAddress socketAddress) throws IOEx
111
100
SocketChannel channel = null ;
112
101
try {
113
102
channel = SocketChannel .open ();
114
- channel .socket ().connect (socketAddress , timeout );
103
+ channel .socket ().connect (socketAddress , connectionTimeout );
115
104
return channel ;
116
105
} catch (IOException e ) {
117
106
if (channel != null ) {
@@ -126,36 +115,37 @@ protected SocketChannel openChannel(InetSocketAddress socketAddress) throws IOEx
126
115
}
127
116
128
117
/**
129
- * Sets maximum amount of time to wait for a socket connection establishment
130
- * with an individual server.
131
- * <p>
132
- * Zero means infinite timeout.
133
- *
134
- * @param timeout timeout value, ms.
118
+ * Gets maximum amount of time to wait for a socket
119
+ * connection establishment with an individual server.
135
120
*
136
- * @throws IllegalArgumentException if timeout is negative.
121
+ * @return timeout
137
122
*/
138
- public void setTimeout (int timeout ) {
139
- if (timeout < 0 ) {
140
- throw new IllegalArgumentException ("timeout is negative." );
141
- }
142
- this .timeout = timeout ;
123
+ public int getConnectionTimeout () {
124
+ return connectionTimeout ;
143
125
}
144
126
145
127
/**
146
- * Gest maximum amount of time to wait for a socket
147
- * connection establishment with an individual server.
128
+ * Sets maximum amount of time to wait for a socket connection establishment
129
+ * with an individual server.
130
+ * <p>
131
+ * Zero means infinite connectionTimeout.
148
132
*
149
- * @return timeout
133
+ * @param connectionTimeout connectionTimeout value, ms.
134
+ *
135
+ * @throws IllegalArgumentException if connectionTimeout is negative.
150
136
*/
151
- public int getTimeout () {
152
- return timeout ;
137
+ @ Override
138
+ public void setConnectionTimeout (int connectionTimeout ) {
139
+ if (connectionTimeout < 0 ) {
140
+ throw new IllegalArgumentException ("connectionTimeout is negative." );
141
+ }
142
+ this .connectionTimeout = connectionTimeout ;
153
143
}
154
144
155
145
/**
156
146
* Provides a decision on whether retries limit is hit.
157
147
*
158
- * @param retries Current count of retries.
148
+ * @param retries current count of retries.
159
149
*
160
150
* @return {@code true} if retries are exhausted.
161
151
*/
@@ -166,4 +156,5 @@ private boolean areRetriesExhausted(int retries) {
166
156
}
167
157
return retries >= limit ;
168
158
}
159
+
169
160
}
0 commit comments