The easiest way to avoid this is to make the channel buffered with
enough room to consume all responses. Only the first one will be read,
but the rest will be garbage collected.

c := make(chan Result, len(replicas))

should be enough.

On Wed, Apr 4, 2018 at 4:20 AM, wilby yang <yang.wi...@gmail.com> wrote:
> I am new to golang and I am not sure if it is a stupid question. I am
> reading the slides of Rob Pike on go concurrency patterns in 2012. I think
> there is a resource leak in the below function. As the function will return
> after the first send&receive pair happens on channel c, the other goroutines
> trying to send on channel c will be blocked and prevents resources GC.
> Anyone knows golang well can confirm this? If it is resource leak, how can I
> detect it using what kind of golang tooling?down votefavorite
>
> func First(query string, replicas ...Search) Result {
>   c := make(chan Result)
>   searchReplica := func(i int) {
>     c <- replicas[i](query)
>   }
>   for i := range replicas {
>     go searchReplica(i)
>   }
>   return <-c
> }
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to