Skip to content

Instantly share code, notes, and snippets.

@tzookb
Last active February 22, 2021 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzookb/c2cce3061747583675b7e606a7b8f14d to your computer and use it in GitHub Desktop.
Save tzookb/c2cce3061747583675b7e606a7b8f14d to your computer and use it in GitHub Desktop.
running ads on queue
package main
import (
"fmt"
"time"
)
func handleSpecificAd(wg *sync.WaitGroup, dId int) {
defer wg.Done()
count := 0
for count < 2 {
fmt.Println("handleSpecificAd", adId, count)
time.Sleep(1 * time.Second)
count++
}
}
/**
this one will read from the DB, it will query for all the ads
that are in state in queued and the queue time ie before now.
for each ad we find, we can either run it on synchronously,
or we can run it in another goroutine.
I assume running synchronously will be simpler for now,
espcially as we dont have a lot of traffic now.
*/
func bgWorker() {
count := 0
for {
fmt.Println("bgworker round")
var wg sync.WaitGroup
// fetch "n" items from DB
fetchItems := fetchFromDb(limit: n)
// each item will be sent to processing
for item in fetchItems {
wg.Add(1)
go handleSpecificAd(&wg, fetchItems)
}
//wait until all items processing is done
wg.Wait()
}
}
/**
this one is out main app that currently serves the grpc app
+
calling the bg worker on a goroutine
*/
func main() {
// this worker will handle the jobs
go bgWorker()
grpc()
//this is the main app grpc
for {
time.Sleep(2 * time.Second)
fmt.Println("main app stuff")
}
}
@junyutian
Copy link

We may want to split fetchItems to groups, and assign each group for func handleSpecificAd, instead of each item for func handleSpecificAd, to control the number of go gorountines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment