HTTP handler stuck

Hello,

I have the following code which is called by a handler at port 8002 at “/api”

https://play.golang.org/p/xFx4DfLpjsV

The handler function opens a JSON file called rubrik_locations at line 56, which contains the following test content:

  "rubrik" : [   
      {
        "site": {
              "server": "https://postman-echo.com/get?foo1=bar1&foo2=bar2"
              
          }
      },

      {
        "site": {
              "server": "https://postman-echo.com/get?foo1=bar1&foo2=bar2"
              
          }
      }
    ],
    "index": 1
}

The idea behind the code is to pass the “server” parameter to the real URLs in a loop every time the URL with the host is passed in the browser:

http://localhost:8002/api/test.com

The issue that I’m having is that the call gets stuck when it starts the loop (never stops loading, keeps spinning) and does not go further down or print anything in the “Output” struct that I have at line 110, where the slice interface should have been passed the results from “resps” channel:

output := Output{
	Success:   true,
	Vm:        slice,
	LogOutput: "Returned information location for host: " + server,
}

for me, the request https://postman-echo.com/get?foo1=bar1&foo2=bar2test.com takes more than 3 seconds and I get timeout (you have timeout set to 3 secs)

I don’t get why you put something to a channel and read if a bit later in the same goroutine.
Please notice that if you won’t put anything to the resps channel, it will never get any response if no error occurs.
When an error happens, you put the error to the channel but finish the function (return) so it never have the chance to execute.
So, if I change the code this way

		resps <- "HURRA"

		select {
		case slice := <-resps:
			fmt.Println(slice)
			return
		}

It works :slight_smile:

Minor suggestion - close the response even before checking the error.

if resp != nil {
    defer resp.Body.Close()
}

if err != nil {
			//append URL output to resps channel
			resps <- err
			return
}
1 Like

Thank you for the suggestions. I totally missed the return statements.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.