Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Go's standard context package and Gorilla context package

Tags:

go

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?

like image 708
TuralAsgar Avatar asked Dec 08 '22 18:12

TuralAsgar


1 Answers

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.

like image 151
Flimzy Avatar answered Dec 11 '22 07:12

Flimzy