void tickertask(void *v) { (void)v; taskname("ticker"); while(!task_was_signaled()) { THE_CURRENT_TIME_IS = time(NULL); int min_wait = Setting_get_int("limits.tick_timer", 10); taskdelay(min_wait * 1000); // avoid doing this during a reload attempt if(!RELOAD) { // don't bother if these are all 0 int min_ping = Setting_get_int("limits.min_ping", DEFAULT_MIN_PING); int min_write_rate = Setting_get_int("limits.min_write_rate", DEFAULT_MIN_READ_RATE); int min_read_rate = Setting_get_int("limits.min_read_rate", DEFAULT_MIN_WRITE_RATE); if(min_ping > 0 || min_write_rate > 0 || min_read_rate > 0) { int cleared = Register_cleanout(); if(cleared > 0) { log_warn("Timeout task killed %d tasks, waiting %d seconds for more.", cleared, min_wait); } else { debug("No connections timed out."); } } // do a server queue cleanup to get rid of dead servers Server_queue_cleanup(); } } }
int Server_run() { int rc = 0; Server *srv = Server_queue_latest(); check(srv != NULL, "Failed to get a server from the configured queue."); int listen_fd = srv->listen_fd; taskname("SERVER"); while(RUNNING) { rc = Server_accept(listen_fd); if(rc == -1 && RUNNING) { log_err("Server accept failed, attempting to clear out dead weight: %d", RUNNING); int cleared = Register_cleanout(); if(cleared == 0) { taskdelay(1000); } } } return 0; error: return -1; }
int Server_stop_handlers(Server *srv) { int i = 0; for(i = 0; i < darray_end(srv->handlers); i++) { Handler *handler = darray_get(srv->handlers, i); check(handler != NULL, "Invalid handler, can't be NULL."); if(handler->running) { log_info("STOPPING HANDLER %s", bdata(handler->send_spec)); if(handler->task != NULL) { tasksignal(handler->task, SIGINT); handler->running = 0; taskdelay(1); } } if(handler->recv_socket) zmq_close(handler->recv_socket); if(handler->send_socket) zmq_close(handler->send_socket); handler->recv_socket = NULL; handler->send_socket = NULL; } return 0; error: return -1; }
void delaytask(void *v) { taskdelay((int) v); printf("awake after %d ms\n", (int) v); chansendul(c, 0); }
void *Handler_recv_create(const char *recv_spec, const char *uuid) { int bind_attempts = 10; void *listener_socket = mqsocket(ZMQ_SUB); check(listener_socket, "Can't create ZMQ_SUB socket."); int rc = zmq_setsockopt(listener_socket, ZMQ_SUBSCRIBE, uuid, strlen(uuid)); check(rc == 0, "Failed to subscribe listener socket: %s", recv_spec); log_info("Binding listener SUB socket %s subscribed to: %s", recv_spec, uuid); rc = zmq_bind(listener_socket, recv_spec); while(rc != 0 && bind_attempts-- > 0) { taskdelay(1000); debug("Failed to bind recv socket trying again."); rc = zmq_bind(listener_socket, recv_spec); } check(bind_attempts > 0, "Too many bind attempts for handler %s", recv_spec); return listener_socket; error: return NULL; }
void test(void *data) { while (1) { puts("test"); taskdelay(500); } }
void Handler_task(void *v) { int rc = 0; int i = 0; Handler *handler = (Handler *)v; HandlerParser *parser = NULL; int max_targets = Setting_get_int("limits.handler_targets", 128); log_info("MAX allowing limits.handler_targets=%d", max_targets); parser = HandlerParser_create(max_targets); check_mem(parser); check(Handler_setup(handler) == 0, "Failed to initialize handler, exiting."); while(handler->running && !task_was_signaled()) { taskstate("delivering"); rc = handler_recv_parse(handler, parser); if(task_was_signaled()) { log_warn("Handler task signaled, exiting."); break; } else if( rc == -1 || parser->target_count <= 0) { log_warn("Skipped invalid message from handler: %s", bdata(handler->send_spec)); taskdelay(100); continue; } else { for(i = 0; i < (int)parser->target_count; i++) { int id = (int)parser->targets[i]; int fd = Register_fd_for_id(id); Connection *conn = fd == -1 ? NULL : Register_fd_exists(fd); // don't bother calling process request if there's nothing to handle if(conn && fd >= 0) { handler_process_request(handler, id, fd, conn, parser->body); } else { // TODO: I believe we need to notify the connection that it is dead too Handler_notify_leave(handler, id); } } } HandlerParser_reset(parser); } handler->running = 0; handler->task = NULL; HandlerParser_destroy(parser); taskexit(0); error: log_err("HANDLER TASK DIED: %s", bdata(handler->send_spec)); handler->running = 0; handler->task = NULL; HandlerParser_destroy(parser); taskexit(1); }
void final_setup() { start_terminator(); Server_init(); bstring end_point = bfromcstr("inproc://access_log"); Server *srv = Server_queue_latest(); Log_init(bstrcpy(srv->access_log), end_point); Control_port_start(); taskdelay(500); log_info("-- " VERSION " Running. Copyright (C) Zed A. Shaw. Licensed BSD."); }
static int pireset(int fd) { static uint8 ff = 0xff; if(hexwrite(fd, &ff, 1) < 0) return -1; /* This is fairly conservative. */ taskdelay(250); return 0; }
int _pcall(Pcall *tx, Pcall *rx) { static Pcall tx1; int rv; uint preamblems, timeoutms, tries; switch(tx->type){ case Twakeup: preamblems = 2000; timeoutms = 200; tries = 10; break; case Tadjourn: preamblems = 0; tries = 1; break; default: preamblems = 0; timeoutms = 300; tries = 15; break; } if((rv=_pcall1(tx, rx, timeoutms, preamblems, tries)) != 1) return rv; while(rx->type == Rkeepalive){ /* The keepalive reply tells the remote to backoff * for the given number of milliseconds; we comply. */ taskdelay(rx->backoffms); tx1.type = Tkeepalive; if((rv=_pcall1(&tx1, rx, timeoutms*2, 0, 10)) != 1) return rv; } if(rx->type != tx->type+0xff){ werrstr("unexpected reply type %2x to request %2x", rx->type, tx->type); return -1; } return rv; }
void *Handler_send_create(const char *send_spec, const char *identity) { void *handler_socket = mqsocket(ZMQ_PUSH); int rc = zmq_setsockopt(handler_socket, ZMQ_IDENTITY, identity, strlen(identity)); check(rc == 0, "Failed to set handler socket %s identity %s", send_spec, identity); log_info("Binding handler PUSH socket %s with identity: %s", send_spec, identity); rc = zmq_bind(handler_socket, send_spec); while(rc != 0) { taskdelay(1000); log_warn("Failed to bind send socket trying again for: %s", send_spec); rc = zmq_bind(handler_socket, send_spec); } return handler_socket; error: return NULL; }
void *Handler_recv_create(const char *recv_spec, const char *uuid) { void *listener_socket = mqsocket(ZMQ_SUB); check(listener_socket, "Can't create ZMQ_SUB socket."); int rc = zmq_setsockopt(listener_socket, ZMQ_SUBSCRIBE, uuid, 0); check(rc == 0, "Failed to subscribe listener socket: %s", recv_spec); log_info("Binding listener SUB socket %s subscribed to: %s", recv_spec, uuid); rc = zmq_bind(listener_socket, recv_spec); while(rc != 0) { taskdelay(1000); debug("Failed to bind recv socket trying again."); rc = zmq_bind(listener_socket, recv_spec); } return listener_socket; error: return NULL; }
void taskmain(int argc, char **argv) { int i, n; if(argc != 4){ fprintf(stderr, "usage: httpload n server url\n"); taskexitall(1); } n = atoi(argv[1]); server = argv[2]; url = argv[3]; for(i=0; i<n; i++){ taskcreate(fetchtask, 0, STACK); //while(taskyield() > 1) // ; //taskdelay(500); } for(;;){ taskdelay(1000); fprintf(stderr,"sucess:%d, fail:%d,rate:%f%%\n",sucess,fail,((float)sucess)/(sucess+fail)*100); } }
void Connection_deliver_task_kill(Connection *conn) { if(conn && conn->deliverTaskStatus==DT_RUNNING) { debug("Killing task for connection %p",conn); int sleeptime=10; while(conn->deliverAck!=conn->deliverPost) { bdestroy(Connection_deliver_dequeue(conn)); } Connection_deliver_enqueue(conn,NULL); conn->deliverTaskStatus=DT_DYING; while(conn->deliverTaskStatus != DT_DEAD) { taskdelay(sleeptime); if(sleeptime<10000) { sleeptime<<=1; } else { log_warn("Connection %p is not dying, contact jasom\n",conn); //*((int *)0)=1; } } debug("Deliver Task Killed %p",conn); } }
void fdsignal() { SIGNALED = 1; taskdelay(1); }
inline void thread_usleep(unsigned int usec) { taskdelay(usec/1000); }
static inline void Action_sleep(int sec) { taskdelay(sec * (1000 + rand() % 1000)); }