douwen5985 2017-04-30 08:34
浏览 68
已采纳

Golang标准上下文包和Gorilla上下文包之间的区别

What are the differences? Which one is more powerful? Can it be accomplished the same thing by using only standard context without using any third party libraries?

  • 写回答

1条回答 默认 最新

  • duanao4729 2017-04-30 09:24
    关注

    Gorilla's context package

    stores values shared during a request lifetime.

    The official context package

    carries deadlines, cancelation signals, and other request-scoped values across API boundaries and between processes.

    So from the outset, it's obvious that the official package does much more. But before getting into the details, some history:

    Gorilla's context package predates the official Go context package. It was created to solve a fundamental problem when responding to HTTP requests: Different middlewares and handlers need to be able to share request-scoped state. Things like the authenticated user's name and user ID, as well as the results of information retrieved from a database before sending it to a template to be parsed, etc.

    Gorilla's context package does this through a pretty ugly hack: It creates a map with the HTTP request pointer as a key. To make this concurrency-safe, it wraps all access to this map in mutexes, which make the access slower (although realistically, it probably only matters for very busy web sites).

    The Go context package, as stated, came later, and with a different need in mind. The Go context package exists primarily to solve the problem of cancelling operations after they're no longer needed.

    Before this, if you were serving an HTTP request, and the user closed their web browser or hit the 'Stop' button, or their wifi connection dropped, you would have no way of knowing. Your server would happily continue chugging along, fetching values from the database, rendering templates, etc, just to send the result back to... nobody.

    Or maybe your program needs to fetch data from a bunch of remote APIs, but you're only willing to wait 10 seconds. After 10 seconds, you want to cancel the pending requests.

    With the Go context package, these things are possible--and easy. By providing a cancelable context, the http library can now tell your HTTP server that the HTTP request was cancelled by the client. Or you can set a context with a timeout for the latter scenario.

    So you can see, the two packages are intended to solve entirely different problems.

    However, the official Go context package also has an extra feature. The WithValue method allows you to pass along arbitrary data in a context. This does serve the same purpose as Gorilla's context package, but kind of as an after thought.

    Best practice these days is to use the official context package. But this should be done primarily for cancellation purposes. As a secondary benefit, you can also use it to pass along values--the same sorts of values you would pass in Gorilla's context.

    But if you're only using it to pass along values, you're missing about 90% of its benefit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义