Asynchronous multithreaded C/s framework Gko_pool

Source: Internet
Author: User
Tags ticket

Once in the home company has done a network-to-peer transmission tool name Yue Gingko

More examples, such as: A client of a custom protocol, server, etc. please go to GitHub
The stability of this library is not to be questioned, has been used in my company's tens of thousands of servers in many network daemon

Welcome to join Reboot development of 2000 people QQ Group (365534424) Exchange Discussion


A long time ago, when too much food, the network part of the poor writing, all kinds of blocking network communication
Later left the memcached code to study a bit, feeling that the network model is very advanced
I took the network part of Memcached and Gingko did a combination, wrote a network framework called Gko_pool
The approximate characteristics 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 find memcached state machine There's a logical bug that's got a patch for memcached.
Later this was forgotten, and after two years the maintainer found it on the master, as the 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 it as 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 18, 2012 *       author: auxten */#include   "gko.h" #include  <sys/time.h>/// gingko  global stuffs_gingko_global_t gko;pthread_mutex_t lock=pthread_mutex_initializer;intcnt=3; Intcounter=0;voidreport_result (void*c,constchar*msg) {Gkolog (NOTICE, "%s", msg);p Thread_mutex_lock (&lock); ++counter==cnt) {printf ("finished\n"); exit (0);} Pthread_mutex_unlock (&lock);} Intmain (INTARGC,CHAR**ARGV) {Charcmd[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-&gt;setport ( -1); Gingko-&gt;setoption (&gko.opt);//     gingko-&gt;setprocesshandler (Conn_send_data); Gingko-&gt;setreporthandler (Report_ result); Gingko-&gt;gko_run (); charua[56][256]={"MOZILLA/5.0  (windows nt 6.1; wow64; rv:15.0)  gecko/20100101 firefox/15.0.1 "," Mozilla /5.0  (WINDOWS&NBSP;NT&NBSP;6.1;&NBSP;WOW64)  AppleWebKit/537.4  (Khtml, like gecko)  chrome/22.0.1229.79 safari/537.4 "," mozilla/5.0  (WINDOWS&NBSP;NT&NBSP;6.1;&NBSP;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; &NBSP;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)  AppleWebKit/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.782.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&NBSP;NT&NBSP;6.1;&NBSP;WOW64)  AppleWebKit/535.1  (Khtml, like gecko)  chrome/13.0.782.112 safari/535.1 "," mozilla/5.0  (windows nt 5.1)  AppleWebKit/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;&NBSP;SV1)  ; .net clr 3.5.30729) "," mozilla/4.0  (compatible; msie 7.0;  windows nt 5.1;&nbsp 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;&NBsp;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&NBSP;NT&NBSP;6.1;&NBSP;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  (macintosh; 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&NBSp 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  nt 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&NBSP;NT&NBSP;6.1;&NBSP;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 " ,};charsrv[3][32]={"202.108.43.189", "202.106.182.237", "Home"};inti=cnt;while (i--) {structtimeval tv; Gettimeofday (&tv,null); longt=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" "host: hi.video.sina.com.cn\r\n" " user-agent: %s\r\n "" accept: text/html,application/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?dpc=1\r\n\r\n ", t%2,ua[t%56],122+t% 20,121+ (i+t)%128,24+ (i+t)%135,24+ (i+t)%213) gingko-&gt;make_active_connect (Srv[i%3],80,strlen (cmd), cmd,-1, -1,0,10);//        gingko-&gt;make_active_connect ("61.30.127.2",  80, strlen (cmd),  cmd, -1, -1, 0, 10);} Sleep (10);//due to the asynchronous request, there is no processing response,sleep seconds before exiting Return0;}

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 client of a custom protocol, server, etc. please go to GitHub
The stability of this library is not to be questioned, has been used in my company's tens of thousands of servers in many network daemon

Welcome to join reboot to develop thousands of QQ group (365534424)

This article is from the "Reboot DevOps Development" blog, please be sure to keep this source http://opsdev.blog.51cto.com/2180875/1669505

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.