1 /*---------- assigning articles to conns, and transmitting ----------*/
3 static Article *dequeue_from(int peek, InputFile *ipf) {
5 if (peek) return LIST_HEAD(ipf->queue);
7 Article *art= LIST_REMHEAD(ipf->queue);
9 check_reading_pause_resume(ipf);
13 static Article *dequeue(int peek) {
15 art= dequeue_from(peek, flushing_input_file); if (art) return art;
16 art= dequeue_from(peek, backlog_input_file); if (art) return art;
17 art= dequeue_from(peek, main_input_file); if (art) return art;
21 static void check_assign_articles(void) {
27 int spare=0, inqueue=0;
29 /* Find a connection to offer this article. We prefer a busy
30 * connection to an idle one, provided it's not full. We take the
31 * first (oldest) and since that's stable, it will mean we fill up
32 * connections in order. That way if we have too many
33 * connections, the spare ones will go away eventually.
36 if (walk->quitting) continue;
37 inqueue= walk->sent.count + walk->priority.count
38 + walk->waiting.count;
39 spare= walk->max_queue - inqueue;
40 assert(inqueue <= max_queue_per_conn);
42 if (inqueue==0) /*idle*/ { if (!use) use= walk; }
43 else if (spare>0) /*working*/ { use= walk; break; }
46 if (!inqueue) use->since_activity= 0; /* reset idle counter */
48 Article *art= dequeue(0);
50 LIST_ADDTAIL(use->waiting, art);
51 lowvol_perperiod[lowvol_circptr]++;
54 conn_maybe_write(use);
55 } else if (allow_connect_start()) {
64 static void *conn_writeable(oop_source *l, int fd, oop_event ev, void *u) {
69 static void conn_maybe_write(Conn *conn) {
71 conn_make_some_xmits(conn);
73 loop->cancel_fd(loop, conn->fd, OOP_WRITE);
78 void *rp= conn_write_some_xmits(conn);
79 if (rp==OOP_CONTINUE) {
80 if (!conn->oopwriting) {
81 loop->on_fd(loop, conn->fd, OOP_WRITE, conn_writeable, conn);
85 } else if (rp==OOP_HALT) {
88 /* transmitted everything */
95 /*---------- expiry, flow control and deferral ----------*/
99 * to ensure articles go away eventually
100 * separate queue for each input file
102 * every period, check head of backlog queue for expiry with SMretrieve
103 * if too old: discard, and check next article
104 * also check every backlog article as we read it
106 * after too long in SEPARATED/DROPPING ie Separated/Finishing/Dropping
107 * one-off: eat queued articles from flushing and write them to defer
108 * one-off: connfail all connections which have any articles from flushing
109 * newly read articles from flushing go straight to defer
110 * this should take care of it and get us out of this state
111 * to avoid filling up ram needlessly
113 * limit number of queued articles for each ipf
114 * pause/resume inputfile tailing
117 static void check_reading_pause_resume(InputFile *ipf) {
118 if (ipf->queue.count >= max_queue_per_ipf)
119 inputfile_reading_pause(ipf);
121 inputfile_reading_resume(ipf);
124 static void article_defer(Article *art /* not on a queue */, int whichcount) {
126 if (fprintf(defer, "%s %s\n", TokenToText(art->token), art->messageid) <0
128 sysdie("write to defer file %s",path_defer);
129 article_done(art, whichcount);
132 static int article_check_expired(Article *art /* must be queued, not conn */) {
133 ARTHANDLE *artdata= SMretrieve(art->token, RETR_STAT);
134 if (artdata) { SMfreearticle(artdata); return 0; }
136 LIST_REMOVE(art->ipf->queue, art);
138 art->ipf->count_nooffer_missing++;
139 article_done(art,-1);
143 void inputfile_queue_check_expired(InputFile *ipf) {
147 Article *art= LIST_HEAD(ipf->queue);
148 int expd= article_check_expired(art);
151 check_reading_pause_resume(ipf);
154 static void article_autodefer(InputFile *ipf, Article *art) {
156 article_defer(art,-1);
159 static int has_article_in(const ArticleList *al, InputFile *ipf) {
161 for (art=LIST_HEAD(*al); art; art=LIST_NEXT(art))
162 if (art->ipf == ipf) return 1;
166 static void autodefer_input_file_articles(InputFile *ipf) {
168 while ((art= LIST_REMHEAD(ipf->queue)))
169 article_autodefer(ipf, art);
172 static void autodefer_input_file(InputFile *ipf) {
173 static const char *const abandon= "stuck";
176 autodefer_input_file_articles(ipf);
178 if (ipf->inprogress) {
181 if (has_article_in(&walk->waiting, ipf) ||
182 has_article_in(&walk->priority, ipf) ||
183 has_article_in(&walk->sent, ipf))
184 walk->quitting= abandon;
186 while (ipf->inprogress) {
188 if (walk->quitting == abandon) goto found;
189 abort(); /* where are they ?? */
192 connfail(walk, "connection is stuck or crawling,"
193 " and we need to finish flush");
194 autodefer_input_file_articles(ipf);
198 check_reading_pause_resume(ipf);
201 /*========== article transmission ==========*/
203 static XmitDetails *xmit_core(Conn *conn, const char *data, int len,
204 XmitKind kind) { /* caller must then fill in details */
205 struct iovec *v= &conn->xmit[conn->xmitu];
206 XmitDetails *d= &conn->xmitd[conn->xmitu++];
207 v->iov_base= (char*)data;
213 static void xmit_noalloc(Conn *conn, const char *data, int len) {
214 xmit_core(conn,data,len, xk_Const);
216 #define XMIT_LITERAL(lit) (xmit_noalloc(conn, (lit), sizeof(lit)-1))
218 static void xmit_artbody(Conn *conn, ARTHANDLE *ah /* consumed */) {
219 XmitDetails *d= xmit_core(conn, ah->data, ah->len, xk_Artdata);
223 static void xmit_free(XmitDetails *d) {
225 case xk_Artdata: SMfreearticle(d->info.sm_art); break;
226 case xk_Const: break;
231 static void *conn_write_some_xmits(Conn *conn) {
233 * 0: nothing more to write, no need to call us again
234 * OOP_CONTINUE: more to write but fd not writeable
235 * OOP_HALT: disaster, have destroyed conn
238 int count= conn->xmitu;
239 if (!count) return 0;
241 if (count > IOV_MAX) count= IOV_MAX;
242 ssize_t rs= writev(conn->fd, conn->xmit, count);
244 if (isewouldblock(errno)) return OOP_CONTINUE;
245 connfail(conn, "write failed: %s", strerror(errno));
252 assert(done<conn->xmitu);
253 struct iovec *vp= &conn->xmit[done];
254 XmitDetails *dp= &conn->xmitd[done];
255 assert(vp->iov_len <= SSIZE_MAX);
256 if ((size_t)rs >= vp->iov_len) {
258 xmit_free(dp); /* vp->iov_len -= vp->iov_len, etc. */
261 vp->iov_base= (char*)vp->iov_base + rs;
263 break; /* rs -= rs */
266 int newu= conn->xmitu - done;
267 memmove(conn->xmit, conn->xmit + done, newu * sizeof(*conn->xmit));
268 memmove(conn->xmitd, conn->xmitd + done, newu * sizeof(*conn->xmitd));
273 static void conn_make_some_xmits(Conn *conn) {
275 if (conn->xmitu+5 > CONNIOVS)
278 Article *art= LIST_REMHEAD(conn->priority);
279 if (!art) art= LIST_REMHEAD(conn->waiting);
282 if (art->state >= art_Wanted || (conn->stream && nocheck)) {
283 /* actually send it */
285 ARTHANDLE *artdata= SMretrieve(art->token, RETR_ALL);
288 art->state == art_Unchecked ? art_Unsolicited :
289 art->state == art_Wanted ? art_Wanted :
292 if (!artdata) art->missing= 1;
293 art->ipf->counts[art->state][ artdata ? RC_sent : RC_missing ]++;
297 XMIT_LITERAL("TAKETHIS ");
298 xmit_noalloc(conn, art->messageid, art->midlen);
299 XMIT_LITERAL("\r\n");
300 xmit_artbody(conn, artdata);
302 article_done(art, -1);
306 /* we got 235 from IHAVE */
308 xmit_artbody(conn, artdata);
310 XMIT_LITERAL(".\r\n");
314 LIST_ADDTAIL(conn->sent, art);
320 XMIT_LITERAL("CHECK ");
322 XMIT_LITERAL("IHAVE ");
323 xmit_noalloc(conn, art->messageid, art->midlen);
324 XMIT_LITERAL("\r\n");
326 assert(art->state == art_Unchecked);
327 art->ipf->counts[art->state][RC_sent]++;
328 LIST_ADDTAIL(conn->sent, art);