Go with multiple workers - Goroutines

What I wanted is give a plan with golang as a job workers . So build very small project initially as a prototype.

Requirements :

  1. Language should be golang

Let’s plan how we achieve this.

  1. We need to add multiple subscribers for rabbitmq queue.

To run concurrently in go we need to use Goroutines (link : https://tour.golang.org/concurrency/1)

Assume following things

  • you have basic idea about golang.

I will provide another post basic rabbitmq with golang later (how to connect and how to create queue.)

will write a function for generate multiple goroutines . will call that function as “multipleWorkers”

func multipleWorkers() {

for i := 1; i <= 10; i++ {
go worker(i)
}
}

inside that function will create 10 goroutines for run 10 jobs.

Now inside worker function we need to read rabbitmq queue and get one job and ack once job is done.

“failOnError” function is very straight forward that will handle errors for now just show the error.

keep in mind auto-ack is false so we need to send ack status.

var job *Job is job struct

json.Unmarshal(d.Body, &job) will help to json to struct

status := job.Process() run the job and give the bool status

d.Ack(!status) according to the above status, will ack for rabbitmq.

following lines to help to run these forever

forever := make(chan bool)
//and
<-forever

THATS IT…..!

Next post will talk sync.WaitGroup, limited concurrent for many http requests.

--

--

VP of Engineering

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store