drip5880 2019-04-13 21:52
浏览 100
已采纳

如何与time.After进行惯用同步?

I'm writing an application that queues incoming requests. If a request has been on the queue for more than a certain amount of time, I'd like to throw a timeout. I'm doing that with time.After:

timeoutCh := time.After(5 * time.Second)
select {
    case <-timeoutCh:
         //throw timeout 504
    case <-processing:
         //process request
}

The processing channel (along with the request) is put on the queue, and when a request is taken off to be processed, I send a signal to the channel to hit the case statement:

processing <- true

The problem with this is that if timeoutCh has already been selected, the processing channel will block, so I need some way to check whether the request has timed out.

I considered using a shared atomic boolean, but if I do something like this:

case <-timeoutCh:
     requestTimedOut = true

and then check the boolean before sending to the processing channel, there's still a race condition, because the timeoutCh case may have been selected, but the bool not yet set to true!

Is there an idiomatic way of dealing with this sort of synchronization problem in Go?

  • 写回答

1条回答 默认 最新

  • doushenxu7294 2019-04-14 03:34
    关注

    Use a mutex coordinate processing of the data and timeout.

    Define a type to hold the mutex, input, result, a channel to signal completion of the work and a flag indicating that the work, if any, is complete.

    type work struct {
        sync.Mutex
        input    InputType
        result   ResultType
        signal   chan struct {}
        done     bool
    }
    

    The request handler creates and enqueues a work item and waits for a timeout or a signal from the queue processor. Either way, the request handler checks to see if the queue processor did the work and responds as appropriate.

    func handler(resp http.ResponseWriter, req *http.Request) {
        w := &queueElement{
            input: computeInputFromRequest(req)
            signal:  make(chan struct{})
        }
        enqueue(w)
    
        // Wait for timeout or for queue processor to signal that the work is complete.
        select {
        case <-time.After(5 * time.Second):
        case <-w.signal:
        }
    
        w.Lock()
        done := w.done  // Record state of the work item.
        w.done = true   // Mark the work item as complete.
        w.Unlock()
    
        if !done {
            http.Error(w, "Timeout", http.StatusGatewayTimeout)
        }  else {
            respondWithResult(resp, w.result)
        }
    }
    

    The queue processor will look something like this:

     for {
       w := dequeue()
       w.Lock()
       if !w.done {
          w.done = true
          w.result = computeResultFromInput(w.input)
          close(w.signal)
       }
       w.Unlock()
    }
    

    To ensure that the request handler waits on the result, the queue processor holds the lock while processing the work item.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题