1

I am currently working on a Golang codebase that uses MongoDB. What bothers me is that all the time a query is made, there is a new context.WithTimeout created. Let me give an example:

ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()

collection := m.Client.Database(....
result := collection.FindOne(ctx, filter)

One way I tried to not use context is:

options.FindOne().SetMaxTime(defaultTimeout)

but when I tested, with some really small timeout(say 1 nanosecond), it did not result in a timeout? Do you have ideas about why it did not worked?

Also, is there some way to set such a timeout as a global option for all queries?
What about transactions?

4
  • SetMaxTime should set the time limit for query execution. How did you test it? Did you try to fetch documents? Maybe the actual query execution is delayed until you fetch results. Commented Jun 22, 2020 at 14:11
  • Well, I just removed the ctx and added options.SetMaxTime(1 * time.Nanosecond), so I expected that it should fail with some error like: "MaxTime timeouted". However, the query worked fine. What do you mean by fetch? Decoding the result? Commented Jun 22, 2020 at 14:39
  • It used to be, with the older mgo driver, that the query ran when you got the first result, not when you called Find. It looks like it is not so with the official driver, so disregard that comment. On the other hand, the mongodb timeouts are usually in milliseconds, so you might want to try this with a millisecond-range timeout. Commented Jun 22, 2020 at 14:54
  • It didn't worked with milliseconds either. The strange thing is that if you check the official driver README, in the usage section, their examples are using the context approach. So, maybe that's the path to go, idk Commented Jun 22, 2020 at 15:10

1 Answer 1

2

$maxTimeMS which I'm guessing is what you are trying to use limits how long the server would spend executing the query. The documentation mentions interrupt points, i.e. depending on the query the server may take more than stated limit and not interrupt.

Context timeout applies on the client side and specifies how long the driver will wait for server response.

The two timeouts really have different purposes. If you want to abort long running queries, you should use maxTimeMS (this will also free up server resources, otherwise the server will keep working on a query even after the client times out). If you want to guard against network failures, use client-side timeouts.

You can reuse a context across multiple operations. In effect this limits the total time all operations together can take.

To specify the socket timeout globally, use the socketTimeoutMS URI option.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.