GoLang's network concurrent programming

Introduction

Concurrency: In the operating system , it means that several programs in a period of time are between started and run to completion, and these programs are all running on the same processor , but there are only A program runs on the processor.

Concurrency problems are prone to occur when network program performance is poor and network traffic is high. Concurrency involves the execution of processes and threads, as well as the scheduling of processes and threads by the CPU. If concurrency is not controlled properly, it may cause program execution errors (such as oversold, payment data errors and other serious problems), and also cause websites Problems such as system breakdown. Therefore, concurrency issues require program optimization and system tuning. The Go language itself has excellent performance, and provides features such as coroutines and message queues, which can effectively deal with concurrency problems.

reference

For the basics of Go language, please refer to: "Go Language Introduction and Technical Guide" https://blog.csdn.net/yan_dk/article/details/110557155
"GoLang Basic Network Programming" https://blog.csdn.net/yan_dk/article/details/117621468

Example

Example (coroutine)

Requirements: There is a logic to call redis, mysql, and file processing at the same time in the business program. Suppose redis takes 1 second, mysql takes 2 seconds, file processing takes 3 seconds, and traditional network request processing takes 6 seconds. How to optimize?

Solution: Use GoLang's coroutine processing.

Program 01/main.go

package main import (	"fmt"	"time")func redis()  {	fmt.Println("start redis")	time.Sleep(time.Second*1)	fmt.Println("redis execting...")	fmt.Println("end redis")}func mysql()  {	fmt.Println("start mysql")	time.Sleep(time.Second*2)	fmt.Println("mysql execting...")	fmt.Println("end mysql")} func file()  {	fmt.Println("start file")	time.Sleep(time.Second*3)	fmt.Println("file execting...")	fmt.Println("end file")}func  main()  {	fmt.Println(time.Now())	//time.Sleep(time.Second*6)	go redis()	go mysql()	go file()	time.Sleep(time.Second*3)//主进程设置延时,不然执行过快,看不到线程的输出内容	fmt.Println(time.Now()) }

operation result

It took 3 seconds to double the performance.

Program 02/main.go (using thread waiting group method)

package main import (	"fmt"	"sync"	"time")//使用线程等待组var wg sync.WaitGroupfunc redis()  {	defer wg.Done() // 结束登记	fmt.Println("start redis")	time.Sleep(time.Second*1)	fmt.Println("redis execting...")	fmt.Println("end redis")}func mysql()  {	defer wg.Done() // 结束登记	fmt.Println("start mysql")	time.Sleep(time.Second*2)	fmt.Println("mysql execting...")	fmt.Println("end mysql")} func file()  {	defer wg.Done() // 结束登记	fmt.Println("start file")	time.Sleep(time.Second*3)	fmt.Println("file execting...")	fmt.Println("end file")}func  main()  {	fmt.Println(time.Now())	//time.Sleep(time.Second*6)	wg.Add(1) // 登记协程	go redis()	wg.Add(1) // 登记协程	go mysql()	wg.Add(1) // 登记协程	go file()	//time.Sleep(time.Second*3)	wg.Wait() // 等待登记的协程执行完再执行后续的程序	fmt.Println(time.Now()) }

The result of the operation is the same as above.