Go lang concurrency with channels and worker pools using sync.Pools.

Go Concurrency

Go lang concurrency with channels and worker pools using sync.Pools.

Concurrency

Concurrency is an ability of a program to do multiple things at the same time. This means a program that have two or more tasks that run individually of each other, at about the same time, but remain part of the same program.

In GO concurrency is the ability for functions to run independent of each other. A goroutine is a function that is capable of running concurrently with other functions.

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("Stating program execution...")

    # Go routine.
	go func() {
		time.Sleep(time.Duration(1) * time.Second)
		fmt.Println("Hello from goroutine.")
	}()
	fmt.Println("Stopping program execution...")

    # This for statement is used to pause the program.
    # Since main func is also a go routine it will exit immediately without waiting for the go routine to complete.
	for {

	}
}

go run main.go

Output:

Stating program execution...
Stopping program execution...
Hello from goroutine.

Channels

Channels are the medium for goroutines to communicate with one another. In channels data can be sent from one end and received from the other end using channels.

package main

import (
	"fmt"
)

func calculateSquares(number int, s chan int) {
	sum := 0

	for number != 0 {
		digit := number % 10
		sum += digit * digit
		number = number / 10
	}

	s <- sum

    # Channels should be always closed and the closing should be always done by sending end instead of receiving end.
	defer close(s)
}

func main() {
	s := make(chan int)
	fmt.Println("Starting program execution...")
	go calculateSquares(55, s)

    # Channels are blocking so unless the data is received from channel `s`, the execution of the program won't move to next line.
	sum := <-s

	fmt.Println("Final output", sum)
	fmt.Println("Ending program execution...")
}

Output:

Starting program execution...
Final output 50
Ending program execution...

Here, chanel s is used to communicate between calculateSquares and main go routine.

Wait groups.

To wait for multiple goroutines to finish, we can use a wait group. Wait groups comes under sync package.

package main

import (
	"fmt"
	"sync"
)

var wg sync.WaitGroup

func main() {
	// It notifies the wait group that there is 1 go routine running.
	wg.Add(1)
	fmt.Println("Starting program execution...")
	go calculateSquares(55)

	fmt.Println("Ending program execution...")

	// It will halt the program exit until all the go routines are executed and, until all the go routines
	// signal done.
	wg.Wait()
}

func calculateSquares(number int) {
	sum := 0

	for number != 0 {
		digit := number % 10
		sum += digit * digit
		number = number / 10
	}

	fmt.Println(sum)
	// It notifies the wait group that the execution of go routine is completed.
	defer wg.Done()

}

Sync pool

Frequent allocation and recycling of memory will cause a heavy burden to Garbage collector. sync.Pool caches objects that are not used temporarily and use them directly (without reallocation) when they are needed next time.

Note: example is in worker_pools.go

Owner
Kushal Khadka
Software Engineer
Kushal Khadka
Similar Resources

Go-concurrency-patterns - Sample concurrency patterns with Goroutines

About This sample project provides some concurrency pattern examples in Go using

Feb 21, 2022

A sync.WaitGroup with error handling and concurrency control

go-waitgroup How to use An package that allows you to use the constructs of a sync.WaitGroup to create a pool of goroutines and control the concurrenc

Dec 31, 2022

grafana-sync Keep your grafana dashboards in sync.

grafana-sync Keep your grafana dashboards in sync. Table of Contents grafana-sync Table of Contents Installing Getting Started Pull Save all dashboard

Dec 14, 2022

based on go lang build WEB development framework for go lang beginners .

based on go lang build WEB development framework for go lang beginners .

Oct 31, 2021

The pool price management bot to stabilize overpriced pools

The pool price management bot to stabilize overpriced pools

The pool price management bot to stabilize overpriced pools during the Gravity DEX incentivized testnet. This project is developed solely for the purpose of pool price management during the incentivized testnet.

Jul 26, 2022

This service is intented to collect data using grpc using Go lang backend and cassandra DB as storage

Go Data Collection This service is intented to collect data using grpc using Go lang backend and cassandra DB as storage. Dev Setup make test_env make

Apr 13, 2022

Simple in-memory job queue for Golang using worker-based dispatching

artifex Simple in-memory job queue for Golang using worker-based dispatching Documentation here: https://godoc.org/github.com/mborders/artifex Cron jo

Dec 24, 2022

Worker failover support for PostgreSQL Citus extension using pg_auto_failover.

citus-failover Worker failover support for citus community version using pg_auto_failover. What is this? This is a simple service to monitor changes i

Dec 7, 2022

Gogrok is a self hosted, easy to use alternative to ngrok. It uses SSH as a base protocol, using channels and existing functionality to tunnel requests to an endpoint.

gogrok A simple, easy to use ngrok alternative (self hosted!) The server and client can also be easily embedded into your applications, see the 'serve

Dec 3, 2022

A demo of using Go channels as a function composition technique.

Channels Example This is a Go program containing functions that consume and produce channels. I think this is potentially a nicer API shape than e.g.

Jan 16, 2022

Go-snake - Go CLI implementation of Snake game, using channels

Go-snake - Go CLI implementation of Snake game, using channels

go-snake 🐍 Go CLI implementation of Snake game, using channels. NB: this code w

Oct 19, 2022

Gowl is a process management and process monitoring tool at once. An infinite worker pool gives you the ability to control the pool and processes and monitor their status.

Gowl is a process management and process monitoring tool at once. An infinite worker pool gives you the ability to control the pool and processes and monitor their status.

Gowl is a process management and process monitoring tool at once. An infinite worker pool gives you the ability to control the pool and processes and monitor their status.

Nov 10, 2022

File Processor in Concurrency Pattern using Golang goroutine.

 File Processor in Concurrency Pattern using Golang goroutine.

File Processor in Concurrency Pattern Implement a file processor solution in concurrency pattern using Golang goroutine. Get Started Run docker-compos

Sep 16, 2022

Automatic sync from IMDb to Trakt (watchlist, lists, ratings and history) using GitHub actions

imdb-trakt-sync GoLang app that can sync IMDb and Trakt user data - watchlist, ratings and lists. For its data needs, the app is communicating with th

Jan 2, 2023

Instagram Backend HTTP REST API using GO Lang and Mongo DB

Instagram Backend HTTP REST API using GO Lang and Mongo DB Project for Appointy Summer Internship . Project built within 25 hrs, with no prior knowled

Oct 10, 2021

Simple example using Git actions + Argo CD + K8S + Docker and GO lang

CICD-simple_example Simple example using Git actions + Argo CD + K8S + Docker and GO lang Intro Pre reqs Have an ArgoCD account and Installed. Docker

Oct 28, 2021

A ready to use Pastebin written in Go Lang, fork the files and start editing/using it.

A ready to use Pastebin written in Go Lang, fork the files and start editing/using it.

Dec 31, 2021

Minimalistic and High-performance goroutine worker pool written in Go

pond Minimalistic and High-performance goroutine worker pool written in Go Motivation This library is meant to provide a simple way to limit concurren

Dec 22, 2022

Kubei is a flexible Kubernetes runtime scanner, scanning images of worker and Kubernetes nodes providing accurate vulnerabilities assessment, for more information checkout:

Kubei is a flexible Kubernetes runtime scanner, scanning images of worker and Kubernetes nodes providing accurate vulnerabilities assessment, for more information checkout:

Kubei is a vulnerabilities scanning and CIS Docker benchmark tool that allows users to get an accurate and immediate risk assessment of their kubernet

Dec 30, 2022
SizedWaitGroup has the same role and close to the same API as the Golang sync.WaitGroup but it adds a limit on the amount of goroutines started concurrently.

SizedWaitGroup SizedWaitGroup has the same role and API as sync.WaitGroup but it adds a limit of the amount of goroutines started concurrently. SizedW

Jan 8, 2023
Speed up the memory allocation and improve the GC performance, especially for dynamic-memory-heavy applications.
Speed up the memory allocation and improve the GC performance, especially for dynamic-memory-heavy applications.

Linear Allocator for Golang Goal Speed up the memory allocation and improve the GC performance, especially for dynamic-memory-heavy applications. Pote

Dec 20, 2022
Worker - A Golang library that provides worker pools

Worker A Golang library that provides worker pools. Usage See *_test.go files. T

Apr 15, 2022
Worker pool library with auto-scaling, backpressure, and easy composability of pools into pipelines

workerpool Worker pool library with auto-scaling, backpressure, and easy composability of pools into pipelines. Uses Go 1.18 generics. Notable differe

Oct 5, 2022
golang worker pool , Concurrency limiting goroutine pool

golang worker pool δΈ­ζ–‡θ―΄ζ˜Ž Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks su

Dec 19, 2022
M3U generator for Stirr, optimized for Channels' custom channels.
M3U generator for Stirr, optimized for Channels' custom channels.

Stirr for Channels This simple Docker image will generate an M3U playlist and EPG optimized for use in Channels and expose them over HTTP. Channels su

Oct 7, 2022
Jun 6, 2022
Practical concurrency guide in Go, communication by channels, patterns

Go Concurrency Guide This guide is built on top of the some examples of the book Go Concurrency in Go and Go Programming Language Race Condition and D

Dec 28, 2022
Sample code snippet to familiarize golang . Concept of goroutines , channels, concurrency is implemented in a sample scenario

go-mysql-goroutines-channel Sample code snippet to familiarize golang . Concept of goroutines , channels, concurrency , interface, slice, error handli

May 29, 2022
A faster RWLock primitive in Go, 2-3 times faster than RWMutex. A Go implementation of concurrency control algorithm in paper

Go Left Right Concurrency A Go implementation of the left-right concurrency control algorithm in paper <Left-Right - A Concurrency Control Technique w

Jan 6, 2023