Infinite goroutines until received desired response

Issue

I’m trying launch goroutines in an infinite loop until I get the response I’m looking for but the select is unreachable if I change for i := 0; i < 10; i++ {} to for {}. What’s a pattern for solving this?

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func myFunc() float64 {

    c := make(chan float64)

    for i := 0; i < 10; i++ {
        go func() {

            var value float64
            value = someOp()

            if value > .9 {
                c <- value
            }

        }()
    }

    // unreachable code the if the for loop above is infinite
    for {
        select {
        case x := <-c:
            return x
        default:
        }
    }

}

func someOp() float64 {
    rand.Seed(time.Now().UnixNano())
    return rand.Float64()
}

func main() {
    fmt.Println(myFunc())
}

Solution

Starting an unlimited number of goroutines is usually not a good idea. A better approach is to start a fixed number of goroutines that loop looking for an answer. Return from these goroutines when an answer is found.

func myFunc() float64 {
    c := make(chan float64, 1) // Size 1 prevents race between main goroutine and workers

    done := make(chan struct{})
    defer close(done)

    // Start a fixed number of goroutines
    for i := 0; i < 10; i++ {
        go func() {
            for {
                select {
                case <-done:
                    // myfunc exited with result, return from this goroutine
                    return
                default:
                    var value float64
                    value = someOp()
                    if value > .9 {
                        select {
                        case c <- value:
                            // This is first goroutine to send a value
                        default:
                            // Another goroutine sent a value
                        }
                        return
                    }

                }
            }
        }()
    }
    return <-c
}

https://play.golang.org/p/SRpeT8k34eA

Answered By – Bayta Darell

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.