dongye1143 2015-01-19 21:13
浏览 33
已采纳

Golang干净的项目不在渠道中,不会被读取

I am working on an http endpoint that will receive a request from a client and block until it receives an "ack" for that request from another server or until it passes a timeout. The communication between my code and the server is not included in this sample, but you can assume that for each request, an ack may be received eventually.

Since many requests will pass through my module in short periods of time, I cannot assume that a given ack is related the the request I am blocking on. EDIT: Clarification here since it has caused some confusion. Both requests and acks are received by the controller from external sources. This is why I am handling them asychronously. /EDIT For this reason, my code places acks back on the channel if they are not relevant. It is also important to note that http.ListenAndServe calls my functions asynchronously.

If the request is acked within the timeout, there are no problems. However, it the ack comes after the timeout has passed, it will be added to a channel and never removed. This will cause the channel to fill up. I am afraid to use a "cancel" channel because it is also possible that no ack will be received for a given request, causing the cancel channel to fill as well.

The Question: How can I keep late acks from filling my channel?/How can I identify and remove late acks?

Code below. No play.golang.org link because http.ListenAndServe :/

package main

import (
    "fmt"
    "net/http"
    "time"
)

const timeout = 10

func startEndpoint(w http.ResponseWriter, r *http.Request) {
    var ack string
    timer := time.NewTimer(time.Second * timeout)
    defer timer.Stop()

    m := r.RequestURI[len("/start/"):]
    fmt.Print(m)
AckRecycle:
    for {
        select {
        case ack = <-acks:
            if ack == m {
                //What we found was our own ack
                fmt.Print("+")
                w.Write([]byte("Ack received for " + ack))
                break AckRecycle
            } else {
                //What we found on the channel wasn't for us
                fmt.Print(".")
                time.Sleep(time.Millisecond * 100)
                acks <- ack
            }
        case <-timer.C:
            //We ran out of time waiting for our ack
            w.Write([]byte("Timeout waiting for " + m))
            break AckRecycle
        default:
            //Channel was empty
            fmt.Print("-")
            time.Sleep(time.Millisecond * 100)
        }
    }
    return
}

func ackEndpoint(w http.ResponseWriter, r *http.Request) {
    ack := r.RequestURI[len("/ack/"):]
    acks <- ack
    fmt.Print("Ack for " + ack)
    w.Write([]byte("Thanks!"))
    return
}

var acks = make(chan string, 10)

func main() {
    http.HandleFunc("/ack/", ackEndpoint)
    http.HandleFunc("/start/", startEndpoint)

    http.ListenAndServe("127.0.0.1:8888", nil)
}

NOTE: To test this, run it on your local machine. Curl/Wget 127.0.0.1:8888/start/bob and then Curl/Wget 127.0.0.1:8888/ack/bob. You can replace bob with any string to see the behavior.

I'm new to Go. Feel free to provide other feedback in the comments.

  • 写回答

2条回答 默认 最新

  • dongmin3754 2015-01-19 22:05
    关注

    Keep a map of "uuids in process"; when you receive a /start/ add it to the map, and when you receive an ack (or when the request times out) remove it. If you receive an ack that isn't in the map, discard it immediately.

    Be careful, as maps are not thread-safe by default.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: