Experience of high concurrency learning in Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Signal

SIGRECV1:=make (Chan os. Signal,1)SIGS1:=[]os. Signal{Syscall. SIGINT, Syscall. Sigquit}signal. Notify(SIGRECV1,SIGS1 ...)SigRecv2:=make (Chan os. Signal,1)SIGS2:=[]os. Signal{Syscall. SIGINT, Syscall. Sigquit}signal. Notify(Sigrecv2,sigs2 ...) We then use two for loops to receive the message. Consider that the main program exits, so wait for completion with Waitgroup and exit. VAR wg sync. WaitgroupWg. ADD(2) go func () {for sig:=range sigrecv1{FMT. Printf("Received a signal from sigrecv1%v", SIG)} FMT. Printf("End. [sigrecv1]\n])} () go func () {for sig:=range sigrecv2{FMT. Printf("Received a signal from sigrecv1%v", SIG)} FMT. Printf("End. [sigrecv2]\n])} () WG. Wait()

int socket(int domain,int type,int protocol)

socket type 类型

TCP UDP

tcp 连结类型


may be passed back to IO. EOF, indicating the end of the connection

var dataBuffer bytes.Bufferb:=make([byte,10])for {    n,err:=conn.Read(b)    if err!=nil{        if err==io.EOF{            fmt.Println("The connection is closed.")            conn.Close()        } else{            fmt.Printf("Read Error:%s\n",err)        }        break    }    dataBuffer.Write(b[:n])}

Bufio is the Buffered I/o abbreviation.

Because of net. The conn type implements the read interface in the interface type Io.reader, so an implementation type of the interface type. Therefore, we can use Bufio. The Newreader function wraps the variable conn, like this:
Reader:=bufio. Newreader (conn)
You can invoke the Readbytes ("\ n") of the reader variable to accept a parameter of type byte.
Of course, in many cases it is not easy to find a direct character. For example, the HTTP protocol specifies that the end of the message header information is two blank lines, which is the string "\r\n\r\n",
Writer:=bufio. Newwriter (conn) to write data
# # # Close ()
Close connection

# # # Localaddr & Remoteaddr method
Conn. Rmeoteaddr (). Network (). String ()

# # Setdeadline, Setreaddeadline,setwrtedeadline

b:=make([]byte,10)
conn.SetDeadline(time.Now().Add(2*time.Second))
for {
n,err:=conn.Read(b)
}

We do this by calling time. Now () method the moment after the current absolute time is added to 2s.2s. Assume that the second iteration time after is more than 2s. The third iteration has already timed out, so instead.
"'
B:=make ([]byte,10)
for {
Conn. Setdeadline (time. Now (). ADD (2*time. Second))

n,err:=conn.Read(b)

}
"'

# # # A simple implementation of a server

Buffer in the cache mechanism, in many cases, it will read a bit more than enough data into the buffer. Will produce an early-read problem.

Net/http built a very useful interface based on NET/TCP, in addition to the standard library, the NET/RCP API gives us another way to establish communication and exchange data between the two go programs.
Remote Procedure calls (procedure Call) are also based on the TCP/IP protocol stack.
In UNIX systems, threads defined in the POSIX standard and their properties and operating methods are widely recognized.
Linux is called the NPTL.

# # Multithreading

Thread an ID, TID.
Recommendations for writing high concurrency programs:
1. Control the purity of the critical zone. The critical section contains only the code that operates the shared data.
1. Control the particle size of the critical section.
1. Reduce the execution time of code in the critical area.
1. Avoid holding mutex variables for long periods of time.
1. Use yard operations rather than mutexes.

The go language is a unique two-level threading model built into the kernel threads provided by the operating system.
Goroutine the right meaning of the representative:
Do not use shared memory to communicate. As an alternative, you should use communication as a means to share memory.
The way that data is placed in a shared memory area for programs in multiple threads is simple in its basic sense, but makes concurrent access control extremely complex. Only by doing a lot of constraints and restrictions, it is possible for these simple methods to be implemented correctly. But at the same time, the correctness of the need for scalability.
The go language does not recommend passing data in a shared memory area. As an alternative, use the channel as a priority. As data is passed between multiple goroutine, and the process is also guaranteed to be synchronized.
Go language thread 3 must know the core elements.
M:machine. A single m represents a kernel thread.
P:processor. A P represents the context content required by M.
G:goroutine. A G represents the encapsulation of a Go language code that needs to be executed concurrently.

Go concurrent programming.

Type Intchan chan Int.
The type of the channel with the element type int.
This is a bidirectional type of channel.

If a message is sent to a nil (the closed channel), the process is permanently blocked.
It is important to note that when we send a message to a channel, we get a copy of the value, and when the copy is formed, then the changes to the original value do not affect the value in the channel.

Select Example:

The SELECT statement differs from the switch statement in that only the Send statement or accept statement that follows each case is for a channel.
A channel has been initialized for each case. The type of the channel is not constrained by any constraints. The type and capacity of the element are arbitrary.

Branch selection Rules

Re-do right, top to bottom.
However, when the system discovers the first case that satisfies the selection criteria, the runtime executes the statements contained in the case. This also means that other case will be ignored. If more than one case satisfies the condition at the same time, at run time, the system determines that case will be executed by a pseudo-random algorithm. For example, the following code sends 5 integers in a range of "1,3":

 PackageMainImport("FMT")funcMain () {chancap: =5Ch7: = Make(Chan int, Chancap) forI: =0; i < Chancap; i++ {Select{ CaseCh7 <-1: CaseCh7 <-2: CaseCh7 <-3:        }    } forI: =0; i < Chancap; i++ {fmt. Printf ("%v\n", <-ch7)}}

However, if all of the case is not satisfied (and there is no default), it will block. Until a case is triggered.
So normally, the default case is necessary. And default can contain only one default case.
Two variable assignment, E,ok:=<-chan
The second variable refers to whether a channel is closed.

 PackageMainImport("FMT")funcMain () {Go func(){ for{Select{ CaseE,ok:=<-ch11:if!ok{FMT. Println ("End.") Break}Else{FMT. Printf ("%d\n", e) ß}}}}//Modify the scheme as follows, otherwise it will deadlock. funcMain () {Go func(){varEintok:=true   //declaration outside, convenient for outer use         for{Select{ CaseE,ok=<-ch11:if!ok{FMT. Println ("End.") Break}Else{FMT. Printf ("%d\n", e)}}if!ok{ Break}        }    }}

Sometimes we need to close the process early. Here we add a new timeout behavior.

timeout:=make(chanbool,1)gofunc(){    time.Sleep(time.Millisecond)    timeout<-false}

On the original basis, add

go func(){    var e int    ok := true    for {       select{       case e,ok=<-ch11:        ...        case ok=<-timeout:        fmt.Println("Timeout.")        break       }}    if !ok{     break    }}

Non-buffered channel

Buffer channel: Because the element's delivery is asynchronous, the send operation ends immediately after the element value is successfully sent to the channel.
Non-buffered channel: waits for the receive operation to accept the value of the element. And only if you make sure that you receive it successfully will you actually complete the execution.

package mainimport (    "fmt"    "time")func main() {    make(chanint)    gofunc() {        fmt.Printf("Sleep a second ...\n")        time.Sleep(time.Second)        num := <-unbufChan        fmt.Printf("Received a integer %d.\n", num)    }()    num := 1    fmt.Printf("Send integer %d ...\n", num)    unbufChan <- num    fmt.Printf("Done")}

Select statements and non-buffered channels

When sending elements to a non-buffered channel using the SELECT statement, we need to play chicken blood. Because, compared to the SELECT statement that operates the buffer channel, the probability of it being blocked is very large. The underlying reason remains that the unbuffered channel will pass the element value synchronously.

Time Packet and Channel

Timer

First, the struct type, time. Timer. The two methods of new are:

time.NewTimer(Duration) & time.AfterFunc.Duration=3*time.Hour+36*.time.MinuteTimer{    Reset()    Stop()}//到达时间,是通过字段C(chan time.Time)缓冲通道。 时间一道向自己发送一个time.Time类型元素。绝对到期时间。

Structure prior to refactoring:

case <-time.NewTimer(time.Millisecond).C:    fmt.Println("Timeout")    ok=false    break

Timers can be reused. Therefore, it is wasteful to initialize in the case of receiving rain gear. Here's how to use it better:

Go func () {varTimer* Time.Timer    for{Select{ Case<-func () <-chan Time. Time{if Timer==nil{Timer= Time. Newtimer ( Time. Millisecond)}Else{Timer. Reset ( Time. Millisedcond)} returnTimer. C} (): FMT. Println ("Timeout") ok=falseBreak}}}

Staccato device

It is not appropriate to use the timer as a timeout trigger, unlike the timers. As it is communicated to the expiry event may be waived when it is never delayed. Once the interrupter is initialized, all of its expiry times are fixed.
A fixed expiry time precisely makes the interrupter ideal for triggers that are scheduled tasks.
For example, the minimum interval between two executions is 10 minutes. We can write code that satisfies this requirement:

High Concurrency Load Balancer

QPS (query per Second, per second) & TPS (transactions per Second, amount per second).

Slices and arrays are not concurrency-safe. So use a Chan to store the results.

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.