Golang Implementing asynchronous Concurrency Sokect

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Search Golang + epoll example, get the following code, feel Golang programming thinking really do parallel programming:

Package Mainimport ("FMT" "NET" "OS" "Time") const (Max_conn_num = 5)//echo server Goroutinefunc echofunc (CONN net. Conn) {defer Conn. Close () Buf: = Make ([]byte, 1024x768) for {_, ERR: = conn. Read (BUF) if err! = Nil {//println ("Error reading:", err. Error ()) return}//send reply_, err = conn. Write (BUF) if err! = Nil {//println ("Error Send reply:", err. Error ()) Return}}}//initial Listener and Runfunc main () {listener, err: = Net. Listen ("TCP", "0.0.0.0:8088") if err! = Nil {fmt. PRINTLN ("Error listening:", err.) Error ()) OS. Exit (1)}defer listener. Close () fmt. Printf ("Running ... \ n") var cur_conn_num int = 0conn_chan: = Make (chan net.  Conn) Ch_conn_change: = make (chan int) go func () {for conn_change: = Range Ch_conn_change {cur_conn_num + = Conn_change}} () go Func () {For _ = range time. Tick (1e8) {fmt. Printf ("cur conn num:%f\n", Cur_conn_num)}} () for I: = 0; i < Max_conn_num; i++ {go func () {for conn: = range Conn_chan {ch_conn_change <-1EchoFunc (conn) Ch_conn_change <--1}} ()}for {conn, er r: = Listener. Accept() if err! = Nil {println ("Error Accept:", err.) Error ()) Return}conn_chan <-Conn}}


Look at this code again using the traditional way of thinking:

A echo server with max-connections limit and interval connection show//package mainimport ("FMT" "NET" "OS" "Time") const (Max_conn_num = 5)//echo server Goroutinefunc echofunc (CONN net. Conn, Conn_close_flag Chan int) {defer Conn. Close () defer func () {Conn_close_flag <--1} () BUF: = Make ([]byte, 1024x768) for {_, Err: = Co Nn. Read (BUF) if err! = Nil {//println ("Error reading:", err. Error ()) return}//send reply _, err = conn. Write (BUF) if err! = Nil {//println ("Error Send reply:", err. Error ()) return}}}//initial Listener and Runfunc main () {listener, err: = Net. Listen ("TCP", "0.0.0.0:8088") if err! = Nil {println ("Error listening:", err. Error ()) OS. Exit (1)} Defer listener. Close () fmt. Printf ("Running ... \ n") var cur_conn_num float64 = 0 Ch_conn_change: = make (chan int, max_conn_num) Tick: = time. Tick (1e8) for {//read All close flags berfor Accept new connection//todo:better code to handle batch close?                Readmore: = 1 for readmore > 0 {select {case conn_change: = <-ch_conn_change:            Cur_conn_num = Cur_conn_num + float64 (conn_change) default:readmore = 0 }}//fixme:tick block by listener. Accept () Select {Case <-tick:fmt.            Printf ("cur conn num:%f\n", Cur_conn_num) Default:} If Cur_conn_num >= max_conn_num { Reach Max_conn_num, waiting for exist connection close time. Sleep (time. Second)} else {//accept new connetion conn, err: = Listener. Accept () if err! = Nil {println ("Error Accept:", err.)        Error ()) return} cur_conn_num++ go Echofunc (conn, Ch_conn_change) }    }}

In this case, golang the traditional sequential execution pattern through multiple Goroutine + channel blockages.

Code from the Google Group discussion: Https://groups.google.com/forum/#!topic/golang-china/q4pFH-AGnfs

Related Article

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.