Write pipe reading into http response in golang

huangapple go评论103阅读模式
英文:

Write pipe reading into http response in golang

问题

以下是翻译好的内容:

这是架构:

客户端发送一个POST请求给服务器A

服务器A处理请求并发送一个GET请求给服务器B

服务器B通过服务器A将响应发送给客户端


我认为最好的方法是创建一个管道,读取GET请求的响应,并将其写入POST请求的响应中,但是我遇到了很多类型问题。

  1. func main() {
  2. r := mux.NewRouter()
  3. r.HandleFunc("/test/{hash}", testHandler)
  4. log.Fatal(http.ListenAndServe(":9095", r))
  5. }
  6. func handleErr(err error) {
  7. if err != nil {
  8. log.Fatalf("%s\n", err)
  9. }
  10. }
  11. func testHandler(w http.ResponseWriter, r *http.Request){
  12. fmt.Println("FIRST REQUEST RECEIVED")
  13. vars := mux.Vars(r)
  14. hash := vars["hash"]
  15. read, write := io.Pipe()
  16. // 在没有读取器的情况下进行写入会导致死锁,因此在goroutine中进行写入
  17. go func() {
  18. write, _ = http.Get("http://localhost:9090/test/" + hash)
  19. defer write.Close()
  20. }()
  21. w.Write(read)
  22. }

当我运行这段代码时,我得到以下错误:

./ReverseProxy.go:61: cannot use read (type *io.PipeReader) as type []byte in argument to w.Write

有没有办法将io.PipeReader格式正确地插入到http响应中?
或者我完全错了吗?

英文:

Here is the schema :

Client sends a POST request to server A

server A process this and sends a GET to server B

server B sends a response through A to the client


I though the best idea was to make a pipe which would read the response of the GET, and write into the response of the POST, but I got many types problems.

  1. func main() {
  2. r := mux.NewRouter()
  3. r.HandleFunc("/test/{hash}", testHandler)
  4. log.Fatal(http.ListenAndServe(":9095", r))
  5. }
  6. func handleErr(err error) {
  7. if err != nil {
  8. log.Fatalf("%s\n", err)
  9. }
  10. }
  11. func testHandler(w http.ResponseWriter, r *http.Request){
  12. fmt.Println("FIRST REQUEST RECEIVED")
  13. vars := mux.Vars(r)
  14. hash := vars["hash"]
  15. read, write := io.Pipe()
  16. // writing without a reader will deadlock so write in a goroutine
  17. go func() {
  18. write, _ = http.Get("http://localhost:9090/test/" + hash)
  19. defer write.Close()
  20. }()
  21. w.Write(read)
  22. }

When I run this I get the following error:

./ReverseProxy.go:61: cannot use read (type *io.PipeReader) as type []byte in argument to w.Write

Is there a way, to properly insert a io.PipeReader format into an http response?
Or am I doing this in a totally wrong way?

答案1

得分: 8

你实际上并没有写入它,而是替换了管道的写入部分。

大致如下:

  1. func testHandler(w http.ResponseWriter, r *http.Request) {
  2. fmt.Println("FIRST REQUEST RECEIVED")
  3. vars := mux.Vars(r)
  4. hash := vars["hash"]
  5. read, write := io.Pipe()
  6. // 在没有读取器的情况下进行写入会导致死锁,因此在 goroutine 中进行写入
  7. go func() {
  8. defer write.Close()
  9. resp, err := http.Get("http://localhost:9090/test/" + hash)
  10. if err != nil {
  11. return
  12. }
  13. defer resp.Body.Close()
  14. io.Copy(write, resp.Body)
  15. }()
  16. io.Copy(w, read)
  17. }
  18. 虽然如此我同意 @JimB 的观点对于这种情况甚至不需要使用管道像这样的代码应该更高效
  19. ```go
  20. func testHandler(w http.ResponseWriter, r *http.Request) {
  21. vars := mux.Vars(r)
  22. hash := vars["hash"]
  23. resp, err := http.Get("http://localhost:9090/test/" + hash)
  24. if err != nil {
  25. // 处理错误
  26. return
  27. }
  28. defer resp.Body.Close()
  29. io.Copy(w, resp.Body)
  30. }
英文:

You are not actually writing to it, you're replacing the pipe's write.

Something along the lines of:

  1. func testHandler(w http.ResponseWriter, r *http.Request) {
  2. fmt.Println("FIRST REQUEST RECEIVED")
  3. vars := mux.Vars(r)
  4. hash := vars["hash"]
  5. read, write := io.Pipe()
  6. // writing without a reader will deadlock so write in a goroutine
  7. go func() {
  8. defer write.Close()
  9. resp, err := http.Get("http://localhost:9090/test/" + hash)
  10. if err != nil {
  11. return
  12. }
  13. defer resp.Body.Close()
  14. io.Copy(write, resp.Body)
  15. }()
  16. io.Copy(w, read)
  17. }

Although, I agree with @JimB, for this instance, the pipe isn't even needed, something like this should be more efficient:

  1. func testHandler(w http.ResponseWriter, r *http.Request) {
  2. vars := mux.Vars(r)
  3. hash := vars["hash"]
  4. resp, err := http.Get("http://localhost:9090/test/" + hash)
  5. if err != nil {
  6. // handle error
  7. return
  8. }
  9. defer resp.Body.Close()
  10. io.Copy(w, resp.Body)
  11. }

huangapple
  • 本文由 发表于 2016年11月23日 00:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/40747152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定