Ent: How to use context correctly?

Created on 8 Sep 2020  路  1Comment  路  Source: ent/ent

This can be a dumb question due to I am new to context, my apologies first if I should not ask here.

For each operation with the client, it needs a context object. Should I create and save the only one context as the global variable then use it, or create it by context.Background() every time when it is required then pass it to the function, or by other methods?

Thanks!

Question

Most helpful comment

Hey @lynkas! That's not a dumb question at all, if you're new to Go's context, I'll recommend you to read more about it here.

Sorry in advance that I'm keeping it short, I don't have too much time at the moment, and I prefer to "triage" issues as fast as I can. Also, some experts may explain it better than me.

There are many examples where Go context can be useful, here are some of our use cases:

  1. Passing cancelations to database operations when a request is cancelled (it's mentioned in the link above)
  2. Instrument the database operations (we use ocsql for this)
  3. Add some statistics from our hooks
  4. Inject {user, multi-tenancy, authorization} information to be used by the privacy later (and hooks)
  5. Interact with other tools (e.g. gqlgen for supporting field collections)

And there are more - like I mentioned above, I'll recommended you to read about it before.

Should I create and save the only one context as the global variable then use it, or create it by context.Background() every time when it is required then pass it to the function, or by other methods?

For testing, you can context.Background(), for real code, I'll recommend you to use http.Reuqest.Context (if the operation is triggered by an HTTP request).

Closing, but feel free to continue the discussion if you still have questions.

>All comments

Hey @lynkas! That's not a dumb question at all, if you're new to Go's context, I'll recommend you to read more about it here.

Sorry in advance that I'm keeping it short, I don't have too much time at the moment, and I prefer to "triage" issues as fast as I can. Also, some experts may explain it better than me.

There are many examples where Go context can be useful, here are some of our use cases:

  1. Passing cancelations to database operations when a request is cancelled (it's mentioned in the link above)
  2. Instrument the database operations (we use ocsql for this)
  3. Add some statistics from our hooks
  4. Inject {user, multi-tenancy, authorization} information to be used by the privacy later (and hooks)
  5. Interact with other tools (e.g. gqlgen for supporting field collections)

And there are more - like I mentioned above, I'll recommended you to read about it before.

Should I create and save the only one context as the global variable then use it, or create it by context.Background() every time when it is required then pass it to the function, or by other methods?

For testing, you can context.Background(), for real code, I'll recommend you to use http.Reuqest.Context (if the operation is triggered by an HTTP request).

Closing, but feel free to continue the discussion if you still have questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sno6 picture sno6  路  4Comments

satriahrh picture satriahrh  路  3Comments

rotemtam picture rotemtam  路  4Comments

errorhandler picture errorhandler  路  4Comments

jbub picture jbub  路  5Comments