dsbezji539113152 2017-08-24 03:31
浏览 115

如何在golang中选择默认情况下阻止?

I am writing data to network.
The writing goroutine is like this.

forend:
for{
    select{
    case buf,ok:=<-sendqueue:
        if !ok{
            break forend
        }
        writeBuffer(conn,buf)
    }
}

The variable conn is a net.Conn.
Then I want to use bufio to replace net.Conn.

iowriter:=bufio.NewWriter(conn)

iowriter will cache the data.To reduce the delay,I must flush the iowriter immediately when there is no more data in sendqueue.
So I add a default case to the writing goroutine

forend:
for{
    select{
    case buf,ok:=<-sendqueue:
        if !ok{
            break forend
        }
        writeBuffer(iowriter,buf)
    default:
        iowriter.Flush()
        time.Sleep(time.Millisecond)
    }
}

The time.Sleep is necessary,otherwise the goroutine will run busy loop.
But in this case, the really demand is to block not to sleep.
Finally, I found a solution,with two selects.

forend:
for{
    select{
    case buf,ok:=<-sendqueue:
        if !ok{
            break forend
        }
        writeBuffer(iowriter,buf)
    }
nextloop:
    for{
        select{
        case buf,ok:=<-sendqueue:
            if !ok{
                break forend
            }
            writeBuffer(iowriter,buf)
        default:
            iowriter.Flush()
            break nextloop
        }
    }
}

But this solution is complicate.The second select is the duplicate of the first with a default case.Is there any better solution?

~~~~~~~~~~~~~~~~~~~~~~~ More explanation
The behavior here is like this.If the sendqueue is not empty,I continue to pop data and send it.If the sendqueue is empty,I want to flush the data cached in iowriter immediately and then waiting on the sendqueue again.
This behavior can be abstract like this.I want to do something when channel is empty,and then waiting on channel again,with blocking.I found the two duplicate select solution.But that solution may become more complicated when waiting on more than one channel.So I am looking for a better solution.

  • 写回答

2条回答 默认 最新

  • doutuan9357 2017-08-24 04:34
    关注

    We can prevent the loop from constantly flushing with the help of a ticker like so:

    ticker := time.NewTicker(time.Millisecond * 10)
    defer ticker.Stop()    
    for {
            select {
            case buf,ok:=<-sendqueue:
                if !ok{
                    return
                }
                writeBuffer(iowriter,buf)
            case <-ticker.C:
                   iowriter.Flush()
            }
    }
    

    Your data will now be sent on the pipe at latest 10ms after it's ready.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)