FAQ
Hello,

I try to Write some bytes to net.TcpConn, but how can i know is CanWrite ?
i don't want blocking the goroutine...

I wrote a push service : github.com/Terry-Mao/gopush-cluster

the subscriber(with a key) has many connections.

when push a message to the key, i use a linked-list with all the
connections;

but if one connection's SND_BUF full, the call will blocking till the conn
is close.

now i use another goroutine ,and chan to handle this:



// Write different message to client by different protocol


func (c *Connection) Write(key string, msg []byte) {


     select {


     case c.Buf <- msg:


     default:


         c.Conn.Close()


         log.Warn("user_key: \"%s\" discard message: \"%s\" and close
connection", key, string(msg))

     }


}

// HandleWrite start a goroutine get msg from chan, then send to the conn.


func (c *Connection) HandleWrite(key string) {


     go func() {




         log.Debug("user_key: \"%s\" HandleWrite goroutine start", key)


         for {


             msg, ok := <-c.Buf


             if !ok {


                 log.Debug("user_key: \"%s\" HandleWrite goroutine stop",
key)

                 return


             }


             // write msg....

         }


     }()


}

--
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/d/optout.

Search Discussions

  • 毛剑 at Jan 24, 2015 at 6:45 am
    if I can know TcpConn is Can't Write ,i can simply discard the message.
    without another goroutine and the chan send operations;

    2015-01-24 14:43 GMT+08:00 毛剑 <maojian@cmcm.com>:
    Hello,

    I try to Write some bytes to net.TcpConn, but how can i know is CanWrite ?
    i don't want blocking the goroutine...

    I wrote a push service : github.com/Terry-Mao/gopush-cluster

    the subscriber(with a key) has many connections.

    when push a message to the key, i use a linked-list with all the
    connections;

    but if one connection's SND_BUF full, the call will blocking till the conn
    is close.

    now i use another goroutine ,and chan to handle this:



    // Write different message to client by different protocol


    func (c *Connection) Write(key string, msg []byte) {


    select {


    case c.Buf <- msg:


    default:


    c.Conn.Close()


    log.Warn("user_key: \"%s\" discard message: \"%s\" and close
    connection", key, string(msg))

    }


    }

    // HandleWrite start a goroutine get msg from chan, then send to the conn.


    func (c *Connection) HandleWrite(key string) {


    go func() {




    log.Debug("user_key: \"%s\" HandleWrite goroutine start", key)


    for {


    msg, ok := <-c.Buf


    if !ok {


    log.Debug("user_key: \"%s\" HandleWrite goroutine stop",
    key)

    return


    }


    // write msg....

    }


    }()


    }


    --

    移动游戏事业部

    毛剑 Terry Mao

    Tel:(86)18666923210 QQ:83873308

    --
    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/d/optout.
  • Tamás Gulácsi at Jan 24, 2015 at 8:48 am
    Use a goroutine and a chan per tcpconn, and discard messages which can't be put into the Chan.
    The goroutine should just receive on the Chan and write to the conn.

    --
    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/d/optout.
  • 毛剑 at Jan 24, 2015 at 9:35 am
    @Tamás Gulácsi
    if do like that ,per tcpconn need extra goroutine .. more memory cost...

    2015-01-24 16:48 GMT+08:00 Tamás Gulácsi <tgulacsi78@gmail.com>:
    Use a goroutine and a chan per tcpconn, and discard messages which can't
    be put into the Chan.
    The goroutine should just receive on the Chan and write to the conn.

    --
    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/d/optout.


    --

    移动游戏事业部

    毛剑 Terry Mao

    Tel:(86)18666923210 QQ:83873308

    --
    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/d/optout.
  • Mikespook at Jan 24, 2015 at 10:51 am
    I don't think an extra goroutine for a connection would be a trouble to
    your application. How many connections do you have in a single server?

    However, the best way that tests if it could read or write should be just
    reading or writing it. When you get a error, that means you can't read or
    write rightnow.
    On Jan 24, 2015 5:36 PM, "毛剑" wrote:

    @Tamás Gulácsi
    if do like that ,per tcpconn need extra goroutine .. more memory cost...

    2015-01-24 16:48 GMT+08:00 Tamás Gulácsi <tgulacsi78@gmail.com>:
    Use a goroutine and a chan per tcpconn, and discard messages which can't
    be put into the Chan.
    The goroutine should just receive on the Chan and write to the conn.

    --
    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/d/optout.


    --

    移动游戏事业部

    毛剑 Terry Mao

    Tel:(86)18666923210 QQ:83873308

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 24, '15 at 6:43a
activeJan 24, '15 at 10:51a
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase