FAQ
In this slice (http://talks.golang.org/2013/bestpractices.slide#30) , the
author said it would cause goroutine leaks. But I can't see why, it suppose
that if the channel write in brocastMsg is blocked, then range would never
end, then function would never return which means the function is still
running, it makes sense that the channel is not garbage collected. If the
brocastMsg is not blocked, then it would return, then by no means the
channel would no be garbage collected.

package main

import (
"fmt"
"net"
"time"
)

func sendMsg(msg, addr string) error {
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
defer conn.Close()
_, err = fmt.Fprint(conn, msg)
return err
}

func broadcastMsg(msg string, addrs []string) error {
errc := make(chan error)
for _, addr := range addrs {
go func(addr string) {
errc <- sendMsg(msg, addr)
fmt.Println("done")
}(addr)
}

for _ = range addrs {
if err := <-errc; err != nil {
return err
}
}
return nil
}

func main() {
addr := []string{
"localhost:8080",
"http://google.com",
}
err := broadcastMsg("hi", addr)

time.Sleep(time.Second)

if err != nil {
fmt.Println(err)
return
}
fmt.Println("everything went fine")
}


Also following another example, by using buffered chans, we make sure that
it won't causing goroutine leaks:

func broadcastMsg(msg string, addrs []string) error {
     errc := make(chan error, len(addrs))
     for _, addr := range addrs {
         go func(addr string) {
             errc <- sendMsg(msg, addr)
             fmt.Println("done")
         }(addr)
     }

     for _ = range addrs {
         if err := <-errc; err != nil {
             return err
         }
     }
     return nil
}


I still se no sense in this, so long as it's blocked, buffered channels
won't help.

It's posted by a go author, so I can't just skip it, because either I have
something important and remaining unknown to me about go or the author
makes an bad example there.
I hope you nice guys can help me parse this.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jesse McNelis at Feb 19, 2014 at 3:14 am

    On Wed, Feb 19, 2014 at 12:22 PM, Van Hu wrote:

    In this slice (http://talks.golang.org/2013/bestpractices.slide#30) , the
    author said it would cause goroutine leaks. But I can't see why, it suppose
    that if the channel write in brocastMsg is blocked, then range would never
    end, then function would never return which means the function is still
    running, it makes sense that the channel is not garbage collected. If the
    brocastMsg is not blocked, then it would return, then by no means the
    channel would no be garbage collected.
    If one of the errors written to the errc channel is not nil the range loop
    will exit early and all the sendMsg goroutines waiting to write to that
    channel with never have their values read and will wait forever.

    Also following another example, by using buffered chans, we make sure that
    it won't causing goroutine leaks:

    func broadcastMsg(msg string, addrs []string) error {
    errc := make(chan error, len(addrs))
    for _, addr := range addrs {
    go func(addr string) {
    errc <- sendMsg(msg, addr)
    fmt.Println("done")
    }(addr)
    }

    for _ = range addrs {
    if err := <-errc; err != nil {
    return err
    }
    }
    return nil
    }


    I still se no sense in this, so long as it's blocked, buffered channels
    won't help.
    The goroutines won't block when writing to the buffered channel because
    it's large enough to hold the error values written to it by all the created
    goroutines.
    The range loop exiting early and not reading all the values from the
    channel doesn't matter because the writing goroutines are no longer blocked
    waiting to send their values, since those values go in to the channel
    buffer.

    All the goroutines can finish and any values left in the errc channel will
    be garbage collected once it goes out of scope ie. when broadcastMsg
    returns.


    It's posted by a go author, so I can't just skip it, because either I have
    something important and remaining unknown to me about go or the author
    makes an bad example there.
    I hope you nice guys can help me parse this.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    =====================
    http://jessta.id.au

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 19, '14 at 2:36a
activeFeb 19, '14 at 3:14a
posts2
users2
websitegolang.org

2 users in discussion

Van Hu: 1 post Jesse McNelis: 1 post

People

Translate

site design / logo © 2022 Grokbase