0

I am developing a server application with the net/http package. Everytime a request arrives, the net/http package reads the request and create a http.Request object. My server needs to call AWS DynamoDB to serve every request, when calling DynamoDB's QueryWithContext function, I pass the context object in http.Request object, something like this:

ctx := request.Context()
ctx := context.WithValue(ctx, myKey, myVal)

input := &dynamodb.QueryInput{
   ....
}

result, err := dbclient.QueryWithContext(ctx, input)

Recently I saw several context canceled errors returned from QueryWithContext, I am thinking it might because the context object is reused and somehow the request is canceled so service call to DynamoDB also got canceled?

Should I reuse the context object when calling another service (like DynamoDB) or should I always create a new one? Thanks.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
codewarrior
  • 723
  • 7
  • 22

1 Answers1

2

Should I reuse the context object when calling another service (like DynamoDB) or should I always create a new one? Thanks.

That depends.

Do you want the DynamoDB requests to be tied to the request, or not?

In other words, if the HTTP request is cancelled, should the DynamoDB requst continue, or abort?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • First thing first, I want to, and I am figuring now, why the DynamoDB calls got "request canceled: context canceled" error, my guess is the reused context, but I don't know why the request got canceled. If that is the root cause, I would like to let DynamoDB call finishes even request is canceled already. – codewarrior Sep 22 '22 at 10:01
  • That usually means the HTTP request has finished or been canceled. If you're launching your queries in a separate goroutine, that can be a common cause. – Jonathan Hall Sep 22 '22 at 10:11
  • If call to the dynamodb is connected with the request handling (e.g. you need data from dynamodb to fill the response) then it should use the same ctx. If call to the dynamodb is action done in the background without synchronization with the request handling (e.g. you return a response in the handler, but you want to save something in a background to the dynamodb to shorten response time) then you need to create an another context, because such an action lives independently from the request – slsy Sep 22 '22 at 12:31
  • @Flimzy same goroutine. So that is odd why I saw `request canceled: context canceled` error from `QueryWithContext` – codewarrior Sep 22 '22 at 22:27
  • @slsy The request cannot be fulfilled until query results returned. So, the service call to DynamoDB is issued from same goroutine, with same context object retrieved from request object as I showed in my initial question. I just don't know why I received `request canceled: context canceled` error. AWS support said they didn't find any internal errors in DynamoDB in given time. And in my code, I never explicitly cancel a context object. – codewarrior Sep 22 '22 at 22:29
  • Based on the information you've provided, it sounds like the HTTP request was cancelled before DynamoDB returned. So a canceled context is actually the correct behavior in that case. – Jonathan Hall Sep 23 '22 at 08:05