Your use of sync.WaitGroup looks strange. Typically you would expect to
call wg.Add(1) before a goroutine is started, wg.Done() before it returns
and wg.Wait() to ensure all goroutines have completed.
This doesn't seem to be what you're doing.

Consider restructuring to make best use of the goroutines (start them
first) with the WaitGroup being used to detect when they complete
processing all work items.

Other things, your switch on cronType is invariant so could result in
entries added to jobs never being processed hence wg.Wait blocking forever,
might be your leak.

Finally no need for bare return at the end of functions

Hope that helps.

On Mon, 3 Jul 2023 at 04:25, Rohit Rohit <rohit206...@gmail.com> wrote:

> I am running a cron <https://pkg.go.dev/gopkg.in/robfig/cron.v3> whose
> code is written in Golang <https://pkg.go.dev/github.com/gin-gonic/gin>,and
> i am using mongoDb(version -4.4) as database, the cron runs after every 5
> minutes of interval, i have been noticing that when i start the cron within
> a week the cpu usage hikes from 0 to 60% or even more and i have to restart
> the cron service so that it shouldn’t effect the services of cron later on,
> which means that there’s an memory leak, resources are not being released
> properly, i’m not able to figure out what might be the reason, i have been
> using the single db connection for the whole cron let’s say a single cron
> is being for 2000 merchants. I’m running my cron with Worker pool using
> Buffered Channels. Below is the code which i am using for running my cron
> using worker pool
> func RunCronForDbNames(cronType, subsType string) {
> cronStatus := GetCronStatusConditions(cronType)
> if cronStatus {
> dbNames, _ := controller.GetDbNamesForCron(subsType, cronType)
> if len(dbNames) == 0 {
> return
> }
> client, ctx := ConnectDb("ip")
> defer client.Disconnect(ctx)
> cronDataChannel := make(chan view.CronChannelData, len(dbNames))
> var wg sync.WaitGroup
> startTime := time.Now().Unix()
> var cronChData view.CronChannelData
> cronChData.Database.Client = client
> cronChData.Database.Ctx = ctx
> cronChData.StartTime = startTime
> for _, dbName := range dbNames {
> isContinue := db.CheckGlobalCronStatus(cronType, dbNames)
> if !isContinue {
> continue
> }
> wg.Add(1)
> contextKeys := make(map[string]interface{})
> contextKeys["db_name"] = dbName
> contextKeys["role"] = ""
> var c gin.Context
> c.Keys = make(map[string]any)
> c.Keys = contextKeys
> cronChData.C = c
> cronChData.Database.MainDatabase = dbName
> cronDataChannel <- cronChData
> }
> close(cronDataChannel)
> for w := 1; w <= 10; w++ {
> go Worker(w, cronDataChannel, &wg, cronType)
> }
> wg.Wait()
> }
> return
> }
>
> My worker is running with 10 workers at a time
> func Worker(id int, jobs <-chan view.CronChannelData, wg *sync.WaitGroup,
> cronType string) {
> switch cronType {
> case config.CompleteBookingCron:
> for job := range jobs {
> controller.CompleteBookingsCron(job, wg)
> }
> }
> return
> }
>
> In CompleteBookingsCron, I’m subtracting the WaitGroup using wg.Done() and
> marking the bookings as completed and send mails and Sms to our customers
> based on their settings. For sending Emails and Sms, go-routines are being
> used.
>
> Can someone help me find out what could be the reason for the increasing
> of cpu usage, what things should i follow in order to getting the resources
> freed up properly which should not increase the Cpu usage?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/2e700735-7c77-4d4b-95fb-f74599517b3fn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/2e700735-7c77-4d4b-95fb-f74599517b3fn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA38peZwRfdNUYAgho8sjWJXMV1AAAJtT5KW1e5eM7Syhbz0zg%40mail.gmail.com.

Reply via email to