Skip to content

Commit 6d4bce2

Browse files
committed
Client: receive and parse greeting asynchronously
Before this patch the greetings was received in blocking mode right after blocking connect. This patch postpones greetings to the first reading. Part of #28
1 parent 6581de7 commit 6d4bce2

File tree

3 files changed

+33
-40
lines changed

3 files changed

+33
-40
lines changed

src/Client/Connection.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct ConnectionImpl
8484
//Members below can be default-initialized.
8585
ConnectionError error;
8686
Greeting greeting;
87+
bool is_greeting_received;
8788
std::unordered_map<rid_t, Response<BUFFER>> futures;
8889

8990
static constexpr size_t AVAILABLE_IOVEC_COUNT = 32;
@@ -568,13 +569,14 @@ int
568569
decodeGreeting(Connection<BUFFER, NetProvider> &conn)
569570
{
570571
//TODO: that's not zero-copy, should be rewritten in that pattern.
572+
assert(conn.getInBuf().has(conn.impl->endDecoded, Iproto::GREETING_SIZE));
571573
char greeting_buf[Iproto::GREETING_SIZE];
572574
conn.impl->endDecoded.read({greeting_buf, sizeof(greeting_buf)});
573-
assert(conn.impl->endDecoded == conn.impl->inBuf.end());
574575
conn.impl->dec.reset(conn.impl->endDecoded);
575576
if (parseGreeting(std::string_view{greeting_buf, Iproto::GREETING_SIZE},
576577
conn.impl->greeting) != 0)
577578
return -1;
579+
conn.impl->is_greeting_received = true;
578580
LOG_DEBUG("Version: ", conn.impl->greeting.version_id);
579581

580582
#ifndef NDEBUG

src/Client/EpollNetProvider.hpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,7 @@ EpollNetProvider<BUFFER, NETWORK>::connect(Conn_t &conn,
145145
return -1;
146146
}
147147
LOG_DEBUG("Connected to ", addr, ", socket is ", socket);
148-
/* Receive and decode greetings. */
149-
size_t iov_cnt = 0;
150-
struct iovec *iov =
151-
inBufferToIOV(conn, Iproto::GREETING_SIZE, &iov_cnt);
152-
LOG_DEBUG("Receiving greetings...");
153-
int read_bytes = NETWORK::recvall(socket, iov, iov_cnt, false);
154-
if (read_bytes < 0) {
155-
conn.setError(std::string("Failed to receive greetings: ") +
156-
strerror(errno));
157-
::close(socket);
158-
return -1;
159-
}
160-
LOG_DEBUG("Greetings are received, read bytes ", read_bytes);
161-
if (decodeGreeting(conn) != 0) {
162-
conn.setError(std::string("Failed to decode greetings"));
163-
::close(socket);
164-
return -1;
165-
}
166-
LOG_DEBUG("Greetings are decoded");
167-
LOG_DEBUG("Authentication processing...");
168-
//TODO: add authentication step.
148+
conn.getImpl()->is_greeting_received = false;
169149
conn.setSocket(socket);
170150
registerEpoll(conn);
171151
return 0;
@@ -232,6 +212,20 @@ EpollNetProvider<BUFFER, NETWORK>::recv(Conn_t &conn)
232212
strerror(errno), errno);
233213
return -1;
234214
}
215+
216+
if (!conn.getImpl()->is_greeting_received) {
217+
if ((size_t) rcvd < Iproto::GREETING_SIZE)
218+
return 0;
219+
/* Receive and decode greetings. */
220+
LOG_DEBUG("Greetings are received, read bytes ", rcvd);
221+
if (decodeGreeting(conn) != 0) {
222+
conn.setError("Failed to decode greetings");
223+
return -1;
224+
}
225+
LOG_DEBUG("Greetings are decoded");
226+
rcvd -= Iproto::GREETING_SIZE;
227+
}
228+
235229
return 0;
236230
}
237231

src/Client/LibevNetProvider.hpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ connectionReceive(Connection<BUFFER, LibevNetProvider<BUFFER, NETWORK>> &conn)
139139
strerror(errno));
140140
return -1;
141141
}
142+
143+
if (!conn.getImpl()->is_greeting_received) {
144+
if ((size_t) rcvd < Iproto::GREETING_SIZE)
145+
return 0;
146+
/* Receive and decode greetings. */
147+
LOG_DEBUG("Greetings are received, read bytes ", rcvd);
148+
if (decodeGreeting(conn) != 0) {
149+
conn.setError("Failed to decode greetings");
150+
return -1;
151+
}
152+
LOG_DEBUG("Greetings are decoded");
153+
rcvd -= Iproto::GREETING_SIZE;
154+
}
155+
142156
return 0;
143157
}
144158

@@ -289,25 +303,8 @@ LibevNetProvider<BUFFER, NETWORK>::connect(Conn_t &conn,
289303
return -1;
290304
}
291305
LOG_DEBUG("Connected to ", addr, ", socket is ", socket);
292-
/* Receive and decode greetings. */
293-
size_t iov_cnt = 0;
294-
struct iovec *iov = inBufferToIOV(conn, Iproto::GREETING_SIZE, &iov_cnt);
295-
LOG_DEBUG("Receiving greetings...");
296-
int read_bytes = NETWORK::recvall(socket, iov, iov_cnt, false);
297-
if (read_bytes < 0) {
298-
conn.setError(std::string("Failed to receive greetings: ") +
299-
strerror(errno));
300-
::close(socket);
301-
return -1;
302-
}
303-
LOG_DEBUG("Greetings are received, read bytes ", read_bytes);
304-
if (decodeGreeting(conn) != 0) {
305-
conn.setError(std::string("Failed to decode greetings"));
306-
::close(socket);
307-
return -1;
308-
}
306+
conn.getImpl()->is_greeting_received = false;
309307

310-
LOG_DEBUG("Greetings are decoded");
311308
registerWatchers(conn, socket);
312309

313310
conn.setSocket(socket);

0 commit comments

Comments
 (0)