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.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?