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

how can I use golang.org/x/net/context in func xxx(c *gin.Context)? #654

Closed
ynqa opened this issue Jul 4, 2016 · 2 comments
Closed

how can I use golang.org/x/net/context in func xxx(c *gin.Context)? #654

ynqa opened this issue Jul 4, 2016 · 2 comments

Comments

@ynqa
Copy link

ynqa commented Jul 4, 2016

As executing gin applications, I just use golang.org/x/net/context for async process, such as sending logs with fluentd, or r/w databases. So I create ctx := context.Background(), but I cloud not call it because gin.GET(func)'s func has only one arg: *gin.Context, cloud not include ctx (= context.Context).

@ynqa ynqa changed the title how can I use golang.org/x/net/context in func() how can I use golang.org/x/net/context in func xxx(c *gin.Context)? Jul 4, 2016
@ramoas
Copy link

ramoas commented Jul 17, 2016

@ynqa Currently, it looks like the gin.Context implements the context.Context interface in a very basic manner (no deadline, no done channel, no done error, but can get the request and values):
https://github.com/gin-gonic/gin/blob/master/context.go#L543-L568

Thus, it seems like you can only directly pass a gin.Context into code that expects a context.Context, not vice versa. You should also be able to derive a context.Context from a gin.Context.

From history, it looks like the gin experimentation with / introduction of context.Context occurred in May 2015, just before 1.0rc1.

If anyone has experience bridging / mixing gin.Context with context.Context, please share.

@ynqa
Copy link
Author

ynqa commented Jul 19, 2016

@ramoas Thank you for your reply. I see... that we have to use them in combination.
So, I make a mixing gin.Context with context.Context middleware by the reference econtext, and here: https://gist.github.com/ynqa/78e7889a313f67d4a5d0f77e56fa506d

if you use it, please write below:

g := gin.Default()
ctx := context.Background()
// you can put some processes into `ctx`
...
g.Use(middleware.Middleware(ctx))

Also you can call context.Context from FromContext(c *gin.Context).
I would appreciate it very much if you could point out anything to me!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants