Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go http concurrency request to outside #15479

Closed
andymacau853 opened this issue Apr 28, 2016 · 4 comments
Closed

go http concurrency request to outside #15479

andymacau853 opened this issue Apr 28, 2016 · 4 comments

Comments

@andymacau853
Copy link

andymacau853 commented Apr 28, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.4.2 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
    CentOS 7.2 Minimal Desktop
    (Linux Development 3.10.0-327.el7.x86_64 cmd/cgo: fails with gcc 4.4.1 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux)

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/xxxxxxx/Desktop/maas"
GORACE=""
GOROOT="/usr/lib/golang"
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

  1. What did you do?
    go http concurrency request to outside
  2. What did you expect to see?
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xb code=0x1 addr=0x20 pc=0x401a28]
  3. What did you see instead?
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xb code=0x1 addr=0x20 pc=0x401a28]
@davecheney
Copy link
Contributor

Please provide a runnable piece of sample code that demonstrates the
problem.

On Thu, 28 Apr 2016, 19:41 John Kou, [email protected] wrote:

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?
go version go1.4.2 linux/amd64
2.

What operating system and processor architecture are you using (go env
)?
CentOS 7.2 Minimal Desktop
(Linux Development 3.10.0-327.el7.x86_64 #1
#1 SMP Thu Nov 19 22:10:57 UTC
2015 x86_64 x86_64 x86_64 GNU/Linux)

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/johnkou/Desktop/maas"
GORACE=""
GOROOT="/usr/lib/golang"
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

What did you do?
go http concurrency request to outside
2.

What did you expect to see?
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x401a28]
3.

What did you see instead?
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x401a28]


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#15479

@andymacau853
Copy link
Author

andymacau853 commented Apr 28, 2016

var job_temp HTTP
json.NewDecoder(req.Body).Decode(&job_temp)
go func(job_temp *HTTP) {
for {
var method string
switch job_temp.RequestMethod {
case 0: method = "GET"
case 1: method = "POST"
case 2: method = "PUT"
case 3: method = "DELETE"
case 4: method = "PATCH"
case 5: method = "HEAD"
}
client := &http.Client{Timeout: time.Duration(int64(time.Second) * int64(job_temp.RequestTimeout))}
request, _ := http.NewRequest(method, "http://" + job_temp.RequestAddress + ":" + strconv.FormatInt(int64(job_temp.RequestPort), 10), nil)
response, _ := client.Do(request)
response.Close = true
fmt.Println("This is a HTTP job for " + job_temp.RequestAddress + " Result is: " + strconv.FormatInt(int64(response.StatusCode), 10))
time.Sleep(time.Second * time.Duration(job_temp.JobInterval))
}
}(&job_temp)

  1. The job will run every specific interval time, it seems that some bugs when concurrency by client.Do(request).
  2. As this is for API, I send a job to this API then API will send another http request, but if many http request to outside by call client.Do, the error was showed

@andymacau853
Copy link
Author

type HTTP struct {
JobID string
JobUserID string
JobName string
JobInterval uint8
RequestAddress string
RequestMethod uint8
RequestVersion uint8
RequestPort uint32
RequestHeader []HTTPHeader
RequestBody string
RequestUsername string
RequestPassword string
RequestTimeout uint8
ResponseBody string
ResponseContain bool
}

@davecheney
Copy link
Contributor

Thank you for providing a code sample, in future, please provide it in a runnable form. What you have provided is not runnable.

With that said, you are ignoring the error from json decode, which if it fails will probably leave job_temp undefined. I'm guessing your program crashes at the switch on job_temp.RequestMethod.

The rest of your program is also ignoring any error checking. You must check each and every error if you want to want to use the result from an operation that can possibly return an error.

I am going to close this error as there is no bug to be fixed. We do not use the issue tracker for general discussion, I recommend you ask your question in another forum, see https://golang.org/wiki/Questions.

@golang golang locked and limited conversation to collaborators Apr 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants