Posts

Showing posts from September, 2025

golang mutex and RWMutex example

Here is an example of how to use Mutex  package main import (     "fmt"     "sync" ) type Counter struct {     mu     sync . Mutex     value int } func ( c * Counter ) Inc () {     c . mu . Lock ()         // lock     defer c . mu . Unlock () // unlock at function exit     c . value ++ } func ( c * Counter ) Value () int {     c . mu . Lock ()     defer c . mu . Unlock ()     return c . value } func main () {     var wg sync . WaitGroup     c := Counter {}     // 5 goroutines incrementing 1000 times each     for i := 0 ; i < 5 ; i ++ {         wg . Add ( 1 )         go func () {             defer wg . Done ()             for j := 0 ; j < 1000 ; j ++ {         ...

golang worker pool and jobs pattern

The worker pool pattern is where we have a pool of worker spin up and ready to received incoming work from a job queue.  We use waitgroup to wait for all the job to finish. Next we have a job channel that receive jobs and then results channel that collects outputs from these diffferent jobs.  We create a pool of worker (not threads) that ready to read in jobs coming in. New jobs that came in, will call processJob and place results in result channel.  In the end, we print out all the data i the result channel.  package main import (     "context"     "fmt"     "math/rand"     "sync"     "time" ) type Job struct {     ID     int     Data   string     Delay time . Duration } type WorkerPool struct {     jobs     <- chan Job     results chan <- Job     workers int     wg       sync . WaitGro...

golang method override

In golang we can do method override. This can be illustrated in the code below:- package main import "fmt" type Animal struct {     Name string } func ( a Animal ) Speak () {     fmt . Println ( "Animal speaks" ) } type Dog struct {     Animal } // Dog can override by defining its own Speak method func ( d Dog ) Speak () {     fmt . Println ( "Dog barks" ) } func main () {     dog := Dog {}     dog . Speak ()       // Calls Dog's Speak() - method override     dog . Animal . Speak () // Calls Animal's Speak() - explicit }   

golang struct embedding

In golang it doesn't quite has inheritance, instead it uses embedding. For example, let's take a look at the following  Here we are looking at embedding where C would be able to call Foo() directly or via c.A.Foo() package main type A struct {} func ( A ) Foo () {} type B struct { A } type C struct { A } func main () {     c := C {}     c . Foo ()     c . A . Foo () }

golang working with channel

A channel in Go is a communication mechanism that allows goroutines to communicate and synchronize with each other safely. Think of it as a "pipeline" or "conveyor belt" that passes data between goroutines. It is blocking operation - that means if you send 1 to a channel, you are expected to read from it too.  To declare a channel we use make.      ch := make ( chan int , 1 ) Channel are use to send and receive data. To say our function can send data or write to a channel, we  func sendOnly ( ch chan <- int ) {     fmt . Println ( "Sending 42..." )     ch <- 42     fmt . Println ( "Sent 42" ) } To get data from a channel we write as follows  func receiveOnly ( ch <- chan int ) {     value := <- ch     fmt . Println ( "Received:" , value ) } For example you have the following code package main import "fmt" func receiveOnly ( ch <- chan int ) {     value := <- ch...

golang for and select usage

 In Golang, for and select are used quite commonly with each other. For example, the following code would continue to run at certain interval before it receive context cancellation request and terminates.  package main import (     "context"     "fmt"     "time" ) func main () {     // Create a parent context and a cancel function     ctx , cancel := context . WithCancel ( context . Background ())     // Start a goroutine that does some work     go doWork ( ctx )     // Let it run for 3 seconds, then cancel     time . Sleep ( 3 * time . Second )     fmt . Println ( "Main: Cancelling the context..." )     cancel () // This signals all goroutines using this context to stop     // Wait a bit to see the cancellation take effect     time . Sleep ( 1 * time . Second )     fmt . Println ( "Main: Done" ) } func doWork ( ctx context . C...

kueue for machine learning jobs

Image
 Installing Kueue  We can install kueue using the following commands: export VERSION=v0.13.4 kubectl apply --server-side -f \   https://github.com/kubernetes-sigs/kueue/releases/download/$VERSION/manifests.yaml You have to wait until kueue controller manager to fully initialize before you can create cluster-queue. These are the resources being created.   Next we create namespace and deploy cluster-queue.yaml and local-queue.  git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples cd kubernetes-engine-samples/batch/kueue-intro kubectl create namespace team-a kubectl create namespace team-b kubectl apply -f cluster-queue.yaml kubectl apply -f local-queue.yaml kubectl apply -f flavour.yaml ./create_jobs.sh job-team-a.yaml job-team-b.yaml 10 I think the resource request is quite high. So you might want to change to it to smaller to save some cost. After that you can see our jobs running when you run  kubectl get job -n team-a kubectl...