/* Grab reference to avoid early destruction */
REF_RETAIN (session);
session->io.data = session;
- ev_io_init (&session->io, rspamd_fuzzy_reply_io, session->fd, EV_WRITE);
+ ev_io_init (&session->io,
+ rspamd_fuzzy_reply_io, session->fd, EV_WRITE);
ev_io_start (session->ctx->event_loop, &session->io);
}
else {
rspamd_fuzzy_write_reply (session);
}
-static void
-fuzzy_peer_send_io (EV_P_ ev_io *w, int revents)
+static gboolean
+fuzzy_peer_try_send (gint fd, struct fuzzy_peer_request *up_req)
{
- struct fuzzy_peer_request *up_req = (struct fuzzy_peer_request *)w->data;
gssize r;
- r = write (w->fd, &up_req->cmd, sizeof (up_req->cmd));
+ r = write (fd, &up_req->cmd, sizeof (up_req->cmd));
if (r != sizeof (up_req->cmd)) {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static void
+fuzzy_peer_send_io (EV_P_ ev_io *w, int revents)
+{
+ struct fuzzy_peer_request *up_req = (struct fuzzy_peer_request *)w->data;
+
+ if (!fuzzy_peer_try_send (w->fd, up_req)) {
msg_err ("cannot send update request to the peer: %s", strerror (errno));
}
sizeof (up_req->cmd.cmd.shingle.sgl));
}
- up_req->io_ev.data = up_req;
- ev_io_init (&up_req->io_ev, fuzzy_peer_send_io,
- session->ctx->peer_fd, EV_WRITE);
- ev_io_start (session->ctx->event_loop, &up_req->io_ev);
+ if (!fuzzy_peer_try_send (session->ctx->peer_fd, up_req)) {
+ up_req->io_ev.data = up_req;
+ ev_io_init (&up_req->io_ev, fuzzy_peer_send_io,
+ session->ctx->peer_fd, EV_WRITE);
+ ev_io_start (session->ctx->event_loop, &up_req->io_ev);
+ }
+ else {
+ g_free (up_req);
+ }
}
}
(gpointer)&up_req->cmd.cmd.shingle :
(gpointer)&up_req->cmd.cmd.normal;
memcpy (ptr, cmd, up_len);
- up_req->io_ev.data = up_req;
- ev_io_init (&up_req->io_ev, fuzzy_peer_send_io,
- session->ctx->peer_fd, EV_WRITE);
- ev_io_start (session->ctx->event_loop, &up_req->io_ev);
+
+ if (!fuzzy_peer_try_send (session->ctx->peer_fd, up_req)) {
+ up_req->io_ev.data = up_req;
+ ev_io_init (&up_req->io_ev, fuzzy_peer_send_io,
+ session->ctx->peer_fd, EV_WRITE);
+ ev_io_start (session->ctx->event_loop, &up_req->io_ev);
+ }
+ else {
+ g_free (up_req);
+ }
}
result.v1.value = 0;
(struct rspamd_fuzzy_storage_ctx *)w->data;
gssize r;
- r = read (w->fd, &cmd, sizeof (cmd));
+ for (;;) {
+ r = read (w->fd, &cmd, sizeof (cmd));
- if (r != sizeof (cmd)) {
- if (errno == EINTR) {
- rspamd_fuzzy_peer_io (EV_A_ w, revents);
- return;
+ if (r != sizeof (cmd)) {
+ if (errno == EINTR) {
+ continue;
+ }
+ if (errno != EAGAIN) {
+ msg_err ("cannot read command from peers: %s", strerror (errno));
+ }
+
+ break;
}
- if (errno != EAGAIN) {
- msg_err ("cannot read command from peers: %s", strerror (errno));
+ else {
+ g_array_append_val (ctx->updates_pending, cmd);
}
}
- else {
- g_array_append_val (ctx->updates_pending, cmd);
- }
}
static void