Skip to content

Commit f68efd7

Browse files
committed
Daemon mode: wait in fetch_poll().
1 parent 0e30b26 commit f68efd7

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

child-fetch.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <string.h>
2828
#include <time.h>
2929
#include <unistd.h>
30+
#include <math.h>
3031

3132
#include "fdm.h"
3233
#include "deliver.h"
@@ -134,10 +135,13 @@ fetch_poll(struct account *a, struct iolist *iol, struct io *pio, int timeout)
134135
struct io *rio;
135136
char *cause;
136137
double tim;
138+
int dur;
137139

138-
log_debug3(
139-
"%s: polling: %u, timeout=%d", a->name, ARRAY_LENGTH(iol), timeout);
140140
tim = get_time();
141+
142+
restart:
143+
log_debug3("%s: polling: %u, timeout=%d, wake=%d", a->name,
144+
ARRAY_LENGTH(iol), timeout, a->wakein);
141145
switch (io_polln(
142146
ARRAY_DATA(iol), ARRAY_LENGTH(iol), &rio, timeout, &cause)) {
143147
case 0:
@@ -154,7 +158,12 @@ fetch_poll(struct account *a, struct iolist *iol, struct io *pio, int timeout)
154158
xfree(cause);
155159
return (-1);
156160
}
157-
tim = get_time() - tim;
161+
dur = (int)floor(get_time() - tim);
162+
if (a->wakein && (a->wakein > dur)) { /* poll returned early, sleep. */
163+
sleep(a->wakein - dur);
164+
timeout = a->wakein = 0;
165+
goto restart;
166+
}
158167

159168
return (0);
160169
}
@@ -360,7 +369,7 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
360369

361370
ARRAY_INIT(&iol);
362371

363-
aborted = complete = holding = 0;
372+
aborted = complete = holding = a->wakein = 0;
364373
for (;;) {
365374
log_debug3("%s: fetch loop start", a->name);
366375

@@ -426,11 +435,10 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
426435
/* Fetch again - no blocking. */
427436
log_debug3("%s: fetch, again", a->name);
428437
continue;
429-
case FETCH_RESTART:
430-
log_debug("%s: sleeping",a->name);
431-
sleep(conf.fetch_freq);
432-
log_debug("%s: fetch, restart",a->name);
433-
continue;
438+
case FETCH_WAIT:
439+
log_debug("%s: fetch, restart (%u secs)",
440+
a->name,a->wakein);
441+
break;
434442
case FETCH_BLOCK:
435443
/* Fetch again - allow blocking. */
436444
log_debug3("%s: fetch, block", a->name);
@@ -464,7 +472,11 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
464472
* non-empty, we can block unless there are mails that aren't
465473
* blocked (these mails can continue to be processed).
466474
*/
467-
timeout = conf.timeout;
475+
476+
if (a->wakein)
477+
timeout = a->wakein;
478+
else
479+
timeout = conf.timeout;
468480
if (fetch_queued == 0 && ARRAY_LENGTH(&iol) == 1)
469481
timeout = 0;
470482
else if (fetch_queued != 0 && fetch_blocked != fetch_queued)
@@ -511,7 +523,7 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
511523

512524
/* In daemon mode, always try to restart. */
513525
if (conf.daemon) {
514-
sleep(conf.fetch_freq);
526+
log_debug("%s: restarting", a->name);
515527
goto restart;
516528
} else
517529
return (aborted);

fdm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ struct account {
490490

491491
int disabled;
492492
int keep;
493+
int wakein; /* secs */
493494
int timeout;
494495

495496
struct fetch *fetch;

fetch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define FETCH_ERROR 3
2626
#define FETCH_MAIL 4
2727
#define FETCH_EXIT 5
28-
#define FETCH_RESTART 6
28+
#define FETCH_WAIT 6
2929

3030
/* Fetch flags. */
3131
#define FETCH_PURGE 0x1

imap-common.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ imap_state_close(struct account *a, struct fetch_ctx *fctx)
11231123
if (conf.daemon) {
11241124
data->folder = 0; // go back to the first folder.
11251125
fctx->state = imap_state_select1;
1126-
return (FETCH_RESTART);
1126+
a->wakein = conf.fetch_freq;
1127+
return (FETCH_WAIT);
11271128
}
11281129

11291130
if (imap_putln(a, "%u LOGOUT", ++data->tag) != 0)

io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ io_polln(struct io **iop, u_int n, struct io **rio, int timeout, char **cause)
158158
xfree(pfds);
159159

160160
if (error == 0) {
161-
if (timeout == 0) {
161+
if (timeout != conf.timeout) {
162162
errno = EAGAIN;
163163
return (-1);
164164
}

0 commit comments

Comments
 (0)