服務(wù)器架構(gòu)設(shè)計(jì)1
服務(wù)器周期性
說到服務(wù)器,通??梢韵胂蠖际且粋€(gè)while無限循環(huán),當(dāng)某種條件觸發(fā)之后,則跳出循環(huán)。
然而,有些時(shí)候,則需要服務(wù)器針對(duì)某些ses,周期性的執(zhí)行特定操作,如何實(shí)現(xiàn)呢。
一個(gè)簡(jiǎn)單的方法就是,建立session的nodelist,節(jié)點(diǎn)信息包括,主線程周期性的訪問這個(gè)nodelist,如果時(shí)間條件滿足,執(zhí)行回調(diào)函數(shù)。
構(gòu)造了一個(gè)簡(jiǎn)單nodelist,便于說明。
inlineuint64_tget_current_clock{structtimespectp;clock_gettime;returntp.tv_sec*1e3+;}staticvoidheap_insert_helper{if{return;}uint32_tp=>>1;if{return;}else{timer_event_t*tmp=heapp;heapp=heapidx;heapidx=tmp;heap_insert_helper;}}staticvoidevent_heap_insert{timer_event_t**heap=ctx->heap;uint32_tidx=ctx->nevts++;heapidx=evt;heap_insert_helper;}staticvoidheap_del_helper{if{return;}uint32_tlchild=idx*2+1;uint32_trchild=idx*2+2;uint32_ti=idx;if){i=lchild;}if){i=rchild;}if{timer_event_t*tmp=heapi;heapi=heapidx;heapidx=tmp;heap_del_helper;}}statictimer_event_t*event_heap_pop{timer_event_t*evt=ctx->heap0;ctx->heap0=ctx->heap--ctx->nevts;heap_del_helper;returnevt;}staticvoidevent_re_enqueue{if{evt->time+=evt->interval;if{evt->count--;if{evt->interval=0;}}event_heap_insert;}else{mpool_put;}}voidtimer_run{while){timer_event_t*evt=event_heap_pop;if{evt->cb;}event_re_enqueue;}}timer_ctx_t*timer_init{if{returnNULL;}memset);//makesurempoolislargeenoughctx->mpool=mpool_calloc);//ctx->mpool=mpool_init,//+sizeof)*MAX_EVENT_COUNT+512,//NULL);if{gotoerror;}returnctx;error:mpool_cleanup;returnNULL;}voidtimer_cleanup{if{return;}mpool_cleanup;ctx->mpool=NULL;}inttimer_register{timer_event_t*evt=NULL;if{gotoerr;}evt=mpool_get;if{gotoerr;}memset);intidx=mpool_get_idx;if{gotoerr;}evt->id=idx;evt->time=get_current_clock+delay;evt->cb=cb;evt->data=data;evt->interval=interval;evt->count=count;evt->enable=1;event_heap_insert;returnidx;err:if{mpool_put;}return-1;}voidtimer_cancel{timer_event_t*evt=mpool_get_by_idx;if{evt->enable=0;}}
Tags:服務(wù)器周期性,服務(wù)器架構(gòu)設(shè)計(jì)1
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。