登录
首页 >  Golang >  Go问答

代理服务器的上下文超时问题

来源:stackoverflow

时间:2024-02-05 21:41:51 142浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《代理服务器的上下文超时问题》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

我正在尝试测试 HttpServer 代码。我在代码中模拟了我的代理调用 这是我的服务器代码:

func HttpServer(t *testing.T,version string,test ReqRes, basePath string, timeout int,transport metrics.HttpInstrumenter, corsConfig middleware.CorsConfig) (*http.Response,[]byte, bool){
    proxyAuthServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(test.proxyAuthStatusCode)
        _, err := w.Write([]byte(test.proxyAuthResp))
        if err != nil {
            return
        }
    }))

    defer proxyAuthServer.Close()

    configs :=config.Config{
        Outbounds: struct {
            APIAuthentication struct {
                AuthPath    string `yaml:"authPath"`
                AuthTimeout int    `yaml:"authTimeout"`
            } `yaml:"apiauthentication"`
            
        },
    }


    var mockProxy proxy.Proxies
    newproxy:=proxy.NewProxies(zap.NewNop(),proxyInstrumenter,proxyTransport)
    mockProxy,_=newproxy.Make(configs,proxyAuthServer.URL,"")

    proxyAuthServer.URL=proxyAuthServer.URL+configs.Outbounds.APIAuthentication.AuthPath


    service:=base.NewService(proxy.Proxies{ApiAuthenticationService:mockProxy.ApiAuthenticationService,OfferPrefixDataService:mockProxy.OfferPrefixDataService},true) 
    service=base.NewLoggingMiddleware(zap.NewNop())(service)
    service = base.NewInstrumentingService(instrumenter)(service)


    //Generating req
    resp,_:=http.DefaultClient.Do(req)
    body,_:=io.ReadAll(resp.Body)
    return resp,body,false

}

这是调用代理并给出错误的行。s 实现了我的 AuthProxy:

endpointResp, err := s.ValidateEndpoint(ctx, req)

每当我的代码进行代理调用时,proxyAuthServer 就会在我的调试器中返回如下错误:

endpointResp:接口 {} nil

错误(*net/url.Error): *{Op: "Get", URL: "http://127.0.0.1:58807/auth", Err: error(context.deadlineExceededError) {}} p>

当我将端点配置为 proxyAuthServerURL 并将 maxTimeout 配置为 AuthTimeout 时,为什么我的 proxyAuthServer 没有返回响应。有人可以解释一下吗


正确答案


我猜测原因与超时设置有关。

我认为,假设服务器没有问题,从客户端的角度来看,超时/截止日期可能是由于两件事而发生的。

  1. 带有截止日期上下文的请求。 (https://gist.github.com/orian/b54ce2b1cc3a4144b090eb5714725a43)

  2. 客户端超时 (https://medium.com/@jac_ln/how-to-test-real-request-timeout-in-golang-with-httptest-dbc72ea23d1a)

您的问题与截止日期无关。所以我猜大约是2。

我们可以通过这段代码轻松测试它。

package main

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

func main() {
    server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "This is only for test")
    }))

    req, _ := http.NewRequest("GET", server.URL, nil)

    client := http.Client{Timeout: time.Nanosecond}
    _, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return
    }

    return
}

好了,本文到此结束,带大家了解了《代理服务器的上下文超时问题》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>