27
27
#include <string.h>
28
28
#include <time.h>
29
29
#include <unistd.h>
30
+ #include <math.h>
30
31
31
32
#include "fdm.h"
32
33
#include "deliver.h"
@@ -134,10 +135,13 @@ fetch_poll(struct account *a, struct iolist *iol, struct io *pio, int timeout)
134
135
struct io * rio ;
135
136
char * cause ;
136
137
double tim ;
138
+ int dur ;
137
139
138
- log_debug3 (
139
- "%s: polling: %u, timeout=%d" , a -> name , ARRAY_LENGTH (iol ), timeout );
140
140
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 );
141
145
switch (io_polln (
142
146
ARRAY_DATA (iol ), ARRAY_LENGTH (iol ), & rio , timeout , & cause )) {
143
147
case 0 :
@@ -154,7 +158,12 @@ fetch_poll(struct account *a, struct iolist *iol, struct io *pio, int timeout)
154
158
xfree (cause );
155
159
return (-1 );
156
160
}
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
+ }
158
167
159
168
return (0 );
160
169
}
@@ -360,7 +369,7 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
360
369
361
370
ARRAY_INIT (& iol );
362
371
363
- aborted = complete = holding = 0 ;
372
+ aborted = complete = holding = a -> wakein = 0 ;
364
373
for (;;) {
365
374
log_debug3 ("%s: fetch loop start" , a -> name );
366
375
@@ -426,11 +435,10 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
426
435
/* Fetch again - no blocking. */
427
436
log_debug3 ("%s: fetch, again" , a -> name );
428
437
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 ;
434
442
case FETCH_BLOCK :
435
443
/* Fetch again - allow blocking. */
436
444
log_debug3 ("%s: fetch, block" , a -> name );
@@ -464,7 +472,11 @@ fetch_account(struct account *a, struct io *pio, int nflags, double tim)
464
472
* non-empty, we can block unless there are mails that aren't
465
473
* blocked (these mails can continue to be processed).
466
474
*/
467
- timeout = conf .timeout ;
475
+
476
+ if (a -> wakein )
477
+ timeout = a -> wakein ;
478
+ else
479
+ timeout = conf .timeout ;
468
480
if (fetch_queued == 0 && ARRAY_LENGTH (& iol ) == 1 )
469
481
timeout = 0 ;
470
482
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)
511
523
512
524
/* In daemon mode, always try to restart. */
513
525
if (conf .daemon ) {
514
- sleep ( conf . fetch_freq );
526
+ log_debug ( "%s: restarting" , a -> name );
515
527
goto restart ;
516
528
} else
517
529
return (aborted );
0 commit comments