Skip to content

Commit e8ed1f5

Browse files
committed
feat(core): add documentation
- Added documentation of Connection interface. - Added documentation of ConnectionCloseEvent enum.
1 parent d6cbcab commit e8ed1f5

2 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎tarantool-core/src/main/java/io/tarantool/core/connection/Connection.java‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,118 @@
1414
import io.tarantool.core.protocol.IProtoMessage;
1515
import io.tarantool.core.protocol.IProtoResponse;
1616

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+
*/
1723
public interface Connection extends AutoCloseable {
1824

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+
*/
1941
CompletableFuture<Greeting> connect(InetSocketAddress addr, long timeoutMs)
2042
throws IllegalStateException;
2143

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+
*/
2254
CompletableFuture<Void> send(IProtoMessage message) throws IllegalStateException;
2355

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+
*/
2467
Connection listen(Consumer<IProtoResponse> listener);
2568

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+
*/
2681
Connection onClose(ConnectionCloseEvent event, BiConsumer<Connection, Throwable> handler);
2782

83+
/**
84+
* Returns the {@link Greeting} object.
85+
*
86+
* @return {@link Optional#empty()} if the connection is not in a connected state.
87+
*/
2888
Optional<Greeting> getGreeting();
2989

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+
*/
3096
boolean isConnected();
3197

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+
*/
32114
void shutdownClose();
33115

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. */
34124
void pause();
35125

126+
/** Resumes reading incoming data previously stopped by {@link #pause()}. */
36127
void resume();
37128

38-
void setIdleTimeout(int idleTimeout);
39-
129+
/** Returns whether reading of incoming data is currently paused. */
40130
boolean isPaused();
41131
}

‎tarantool-core/src/main/java/io/tarantool/core/connection/ConnectionCloseEvent.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@
55

66
package io.tarantool.core.connection;
77

8+
/** {@link Connection} close events. */
89
public enum ConnectionCloseEvent {
10+
11+
/**
12+
* This event reflects a situation where the connection is closed from the network side—for
13+
* example, due to a network disconnection.
14+
*/
915
CLOSE_BY_REMOTE,
16+
17+
/**
18+
* An event reflecting a situation where the connection has been closed by the client-side calling
19+
* code.
20+
*/
1021
CLOSE_BY_CLIENT,
22+
23+
/**
24+
* This event reflects a situation where a connection is closed from the Tarantool side via a call
25+
* to {@code box.shutdown}. Unlike {@link #CLOSE_BY_REMOTE}, this event does not indicate network
26+
* issues; rather, it signifies that a message has been received from a Tarantool node initiating
27+
* a graceful shutdown.
28+
*
29+
* @see <a
30+
* href="https://www.tarantool.io/ru/doc/latest/reference/internals/iproto/graceful_shutdown/">Tarantool
31+
* documentation</a>
32+
*/
1133
CLOSE_BY_SHUTDOWN,
34+
35+
/** An event representing a situation where a connection has been closed due to a timeout */
1236
CLOSE_BY_TIMEOUT;
1337
}

0 commit comments

Comments
 (0)