Priority on Select cases with channel

Issue

I have this function so that the methods inside, run on a ticker. My problem is that the second case, sigC, only works after the case ticker.C is complete, which is not ideal because the program runs with flags, so if I want to change the flags and alter the program’s behaviour, I have to wait for the ticker methods to finish running, and that can take some time.

My objective is that when I press Ctrl+C, the program finishes running immediately.

func report() error {
    ticker := time.NewTicker(timeConfig)

    sigC := make(chan os.Signal, 1)
    signal.Notify(sigC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt)

    for range ticker.C {
        select {
        case <-ticker.C:
            connection = connectionInit()

            tagValues, err := fetchLatestTags()
            if err != nil {
                return err
            }
            if len(tagValues) >= threshold {
                metrics, err := fetchMetrics(tagValues)
                if err != nil {
                    return err
                }
                if stdout {
                    err = locally(metrics)
                    if err != nil {
                        return err
                    }
                } else {
                    err = sendMail(metrics)
                    if err != nil {
                        return err
                    }
                }
            }
            connection.Close()
        case <-sigC:
            return nil
        }
    }
    return nil
}

I tried these solutions which are pretty similar to each other but to no avail:

[1]Force priority of go select statement

[2]Priority in Go select statement workaround

Solution

If I understood correctly what you want to do is to replace the range-over-channel loop with an infinite loop. i.e. for range ticker.C { ... } -> for { ... }.

And if you want the program, rather than the report function, to finish immediately without waiting on the code in the case <-ticker.C: block to finish, you should execute that block of code in a separate goroutine and update the case <-sigC: block from return nil to os.Exit(1).

func report() {
    ticker := time.NewTicker(timeConfig)
    defer ticker.Stop()

    sigC := make(chan os.Signal, 1)
    signal.Notify(sigC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt)

    for {
        select {
        case <-ticker.C:
            go func() {
                connection = connectionInit()

                tagValues, err := fetchLatestTags()
                if err != nil {
                    return
                }
                if len(tagValues) >= threshold {
                    metrics, err := fetchMetrics(tagValues)
                    if err != nil {
                        return
                    }
                    if stdout {
                        err = locally(metrics)
                        if err != nil {
                            return
                        }
                    } else {
                        err = sendMail(metrics)
                        if err != nil {
                            return
                        }
                    }
                }
                connection.Close()
            }()
        case <-sigC:
            os.Exit(1)
        }
    }
}

Answered By – mkopriva

Answer Checked By – Clifford M. (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.