是否可以在 Gin 服务器中处理客户端套接字事件关闭(基于 net/http 包)?

https://github.com/gin-gonic/gin使用 HTTP 服务器。是否可以处理客户端套接字关闭?在处理程序中


 router.GET("/v1/get/*uri", func (c *gin.Context) {

     var url = c.Param("uri")

     if url[0:1] == "/" {

        url = url[1:]

     }

     req, err := http.NewRequest("GET", url, nil)

我向 url 发出 HTTP 客户端请求。如果我从我的 wget/curl 客户端关闭与 gin 服务器的连接,gin HTTP 客户端请求仍会被处理。我想中断客户端套接字断开事件上的处理程序执行,以免浪费资源。


ITMISS
浏览 280回答 1
1回答

宝慕林4294392

如果客户端断开连接,则 gin.Context.Done() 返回。http.Transport.CancelRequest() 中断对 url 的 http 请求。所以 http 请求是在 goroutine 中发出的,我们等待 select 中的第一个事件:http请求完成客户端与 gin 服务器断开连接router.GET("/v1/get/*uri", func (c *gin.Context) { var url = c.Param("uri") if url[0:1] == "/" { url = url [1:]}&nbsp; tr := &http.Transport{ TLSClientConfig : (&tls.Config{ InsecureSkipVerify: true}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DisableKeepAlives : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DisableCompression : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MaxIdleConns : 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MaxConnsPerHost : 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IdleConnTimeout : 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ResponseHeaderTimeout : 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExpectContinueTimeout : 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ForceAttemptHTTP2 : true}&nbsp; &nbsp; client := http.Client{ Timeout: 20 * time.Second, Transport : tr}&nbsp; &nbsp; req, err := http.NewRequest("GET", url, nil)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fail(1, "NewRequest")&nbsp; &nbsp; }&nbsp; &nbsp; for k, v := range c.Request.Header {&nbsp; &nbsp; &nbsp; &nbsp; req.Header.Set(k, strings.Join(v, "\n"))&nbsp; &nbsp; }&nbsp; &nbsp; is_url_done := make(chan HTTPRequestResult, 1)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; resp, err := client.Do(req)&nbsp; &nbsp; &nbsp; &nbsp; is_url_done <- HTTPRequestResult{res, err}&nbsp; &nbsp; }()&nbsp; &nbsp; for {&nbsp; &nbsp; select {&nbsp; &nbsp; case <-c.Done():&nbsp; &nbsp; &nbsp; &nbsp; tr.CancelRequest(req)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; case r:= <-is_url_done:&nbsp; &nbsp; &nbsp; &nbsp; if r.err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.String(http.StatusInternalServerError, "Error: %v\n", r.err)&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var headers = map[string]string{}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for key, values := range(r.res.Header) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers[key] = strings.Join(values, ", ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.DataFromReader(r.res.StatusCode, r.res.ContentLength, "test", r.res.Body, headers)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go