在下面的程序中我有两个路由器。其中一个在公共接入点上工作localhost:3000并充当公共接入点。它还可以将带有数据的请求发送到localhost:8000正在处理数据的另一个本地地址。第二个路由器正在工作localhost:8000并处理第一个路由器的处理请求。
第一路由器向第二使用功能发送带有上下文的请求http.NewRequestWithContext()。该值将被添加到上下文中,并且上下文将被添加到请求中。当请求到达第二个路由器时,它没有之前添加的值。
有些东西(例如错误处理)没有被编写,以便不在这里发布代码墙。
package main
import (
"bytes"
"context"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
func main() {
go func() {
err := http.ListenAndServe(
"localhost:3000",
GetDataAndSolve(),
)
if err != nil {
panic(err)
}
}()
go func() {
err := http.ListenAndServe( // in GetDataAndSolve() we send requests
"localhost:8000", // with data for processing
InternalService(),
)
if err != nil {
panic(err)
}
}()
// interrupt := make(chan os.Signal, 1)
// signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
// <-interrupt // just a cool way to close the program, uncomment if you need it
}
Run Code Online (Sandbox Code Playgroud)
func GetDataAndSolve() http.Handler {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/tasks/str", func(rw http.ResponseWriter, r *http.Request) {
// receiving data for processing...
taskCtx := context.WithValue(r.Context(), "str", "strVar") // the value is being
postReq, err := http.NewRequestWithContext( // stored to context
taskCtx, // context is being given to request
"POST",
"http://localhost:8000/tasks/solution",
bytes.NewBuffer([]byte("something")),
)
postReq.Header.Set("Content-Type", "application/json") // specifying for endpoint
if err != nil { // what we are sending
return
}
resp, err := http.DefaultClient.Do(postReq) // running actual request
// pls, proceed to Solver()
// do stuff to resp
// also despite arriving to middleware without right context
// here resp contains a request with correct context
})
return r
}
Run Code Online (Sandbox Code Playgroud)
func Solver(next http.Handler) http.Handler { // here we end up after sending postReq
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Context().Value("str").(string) == "" {
return // the request arrive without "str" in its context
}
ctxWithResult := context.WithValue(r.Context(), "result", mockFunc(r.Context()))
next.ServeHTTP(rw, r.Clone(ctxWithResult))
})
}
func InternalService() http.Handler {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.With(Solver).Post("/tasks/solution", emptyHandlerFunc)
return r
}
Run Code Online (Sandbox Code Playgroud)
您对上下文的理解不正确。
上下文(在一定程度上简化并参考NewRequestWithContextAPI)只是一个内存中的对象,您可以使用它来控制请求的生命周期(处理/触发取消)。
但是,您的代码正在发出 HTTP 调用,该调用使用 HTTP 协议通过网络(编组)进行传输。该协议不理解 golang 的上下文或其值。在您的场景中, 和/tasks/str都/tasks/solution在同一服务器上运行。如果它们位于不同的服务器上,可能也有不同的语言和应用程序服务器,那么上下文无法发送。
由于 API 位于同一服务器内,也许您可以避免进行完整的 HTTP 调用,而直接调用 API/方法。它可能也会变得更快。
如果您仍然想从上下文发送其他值,那么您必须使用其他属性(如 HTTP 标头、参数、正文)来发送所需的信息。这可以提供有关如何通过 HTTP 从上下文序列化数据的更多信息。
| 归档时间: |
|
| 查看次数: |
10269 次 |
| 最近记录: |