void disconnectCallback(const redisAsyncContext *c, int status) { if (status != REDIS_OK) { printf("Error: %s\n", c->errstr); aeStop(loop); return; } printf("Disconnected...\n"); aeStop(loop); }
void GatewayDataMgr::disconnectCallback(const redisAsyncContext *c, int status) { if (status != REDIS_OK) { LOG(ERROR)<<"Error: "<<c->errstr; aeStop((aeEventLoop*)(c->data)); return; } LOG(INFO)<<"Disconnected..."; aeStop((aeEventLoop*)(c->data)); }
void masterKillHandler( int sig ) { kill( 0 , SIGTERM ); aeStop( servG->main_reactor->event_loop ); stopReactorThread( servG ); servG->running = 0; }
void stopReactorThread( aeServer* serv ) { int i; for( i=0;i<serv->reactorNum;i++) { aeStop( serv->reactorThreads[i].reactor.eventLoop ); } for( i=0;i<serv->reactorNum;i++) { usleep( 1000 ); pthread_cancel( serv->reactorThreads[i].thread_id ); if( pthread_join( serv->reactorThreads[i].thread_id , NULL ) ) { printf( "pthread join error \n"); } } //free memory for( i=0;i<serv->reactorNum;i++) { if( serv->reactorThreads[i].param ) { zfree( serv->reactorThreads[i].param ); } if( serv->reactorThreads[i].reactor.eventLoop != NULL ) { aeDeleteEventLoop( serv->reactorThreads[i].reactor.eventLoop ); } } }
static void check_unit_destroy(check_unit *cunit) { if (cunit->cdata != NULL) { cunit->cdata->finished_keys_count ++; if (cunit->cdata->finished_keys_count >= cunit->cdata->keys_count) { aeStop(cunit->cdata->loop); } } if (cunit->key != NULL) { sdsfree(cunit->key); cunit->key = NULL; } if (cunit->result1 != NULL) { msg_put(cunit->result1); msg_free(cunit->result1); cunit->result1 = NULL; } if (cunit->result2 != NULL) { msg_put(cunit->result2); msg_free(cunit->result2); cunit->result2 = NULL; } rmt_free(cunit); }
static int smrCbNotiReady (void *arg) { assert (!server.is_ready); server.is_ready = 1; aeStop (server.el); return 0; }
void easysocketbenchmark::stopbenchmark(){ aeStop(this->g_event_loop); std::vector<int>::iterator itr; std::vector<int>::iterator last; for(itr = this->fd_list.begin(), last = this->fd_list.end(); itr != last; ++itr){ close(*itr); } }
void stopReactorThread( appnetServer *serv ) { int i; for (i = 0; i < serv->reactor_num; i++) { aeStop( serv->reactor_threads[i].reactor.event_loop ); } }
static void clientDone(client c) { if (config.requests_finished == config.requests) { freeClient(c); aeStop(config.el); return; } if (config.keepalive) { resetClient(c); } else { config.liveclients--; createMissingClients(c); config.liveclients++; freeClient(c); } }
static int smrCbNotiRckpt (void *arg, char *be_host, int be_port) { if (server.smr_init_flags != SMR_INIT_NONE) { redisLog (REDIS_WARNING, "rckpt command is called after initialization."); exit (1); } server.need_rckpt = 1; server.ckpt_host = zstrdup (be_host); server.ckpt_port = be_port; aeStop (server.el); return 0; }
/* Process every pending time event, then every pending file event * (that may be registered by time event callbacks just processed). * Without special flags the function sleeps until some file event * fires, or when the next time event occurs (if any). * * If flags is 0, the function does nothing and returns. * if flags has AE_ALL_EVENTS set, all the kind of events are processed. * if flags has AE_FILE_EVENTS set, file events are processed. * if flags has AE_TIME_EVENTS set, time events are processed. * if flags has AE_DONT_WAIT set the function returns ASAP until all * the events that's possible to process without to wait are processed. * * The function returns the number of events processed. */ int aeProcessEvents(aeEventLoop *eventLoop, int flags) { int processed = 0, numevents; #ifdef _WIN32 if (ServiceStopIssued() == TRUE) { aeStop(eventLoop); } #endif /* Nothing to do? return ASAP */ if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0; /* Note that we want call select() even if there are no * file events to process as long as we want to process time * events, in order to sleep until the next time event is ready * to fire. */ if (eventLoop->maxfd != -1 || ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) { int j; aeTimeEvent *shortest = NULL; struct timeval tv, *tvp; if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT)) shortest = aeSearchNearestTimer(eventLoop); if (shortest) { PORT_LONG now_sec, now_ms; /* Calculate the time missing for the nearest * timer to fire. */ aeGetTime(&now_sec, &now_ms); tvp = &tv; tvp->tv_sec = (int)(shortest->when_sec - now_sec); WIN_PORT_FIX /* cast (int) */ if (shortest->when_ms < now_ms) { tvp->tv_usec = (int)((shortest->when_ms+1000) - now_ms)*1000; WIN_PORT_FIX /* cast (int) */ tvp->tv_sec --; } else { tvp->tv_usec = (int)(shortest->when_ms - now_ms)*1000; WIN_PORT_FIX /* cast (int) */ } if (tvp->tv_sec < 0) tvp->tv_sec = 0; if (tvp->tv_usec < 0) tvp->tv_usec = 0; } else { /* If we have to check for events but need to return * ASAP because of AE_DONT_WAIT we need to set the timeout * to zero */ if (flags & AE_DONT_WAIT) {
static int check_timeouts(aeEventLoop *loop, long long id, void *data) { thread *thread = data; connection *c = thread->cs; uint64_t now = time_us(); uint64_t maxAge = now - (cfg.timeout * 1000); for (uint64_t i = 0; i < thread->connections; i++, c++) { if (maxAge > c->start) { thread->errors.timeout++; } } if (stop || now >= thread->stop_at) { aeStop(loop); } return TIMEOUT_INTERVAL_MS; }
static void socket_readable(aeEventLoop *loop, int fd, void *data, int mask) { connection *c = data; thread *thread = c->thread; uint64_t now; ssize_t n; if ((n = read(fd, c->buf, sizeof(c->buf))) <= 0) goto error; c->buf[n] = '\0'; now = time_us(); if (!validate_response(c)) goto invalid; thread->bytes += n; thread->complete++; thread->requests++; stats_record(thread->latency, now - c->start); if (now >= thread->stop_at) { aeStop(thread->loop); close(c->fd); } else { reconnect_socket(c->thread, c); } return; invalid: c->thread->errors.validate++; reconnect_socket(c->thread, c); return; error: c->thread->errors.read++; reconnect_socket(c->thread, c); }
static void clientDisconnected(const redisAsyncContext *context, int status) { listNode *ln; client c = (client)context->data; if (status != REDIS_OK) { fprintf(stderr,"Disconnected: %s\n",c->context->errstr); exit(1); } ln = listSearchKey(config.clients,c); assert(ln != NULL); listDelNode(config.clients,ln); zfree(c); /* The run was not done, create new client(s). */ if (!config.done) { createMissingClients(); } /* Stop the event loop when all clients were disconnected */ if (!listLength(config.clients)) { aeStop(config.el); } }
void tReadProc(struct aeEventLoop* eventLoop, int fd, void* clientData, int mask) { client_data_t *c = (client_data_t *)clientData; char buf[SHTTPSVR_IOBUF_LEN] = {0}; int nread = 0; for(;;) { nread = read(fd, c->readBuf + c->readBufPos, sizeof(c->readBuf) - c->readBufPos); if (nread == -1) { if (errno != EAGAIN) { ERRLOG("Read data from fd[%d] error.", fd); free_client(clientData); return; } else { break; } } else if (nread == 0) { DBGLOG("Client closed connection."); free_client(clientData); return; } else if (nread > 0) { c->readBufPos += nread; } } if (c->readBufPos > sizeof(c->readBuf) - 1) { ERRLOG("Read request from fd[%d] error.", fd); free_client(clientData); return; } c->readBuf[c->readBufPos] = '\0'; DBGLOG("Current read buffer: \n%s", c->readBuf); char *http_head_tail_pos = (strstr(c->readBuf, "\r\n\r\n")); if (NULL == http_head_tail_pos) { /* continue read when next file event */ return; } /* stop the server */ if (0 == strcmp("enhanced-redis-event-lib_stop\r\n\r\n", c->readBuf)) { DBGLOG("Received \"enhanced-redis-event-lib_stop\\r\\n\\r\\n\" request, will stop the server."); aeStop(eventLoop); return; } /* copy data for next request to the header of readBuf */ size_t dataLengthForNextRequest = (c->readBuf + c->readBufPos) - (http_head_tail_pos + 4); /* * The memmove() function copies n bytes from memory area src to memory area dest. * The memory areas may overlap: copying takes place as though the bytes in * src are first copied into a temporary array that does not overlap src or dest, * and the bytes are then copied from the temporary array to dest. */ memmove(c->readBuf, http_head_tail_pos + 4, dataLengthForNextRequest); c->readBufPos = dataLengthForNextRequest; if (aeCreateFileEvent(eventLoop, fd, AE_WRITABLE, tWriteProc, clientData) == AE_ERR) { ERRLOG("aeCreateFileEvent error."); return; } }
//停止事件循环 void StopServer() { aeStop(g_event_loop); }
int evt_stop(){ aeStop(loop); return 1; }
//异步信号事件 void onSignEvent( aeEventLoop *el, int fd, void *privdata, int mask) { if( fd == servG->sigPipefd[0] ) { int sig,ret,i; char signals[1024]; ret = recv( servG->sigPipefd[0], signals, sizeof( signals ), 0 ); if( ret == -1 ) { return; } else if( ret == 0 ) { return; } else { for( i = 0; i < ret; ++i ) { switch( signals[i] ) { //child process stoped case SIGCHLD: { printf( "Master recv child process stoped signal\n"); pid_t pid; int stat,pidx; //WNOHANG while ( ( pid = waitpid( -1, &stat, 0 ) ) > 0 ) { for( pidx = 0; pidx < servG->workerNum; pidx++ ) { if( servG->workers[pidx].pid == pid ) { //aeDeleteFileEvent( aEvBase.el,aEvBase.worker_process[i].pipefd[0] ,AE_READABLE ); close( servG->workers[pidx].pipefd[0] ); servG->workers[pidx].pid = -1; } } } servG->running = 0; //停止主循环 aeStop( servG->mainReactor->eventLoop ); break; } case SIGTERM: case SIGQUIT: case SIGINT: { int i; printf( "master kill all the clild now\n" ); for( i = 0; i < servG->workerNum; i++ ) { int pid = servG->workers[i].pid; //alarm/kill send signal to process, //in child process need install catch signal functions; kill( pid, SIGTERM ); } break; } default: { break; } } } } } }
static void command_handler (aeEventLoop * el, int fd, void *data, int mask) { int ret; workerState *ws = (workerState *) data; simpleBuf *sb = &ws->cin; char *cp; int is_quit = 0; int i; ret = sb_read (sb, fd); // master quits this fd instead assert (ret != -1); cp = sb->buf; while (!is_quit && sb->cp - cp >= sizeof (workerCmd *)) { workerCmd *cmd = NULL; memcpy ((char *) &cmd, cp, sizeof (workerCmd *)); switch (cmd->command) { case CMD_SIZE: ws->size = cmd->ival; cmd->ret = 0; break; case CMD_TPS: ws->tps = cmd->ival; cmd->ret = 0; break; case CMD_START: cmd->ret = command_start (ws, cmd->ebuf, sizeof (cmd->ebuf)); break; case CMD_STOP: free_connection (ws, 0); cmd->ret = 0; break; case CMD_STAT: cmd->num_rqst = ws->num_rqst; cmd->num_resp = ws->num_resp; cmd->num_reconn = ws->num_reconn; for (i = 0; i < MAX_HISTO; i++) { cmd->histo[i].count = ws->histo[i].count; cmd->histo[i].sum = ws->histo[i].sum; } cmd->ret = 0; break; case CMD_QUIT: free_connection (ws, 0); aeStop (ws->el); cmd->ret = 0; is_quit = 1; break; default: fprintf (stdout, "-ERR invalid command:%d\n", cmd->command); abort (); } write (ws->arg->wfd, "k", 1); cp += sizeof (workerCmd *); } memmove (sb->buf, cp, sb->cp - cp); sb->cp = sb->buf + (sb->cp - cp); }
void eventloopStop() { aeStop(M->eventloop); }
static void check_begin(aeEventLoop *el, int fd, void *privdata, int mask) { int ret; redis_node *srnode = privdata; thread_data *cdata = srnode->write_data; mbuf_base *mb = cdata->srgroup->mb; struct msg *msg; check_unit *cunit; RMT_NOTUSED(el); RMT_NOTUSED(fd); RMT_NOTUSED(privdata); RMT_NOTUSED(mask); ASSERT(el == cdata->loop); ASSERT(fd == srnode->sk_event); if (cdata->sent_keys_count - cdata->finished_keys_count > MAX_UNITS_HOLD_PER_THREAD) { return; } if (cdata->sent_keys_count >= cdata->keys_count) { aeDeleteFileEvent(cdata->loop,srnode->sk_event,AE_WRITABLE); return; } cunit = check_unit_create(); if (cunit == NULL) { log_error("Error: out of memory."); goto error; } cunit->srnode = srnode; cunit->cdata = cdata; cunit->state = CHECK_UNIT_STATE_GET_KEY; cdata->sent_keys_count ++; msg = msg_get(mb, 1, REDIS_DATA_TYPE_CMD); if (msg == NULL) { log_error("ERROR: msg get failed."); goto error; } ret = msg_append_full(msg, (uint8_t *)REDIS_RANDOMKEY, strlen(REDIS_RANDOMKEY)); if (ret != RMT_OK) { log_error("ERROR: msg append REDIS_RANDOMKEY failed."); goto error; } msg->ptr = cunit; msg->resp_check = check_response; /*send msg to source msg*/ ret = prepare_send_msg(srnode, msg, srnode); if (ret != RMT_OK) { goto error; } return; error: aeDeleteFileEvent(cdata->loop,srnode->sk_event,AE_READABLE|AE_WRITABLE); aeStop(cdata->loop); }