Asynchronous multithreaded C/s framework Gko_pool

Source: Internet
Author: User
Tags memcached ticket

Once in the home company did an intranet-to-peer transmission tool name Yue Gingko at that time too food, the network part write is poor, all kinds of blocking network communication later left to memcached code research, feel inside the network model is very advanced I took Memcached's network part out and Gingko did a combination, wrote a network framework called Gko_pool General features are:

    • Fully asynchronous (including DNS resolution)
    • Can listen a port when server, can also initiate connection write client
    • Worker is peer multi-threaded
    • Built-in a simple "memory pool"

See Code found memcached state machine has a logical bug to give memcached a patch later this matter was forgotten, after two years by the maintainer found to master, Defenders said:

Wow, years old ... and it looks correct to me. If that update_event fails the connection might zombie. It's very hard for that to fail and it's been that forever. Pushed a commit for the next release.

It's no exaggeration to say that memecached is a monument to multi-threaded asynchronous network programming (Nginx is a monument to multi-process asynchronous network programming) I took Memcached's network part out and wrote a network framework called Gko_pool https:// Github.com/auxten/gko_pool

For a chestnut: HTTP brush ticket, in my notebook machine can also reach 300KQPS, control the speed, accidentally sina the server to hang up

/* * * * * * * * * Created on:may, * author:auxten */#include "gko.h" #include <sys/time.h>///gingko Global Stuf fs_gingko_global_t gko;pthread_mutex_t lock = pthread_mutex_initializer;int CNT = 3;int counter = 0;void Report_result (vo    ID * C, const char * msg) {Gkolog (NOTICE, "%s", msg);    Pthread_mutex_lock (&lock);        if (++counter = = cnt) {printf ("finished\n");    Exit (0); } pthread_mutex_unlock (&lock);}    int main (int argc, char** argv) {char cmd[2048];    Gko.opt.to_debug = 0;    Gko.ready_to_serv = 1;    Gko.sig_flag = 0;    Gko.opt.worker_thread = 8;    Gko.opt.connlimit = serv_pool_size;//Gko.opt.to_debug = 1;    Gko_pool * Gingko = Gko_pool::getinstance ();    Gingko->setport (-1);    Gingko->setoption (&gko.opt);//Gingko->setprocesshandler (Conn_send_data);    Gingko->setreporthandler (Report_result);    Gingko->gko_run (); Char ua[56][256] = {"mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) gecko/20100101 Firefox/15.0.1 "," mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.79 safari/537.4 "," mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.94 safari/537.4 "," mozilla/5.0 (Macintosh;  Intel Mac OS X 10_8_2) applewebkit/536.26.14 (khtml, like Gecko) version/6.0.1 safari/536.26.14 "," mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.1 (khtml, like Gecko) chrome/21.0.1180.89 safari/537.1 "," mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; trident/5.0) "," mozilla/5.0 (Windows NT 5.1; rv:15.0) gecko/20100101 firefox/15.0.1 "," mozilla/5.0 (Windows NT 6.1; rv:15.0) gecko/20100101 firefox/15.0.1 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.79 safari/537.4 "," mozilla/5.0 (Linux; U Android 2.2; FR-FR;  desire_a8181 build/frf91) app3lewebkit/53.1 (khtml, like Gecko) version/4.0 Mobile safari/533.1 "," mozilla/5.0 (Windows NT 6.1) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.79 safari/537.4 "," mozilla/5.0 (Windows NT 6.1) APPLEWEBK it/537.4 (khtml, like Gecko) chrome/22.0.1229.94 safari/537.4 "," mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) gecko/20100101 firefox/5.0 "," mozilla/5.0 (Windows NT 6.0) applewebkit/535.1 (khtml, like Gecko) chrome/13.0.78 2.112 safari/535.1 "," mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) gecko/20100101 firefox/16.0 "," mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 7.02 bork-edition [en] "," mozilla/5.0 (Windows NT 6.1; rv:5.0) gecko/20100101 firefox/5.02 "," mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/535.1 (khtml, like Gecko) chrome/13.0.782.112 safari/535.1 "," mozilla/5.0 (Windows NT 5.1) APPLEWEBK it/537.4 (khtml, like Gecko) chrome/22.0.1229.79 safari/537.4 "," mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; trident/4.0; mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1);. NET CLR 3.5.30729) "," mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; trident/4.0;. NET CLR 1.1.4322) "," mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; funwebproducts;. NET CLR 1.1.4322; Peoplepal 6.2) "," mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MRA 5.8 (build 4157);. NET CLR 2.0.50727; asktbptv/5.11.3.15590) "," mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1;. NET CLR 2.0.50727) "," mozilla/5.0 (Windows NT 6.1; Rv:2.0b7pre) gecko/20100921 Firefox/4.0b7pre "," mozilla/5.0 (Windows NT 5.1; rv:5.0.1) gecko/20100101 firefox/5.0.1 "," mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; trident/4.0;. NET CLR 2.0.50727;. NET CLR 3.0.4506.2152;. NET CLR 3.5.30729) "," mozilla/5.0 (Macintosh;  Intel Mac OS X 10_8_2) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.94 safari/537.4 "," opera/9.80 (Windows NT 5.1; U EN) presto/2.10.289 version/12.01 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) gecko/20100101 firefox/15.0.1 "," mozilla/5.0 (IPhone; CPU IPhone OS 6_0 like Mac os X applewebkit/536.26 (khtml, like Gecko) version/6.0 mobile/10a403 safari/8536.25 "," mozilla/5.0 ( Windows NT 6.1) applewebkit/537.1 (khtml, like Gecko) chrome/21.0.1180.89 safari/537.1 "," mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1;    trident/5.0) "," mozilla/5.0 (Windows NT 5.1) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.94 safari/537.4 ", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) gecko/20100101 firefox/15.0.1 "," mozilla/5.0 (Windows NT 5.1) applewebkit/537.1 (khtml, like Gecko) Chrome/21. 0.1180.89 safari/537.1 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) applewebkit/534.57.2 (khtml, like Gecko) version/5.1.7 safari/534.57.2 "," mozilla/5.0 (Windows N T 6.1; WOW64) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.92 safari/537.4 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) applewebkit/536.26.14 (khtml, like Gecko) version/6.0.1 safari/536.26.14 "," mozilla/5.0 (Macinto Sh Intel Mac OS X 10_7_5) Applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.79 safari/537.4 "," mozilla/5.0 (Windows NT 5.1; rv:13.0) gecko/20100101 firefox/13.0.1 "," mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) gecko/20100101 firefox/15.0.1 "," mozilla/5.0 (Windows; U Windows NT 5.1; En-us; rv:1.9.2) gecko/20100115 firefox/3.6 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) applewebkit/537.1 (khtml, like Gecko) chrome/21.0.1180.89 safari/537.1 "," mozilla/5.0 (Macintosh ; Intel Mac OS X 10.6; rv:15.0) gecko/20100101 firefox/15.0.1 "," mozilla/5.0 (IPad; CPU os 6_0 like Mac os X applewebkit/536.26 (khtml, like Gecko) version/6.0 mobile/10a403 safari/8536.25 "," MOZILLA/5. 0 (Macintosh; Intel Mac OS X 10_7_4) applewebkit/536.25 (khtml, like Gecko) version/6.0 safari/536.25 "," mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.79 safari/537.4 "," mozilla/5.0 (Windows N T 5.1; rv:16.0) gecko/20100101 firefox/16.0 "," mozilla/5.0 (MaciNtosh; Intel Mac OS X 10_6_8) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.79 safari/537.4 "," mozilla/5.0 (IPad; CPU os 5_1_1 like Mac os X applewebkit/534.46 (khtml, like Gecko) version/5.1 mobile/9b206 safari/7534.48.3 "," Mozilla /5.0 (Macintosh; Intel Mac OS X 10_7_5) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.94 safari/537.4 "," mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) gecko/20100101 firefox/15.0.1 "," mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/534+ (khtml, like Gecko) bingpreview/1.0b "," mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) gecko/20100101 firefox/14.0.1 "," mozilla/5.0 (Windows NT 6.1; WOW64;    rv:14.0) gecko/20100101 firefox/14.0.1 ",};    Char srv[3][32] = {"202.108.43.189", "202.106.182.237", "Home"};    int i = CNT;        while (i--) {struct Timeval TV;        Gettimeofday (&AMP;TV, NULL);        Long T = tv.tv_usec; sprintf (cmd, "Get/poll.php?project_id=5168& id=49 http/1.%ld\r\n "//" get/poll.php?project_id=5168&id=80 http/1.%ld\r\n "" H Ost:hi.video.sina.com.cn\r\n "" User-agent:%s\r\n "" Accept:text/html,appli cation/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n "" accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,                        en;q=0.3\r\n "" dnt:1\r\n "" Client-ip:%ld.%ld.%ld.%ld\r\n " "Connection:keep-alive\r\n" "REFERER:HTTP://HI.VIDEO.SINA.COM.CN/YOU/2012BANJITI/PH.PHP?DP c=1\r\n\r\n ", t% 2,ua[t%, 122 + t, 121 + (i + t)%, + (i + t)% 135, + (i + t)% 213)        ; Gingko->make_active_connect (srv[i%3], N, strlen (cmd), cmd,-1,-1, 0, ten);//Gingko->make_active_connect ("61    .30.127.2 ", strlen, (cmd), cmd,-1,-1, 0, 10); } sleep (10); Since the request was made asynchronously, there was no processing response,sleep seconds before exiting return 0;}

In order to brush the truth of the ticket, but also write so many useragent, brush the conscience of the world, ha ha haha more examples, such as: A custom protocol client, server, etc. please go to the stability of GitHub this set of libraries do not question, has been used in many of my company's tens of thousands of servers on the network daemon

Asynchronous multithreaded C/s framework Gko_pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.