FAQ
I am using the Go websocket
page: https://code.google.com/p/go/source/browse/websocket/
Is the type websocket.Conn safe to be accessed from multiple goroutines?
My scenario is that I have a single instance of *websocket.Conn and I want
to send messages across the connection from multiple goroutines. Is that
scenario supported or do I need to use a mutex or channels to protect the
connection?

Luke

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

  • Kyle Lemons at Apr 16, 2013 at 7:52 pm
    A proper implementation of net.Conn should be goroutine safe.

    http://tip.golang.org/pkg/net/#Conn

    On Tue, Apr 16, 2013 at 11:23 AM, Luke Mauldin wrote:

    I am using the Go websocket page:
    https://code.google.com/p/go/source/browse/websocket/
    Is the type websocket.Conn safe to be accessed from multiple goroutines?
    My scenario is that I have a single instance of *websocket.Conn and I want
    to send messages across the connection from multiple goroutines. Is that
    scenario supported or do I need to use a mutex or channels to protect the
    connection?

    Luke

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

    --
    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.
  • Kevin Gillette at Apr 18, 2013 at 7:20 pm
    Whether or not it's safe to call Write on the websocket from multiple
    goroutines concurrently (and thus one Write's bytes won't interleave with
    another's bytes) does not mean that concurrent writing of *messages* is
    safe. For that, the implementation would have to guarantee that it would do
    the necessary buffering to ensure that the returned n is never less than
    the length of the input byteslice, and the application would need to make
    sure it always sends whole message(s) in one Write call. Without *both* of
    these conditions, it's not safe to concurrently use a websocket connection
    without adding your own synchronization layer.
    On Tuesday, April 16, 2013 1:51:50 PM UTC-6, Kyle Lemons wrote:

    A proper implementation of net.Conn should be goroutine safe.

    http://tip.golang.org/pkg/net/#Conn


    On Tue, Apr 16, 2013 at 11:23 AM, Luke Mauldin <lukem...@gmail.com<javascript:>
    wrote:
    I am using the Go websocket page:
    https://code.google.com/p/go/source/browse/websocket/
    Is the type websocket.Conn safe to be accessed from multiple goroutines?
    My scenario is that I have a single instance of *websocket.Conn and I want
    to send messages across the connection from multiple goroutines. Is that
    scenario supported or do I need to use a mutex or channels to protect the
    connection?

    Luke

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

    --
    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.
  • John Nagle at Apr 18, 2013 at 5:04 pm

    On 4/16/2013 11:23 AM, Luke Mauldin wrote:
    I am using the Go websocket
    page: https://code.google.com/p/go/source/browse/websocket/
    Is the type websocket.Conn safe to be accessed from multiple goroutines?
    My scenario is that I have a single instance of *websocket.Conn and I want
    to send messages across the connection from multiple goroutines. Is that
    scenario supported or do I need to use a mutex or channels to protect the
    connection?
    For that, the question is not just "is websocket.Conn thread safe"
    with respect to the underlying library. The next level up in this
    question is "are sends atomic"? That is, if a task does a Write,
    is that data guaranteed not to be interleaved with other data
    from other tasks?

    For

    http://code.google.com/p/go/source/browse/websocket/websocket.go

    the answer appears to be yes, Write is atomic, because it locks.

    Note that the user must make sure that all writes and reads have
    completed before closing. That doesn't appear to be interlocked.

    John Nagle


    --
    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
postedApr 16, '13 at 6:23p
activeApr 18, '13 at 7:20p
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase