Tutorial : Go Concurrency - Controlling Channels (Part 3)

Tutorial  Go Concurrency - Controlling Channels's picture

Hope you are enjoying our blog on go routines and concurrency. In this blog, we will basically cover how you will control the channel. To make this complex problem easy I will make a control flow that you can use in your program. As we do instead of theoretic we will take it practically.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 package main import ( "fmt" "time" ) type Server struct { quitch chan struct{} msgch chan string } func (s *Server) sendmessage(msg string) { s.msgch <- msg } func newServer() *Server { return &Server{ quitch: make(chan struct{}), msgch: make(chan string, 128), } } func (s *Server) Start() { fmt.Println("Server Starting...") s.loop() } func (s *Server) loop() { for { select { case <-s.quitch: case msg := <-s.msgch: s.handleMessage(msg) default: } } } func (s *Server) handleMessage(msg string) { fmt.Println("Got message", msg) } func main() { server := newServer() go server.Start() for i := 0; i < 100; i++ { server.sendmessage(fmt.Sprintf("handle this number %d", i)) } time.Sleep(time.Second * 5) }

This is a blog, where I am sharing patterns that can be used in “golang” for go routines.

Let me example of each component now.

1. Creating server

1 2 3 4 type Server struct { quitch chan struct{} msgch chan string }

The server could be anything not necessarily HTTP which is serving data. Therefore it is a struct here where we have created two value msgch to send a message the message to the channel and quitch which is to inform that no more messages are there so we can close the connection.

2. Starting server

1 2 3 4 func (s *Server) Start() { fmt.Println("Server Starting...") s.loop() }

After creating one server we will start the server and once the server is started we will inform the server about the next plan. Here we are calling the loop function once it is starting. In your project, you can call the appropriate function as per your project requirement. As this is a template to control channels.

3. Calling loop function

In the loop function, we will create two loops one For and the other Select loop so for loop we will keep on looping over the select loop so that it keeps reading incoming messages from the server. And if it receives a close message then it just comes out of the loop function.

1 2 3 4 5 6 7 8 9 10 11 func (s *Server) loop() { Mainloop: for { select { case <-s.quitch: Break Mainloop case msg := <-s.msgch: s.handleMessage(msg) } } }

So when the case is a new message then this program will call the handle message function and pass the message as value to that integer.

If the case is quitch then it will break the Mainloop and come out from for and select loop.

4. Handle Message function

1 2 3 func (s *Server) handleMessage(msg string) { fmt.Println("Got message", msg) }

We are just printing the message received from the server. As per your program needs you can play as per your program requirement.

Ok so, this is all practical here, we are all set for a basic channel setup for go routines.

Let us understand the main body from where our program will run

1 2 3 4 5 6 7 8 9 10 func main() { server := newServer() go server.Start() for i := 0; i < 100; i++ { server.sendmessage(fmt.Sprintf("handle this number %d", i)) } time.Sleep(time.Second * 5) }

We are creating a new server say server and then using the go routine we are starting the server simultaneously. And sending the message to the server which could be received from the actual server in the program.

Check the source code in the linked repository.

https://github.com/GolangCompany/go-concurrency-part03/tree/develop

I hope you have understood the topic. I am keeping this blog a little bit short as in the next blog, I will be covering Mutexes, race conditions, and Automic values.

Keep reading, and grow the Go community. If you face any issues in development feel free to contact our Golang developers.

Build Your Golang Team