Skip to content

Commit

Permalink
Daemon mode: wait in fetch_poll().
Browse files Browse the repository at this point in the history
  • Loading branch information
gmp216 committed Oct 31, 2016
1 parent 0e30b26 commit 0711031
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
34 changes: 23 additions & 11 deletions child-fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>

#include "fdm.h"
#include "deliver.h"
Expand Down Expand Up @@ -134,10 +135,13 @@ fetch_poll(struct account *a, struct iolist *iol, struct io *pio, int timeout)
struct io *rio;
char *cause;
double tim;
int dur;

log_debug3(
"%s: polling: %u, timeout=%d", a->name, ARRAY_LENGTH(iol), timeout);
tim = get_time();

restart:
log_debug3("%s: polling: %u, timeout=%d, wake=%d", a->name,
ARRAY_LENGTH(iol), timeout, a->wakein);
switch (io_polln(
ARRAY_DATA(iol), ARRAY_LENGTH(iol), &rio, timeout, &cause)) {
case 0:
Expand All @@ -154,7 +158,12 @@ fetch_poll(struct account *a, struct iolist *iol, struct io *pio, int timeout)
xfree(cause);
return (-1);
}
tim = get_time() - tim;
dur = (int)floor(get_time() - tim);
if (a->wakein && (a->wakein > dur)) { /* poll returned early, sleep. */
sleep(a->wakein - dur);
timeout = 0;
goto restart;
}

return (0);
}
Expand Down Expand Up @@ -371,7 +380,7 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
sigusr1 = 0;
}

fetch_blocked = 0;
fetch_blocked = a->wakein = 0;

/* Check for new privsep messages. */
msgp = NULL;
Expand Down Expand Up @@ -426,11 +435,10 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
/* Fetch again - no blocking. */
log_debug3("%s: fetch, again", a->name);
continue;
case FETCH_RESTART:
log_debug("%s: sleeping",a->name);
sleep(conf.fetch_freq);
log_debug("%s: fetch, restart",a->name);
continue;
case FETCH_WAIT:
log_debug("%s: fetch, restart (%u secs)",
a->name,a->wakein);
break;
case FETCH_BLOCK:
/* Fetch again - allow blocking. */
log_debug3("%s: fetch, block", a->name);
Expand Down Expand Up @@ -464,7 +472,11 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
* non-empty, we can block unless there are mails that aren't
* blocked (these mails can continue to be processed).
*/
timeout = conf.timeout;

if (a->wakein)
timeout = a->wakein;
else
timeout = conf.timeout;
if (fetch_queued == 0 && ARRAY_LENGTH(&iol) == 1)
timeout = 0;
else if (fetch_queued != 0 && fetch_blocked != fetch_queued)
Expand Down Expand Up @@ -511,7 +523,7 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)

/* In daemon mode, always try to restart. */
if (conf.daemon) {
sleep(conf.fetch_freq);
log_debug("%s: restarting", a->name);
goto restart;
} else
return (aborted);
Expand Down
1 change: 1 addition & 0 deletions fdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ struct account {

int disabled;
int keep;
int wakein; /* secs */
int timeout;

struct fetch *fetch;
Expand Down
2 changes: 1 addition & 1 deletion fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#define FETCH_ERROR 3
#define FETCH_MAIL 4
#define FETCH_EXIT 5
#define FETCH_RESTART 6
#define FETCH_WAIT 6

/* Fetch flags. */
#define FETCH_PURGE 0x1
Expand Down
3 changes: 2 additions & 1 deletion imap-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,8 @@ imap_state_close(struct account *a, struct fetch_ctx *fctx)
if (conf.daemon) {
data->folder = 0; // go back to the first folder.
fctx->state = imap_state_select1;
return (FETCH_RESTART);
a->wakein = conf.fetch_freq;
return (FETCH_WAIT);
}

if (imap_putln(a, "%u LOGOUT", ++data->tag) != 0)
Expand Down
2 changes: 1 addition & 1 deletion io.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ io_polln(struct io **iop, u_int n, struct io **rio, int timeout, char **cause)
xfree(pfds);

if (error == 0) {
if (timeout == 0) {
if (timeout != conf.timeout) {
errno = EAGAIN;
return (-1);
}
Expand Down

0 comments on commit 0711031

Please sign in to comment.