495 if(!event) { |
495 if(!event) { |
496 connection_destroy(conn); |
496 connection_destroy(conn); |
497 return; |
497 return; |
498 } |
498 } |
499 |
499 |
500 EVWatchList *keepalive = malloc(sizeof(EVWatchList)); |
|
501 if(!keepalive) { |
|
502 free(event); |
|
503 connection_destroy(conn); |
|
504 return; |
|
505 } |
|
506 |
|
507 ZERO(keepalive, sizeof(EVWatchList)); |
|
508 keepalive->destroy = evt_keep_alive_destroy; |
|
509 keepalive->data = conn; |
|
510 |
|
511 ZERO(event, sizeof(Event)); |
500 ZERO(event, sizeof(Event)); |
512 event->fn = evt_keep_alive_enqueue; |
501 event->fn = evt_keep_alive_enqueue; |
513 event->finish = ev_free_event; // this will free the event obj at the end |
502 event->finish = ev_free_event; // this will free the event obj at the end |
514 event->cookie = keepalive; |
503 event->cookie = conn; |
515 |
504 |
516 EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler); |
505 EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler); |
517 if(event_send(ev, event)) { |
506 if(event_send(ev, event)) { |
518 log_ereport(LOG_FAILURE, "Keep-Alive: ev_send failed"); |
507 log_ereport(LOG_FAILURE, "Keep-Alive: ev_send failed"); |
519 connection_destroy(conn); |
508 connection_destroy(conn); |
520 free(event); |
509 free(event); |
521 free(keepalive); |
510 } |
522 } |
511 } |
523 } |
512 |
524 |
513 int evt_keep_alive_enqueue(EventHandler *h, Event *event) { |
525 int evt_keep_alive_enqueue(EventHandler *h, Event *event) { |
514 Connection *conn = event->cookie; |
526 EVWatchList *keepalive = event->cookie; |
515 |
527 Connection *conn = keepalive->data; |
516 EVWatchList *keepalive = malloc(sizeof(EVWatchList)); |
|
517 if(!keepalive) { |
|
518 connection_destroy(conn); |
|
519 return 0; |
|
520 } |
528 |
521 |
529 Event *ioevent = malloc(sizeof(Event)); |
522 Event *ioevent = malloc(sizeof(Event)); |
530 if(!ioevent) { |
523 if(!ioevent) { |
531 connection_destroy(conn); |
524 connection_destroy(conn); |
532 free(keepalive); |
525 free(keepalive); |
533 return 0; |
526 return 0; |
534 } |
527 } |
535 |
528 |
536 // add keepalive object to the eventhandler watchlist |
529 // add keepalive object to the eventhandler watchlist |
537 // the watchlist will check the timeout |
530 // the watchlist will check the timeout |
|
531 ZERO(keepalive, sizeof(EVWatchList)); |
|
532 keepalive->data1 = conn; |
|
533 keepalive->data2 = ioevent; |
|
534 keepalive->destroy = evt_keep_alive_destroy; |
538 keepalive->created = time(NULL); |
535 keepalive->created = time(NULL); |
539 keepalive->expire = keepalive->created + 60; // TODO: config |
536 keepalive->expire = keepalive->created + 10; // TODO: config |
540 ev_watchlist_add(h, keepalive); |
537 ev_watchlist_add(h, keepalive); |
541 |
538 |
542 // wait for input |
539 // wait for input |
543 ZERO(ioevent, sizeof(Event)); |
540 ZERO(ioevent, sizeof(Event)); |
544 ioevent->fn = evt_keep_alive_input_event; |
541 ioevent->fn = evt_keep_alive_input_event; |
555 return 0; |
552 return 0; |
556 } |
553 } |
557 |
554 |
558 int evt_keep_alive_input_event(EventHandler *h, Event *event) { |
555 int evt_keep_alive_input_event(EventHandler *h, Event *event) { |
559 EVWatchList *keepalive = event->cookie; |
556 EVWatchList *keepalive = event->cookie; |
560 Connection *conn = keepalive->data; |
557 Connection *conn = keepalive->data1; |
561 |
558 |
562 // remove connection from the keep-alive list |
559 // remove connection from the keep-alive list |
563 ev_watchlist_remove(h, keepalive); |
560 ev_watchlist_remove(h, keepalive); |
564 free(keepalive); |
561 free(keepalive); |
565 |
562 |
577 event->finish = evt_request_finish; |
574 event->finish = evt_request_finish; |
578 return event->fn(h, event); |
575 return event->fn(h, event); |
579 } |
576 } |
580 |
577 |
581 void evt_keep_alive_destroy(EventHandler *h, EVWatchList *item) { |
578 void evt_keep_alive_destroy(EventHandler *h, EVWatchList *item) { |
582 |
579 Connection *conn = item->data1; |
583 } |
580 Event *ioevent = item->data2; |
|
581 log_ereport(LOG_DEBUG, "sessionhandler: keep-alive timeout: close connection"); |
|
582 |
|
583 if(ev_remove_poll(h, conn->fd)) { |
|
584 log_ereport(LOG_FAILURE, "sessionhandler: keep-alive timeout: cannot remove poll"); |
|
585 } |
|
586 |
|
587 connection_destroy(conn); |
|
588 free(ioevent); |
|
589 free(item); |
|
590 } |