FAQ
Hi GoNuts,

I'm trying to get familiar with the websocket api for Go. I found the
following code example on github which at first I thought to be a good
basic example of getting a simple websocket example up and running.

*Bad example I think*:
https://github.com/Niessy/websocket-golang-chat/blob/master/chat.go

But then I realized something: assuming the websocket api does one
goroutine for each connection, this example is probably bad because the
author is reading/writing to a shared map. To the untrained eye, I think
it's easy for people like myself to not realize that underneath the covers
there is concurrency happening that could make this an issue. Aside from
good documentation or studying implementation details this could be a real
trap for people new to Go right?

Can anyone confirm my suspicion that the author should either lock the map
appropriately or use a channels as this following example which I think is
nicely done from the author of the Redigo library. In this example he
doesn't share anything and instead chooses to strictly communicate through
channels.

*Good example:* https://gist.github.com/garyburd/1316852

Thanks for any insight,

Still trying to wrap my head around Go as I continue with this language.

-Ralph Caraveo


--
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

  • Islan Dberry at Nov 3, 2013 at 3:25 am
    You are correct about the first example. There is a race condition. A mutex
    can be used to fix the bug.

    You don't need to study the implementation to learn that there'a goroutine
    per connection, but you do need to read the documentation very carefully.
    The documentation is at http://golang.org/pkg/net/http/#Server.Serve
      and http://golang.org/pkg/net/http/#Serve.

    --
    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.
  • Deckarep at Nov 3, 2013 at 3:42 am
    Okay,

    Thanks for the extra insight Islan and for confirming my suspicion.

    -Ralph Caraveo
    On Saturday, November 2, 2013 8:25:35 PM UTC-7, Islan Dberry wrote:

    You are correct about the first example. There is a race condition. A
    mutex can be used to fix the bug.

    You don't need to study the implementation to learn that there'a goroutine
    per connection, but you do need to read the documentation very carefully.
    The documentation is at http://golang.org/pkg/net/http/#Server.Serve and
    http://golang.org/pkg/net/http/#Serve.
    --
    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
postedNov 3, '13 at 2:25a
activeNov 3, '13 at 3:42a
posts3
users2
websitegolang.org

2 users in discussion

Deckarep: 2 posts Islan Dberry: 1 post

People

Translate

site design / logo © 2021 Grokbase