|
14 | 14 | import io.tarantool.core.protocol.IProtoMessage; |
15 | 15 | import io.tarantool.core.protocol.IProtoResponse; |
16 | 16 |
|
| 17 | +/** |
| 18 | + * Tarantool node connection interface. |
| 19 | + * |
| 20 | + * @implSpec if an implementation of this interface represents a connection that allows reconnecting |
| 21 | + * via the same {@link Connection} object, the implementation must document this behavior. |
| 22 | + */ |
17 | 23 | public interface Connection extends AutoCloseable { |
18 | 24 |
|
| 25 | + /** |
| 26 | + * Connects to a Tarantool node asynchronously. |
| 27 | + * |
| 28 | + * @param addr the Tarantool node address, represented as an {@link InetSocketAddress} object. |
| 29 | + * Must not be null. |
| 30 | + * @param timeoutMs the time from the start of the connection attempt until the actual connection |
| 31 | + * is established. If the connection to the Tarantool node is not established after this time |
| 32 | + * expires, the connection transitions to an unconnected state, and the {@link |
| 33 | + * CompletableFuture} promise completes exceptionally. |
| 34 | + * @return a connection promise represented as a {@link CompletableFuture} object. Completes |
| 35 | + * successfully when the connection to the node is actually established, or exceptionally if |
| 36 | + * the connection fails for any reason. Never null. |
| 37 | + * @throws IllegalStateException if a connection attempt is made in a state where connecting is |
| 38 | + * prohibited. The states in which connecting is prohibited are defined by the implementation. |
| 39 | + * @throws IllegalArgumentException if {@code timeoutMs <= 0}. |
| 40 | + */ |
19 | 41 | CompletableFuture<Greeting> connect(InetSocketAddress addr, long timeoutMs) |
20 | 42 | throws IllegalStateException; |
21 | 43 |
|
| 44 | + /** |
| 45 | + * Sends a message to a Tarantool node asynchronously. |
| 46 | + * |
| 47 | + * @param message the message, represented as an {@link IProtoMessage} object. Must not be null. |
| 48 | + * @return a {@link CompletableFuture} object representing the message delivery result. Completes |
| 49 | + * successfully if the message is sent to the Tarantool node, or exceptionally if the message |
| 50 | + * fails to send. Never null. |
| 51 | + * @throws IllegalStateException if, at the time of sending, the connection is not in a |
| 52 | + * ready-to-send state. |
| 53 | + */ |
22 | 54 | CompletableFuture<Void> send(IProtoMessage message) throws IllegalStateException; |
23 | 55 |
|
| 56 | + /** |
| 57 | + * Registers a callback for handling any messages from a Tarantool node, which are represented as |
| 58 | + * an {@link IProtoMessage} object. |
| 59 | + * |
| 60 | + * @param listener the callback for handling any messages from the Tarantool node. Must not be |
| 61 | + * null. |
| 62 | + * @return the same {@link Connection} object on which this method was invoked. |
| 63 | + * @implSpec any implementation-specific details of this method - such as overwriting the previous |
| 64 | + * callback upon multiple invocations, or other distinct behaviors - must be documented in the |
| 65 | + * method's implementation documentation. |
| 66 | + */ |
24 | 67 | Connection listen(Consumer<IProtoResponse> listener); |
25 | 68 |
|
| 69 | + /** |
| 70 | + * Registers a callback to receive notifications when the connection is closed for any reason, as |
| 71 | + * reflected by a {@link ConnectionCloseEvent}. |
| 72 | + * |
| 73 | + * @param event the event that caused the connection to close. |
| 74 | + * @param handler the connection close event handler. |
| 75 | + * @return the same connection object on which this method was invoked. |
| 76 | + * @apiNote when the connection is closed for any reason, only the callbacks that were subscribed |
| 77 | + * to the corresponding event will be invoked. |
| 78 | + * @implSpec implementations must guarantee that the connection is in a closed state at the moment |
| 79 | + * the callback is invoked. |
| 80 | + */ |
26 | 81 | Connection onClose(ConnectionCloseEvent event, BiConsumer<Connection, Throwable> handler); |
27 | 82 |
|
| 83 | + /** |
| 84 | + * Returns the {@link Greeting} object. |
| 85 | + * |
| 86 | + * @return {@link Optional#empty()} if the connection is not in a connected state. |
| 87 | + */ |
28 | 88 | Optional<Greeting> getGreeting(); |
29 | 89 |
|
| 90 | + /** |
| 91 | + * Returns whether the connection is in a connected state at the time of invocation. |
| 92 | + * |
| 93 | + * @return {@code true} if the connection is in a connected state at the moment this method |
| 94 | + * executes. |
| 95 | + */ |
30 | 96 | boolean isConnected(); |
31 | 97 |
|
| 98 | + /** |
| 99 | + * Initiates connection closure of the connection asynchronously, reporting the close cause as a |
| 100 | + * graceful shutdown of the server. |
| 101 | + * |
| 102 | + * <p>This method is intended to be called upon receiving the {@code box.shutdown} notification |
| 103 | + * from Tarantool. At this moment, the connection is still operational, but the server is |
| 104 | + * preparing to stop. |
| 105 | + * |
| 106 | + * <p>In contrast to {@link #close()}, the close is reported to listeners as {@link |
| 107 | + * ConnectionCloseEvent#CLOSE_BY_SHUTDOWN} rather than {@link |
| 108 | + * ConnectionCloseEvent#CLOSE_BY_CLIENT}. Their reaction to these events is expected to differ. |
| 109 | + * |
| 110 | + * @see <a |
| 111 | + * href="https://www.tarantool.io/ru/doc/latest/reference/internals/iproto/graceful_shutdown/">"Tarantool |
| 112 | + * Documentation</a> |
| 113 | + */ |
32 | 114 | void shutdownClose(); |
33 | 115 |
|
| 116 | + /** Initiates connection closure via a {@link ConnectionCloseEvent} asynchronously. */ |
| 117 | + @Override |
| 118 | + void close() throws Exception; |
| 119 | + |
| 120 | + /** Sets the idle timeout duration in milliseconds. */ |
| 121 | + void setIdleTimeout(int idleTimeoutMs); |
| 122 | + |
| 123 | + /** Stops reading incoming data from the socket. */ |
34 | 124 | void pause(); |
35 | 125 |
|
| 126 | + /** Resumes reading incoming data previously stopped by {@link #pause()}. */ |
36 | 127 | void resume(); |
37 | 128 |
|
38 | | - void setIdleTimeout(int idleTimeout); |
39 | | - |
| 129 | + /** Returns whether reading of incoming data is currently paused. */ |
40 | 130 | boolean isPaused(); |
41 | 131 | } |
0 commit comments