[Golang] context

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi so today a deep dive of the context package ingo so this package allows you to build and manipulate a context and a context is just a scope holder so basically here just a quick example where you have an application where you can send a request and then this request will perform an http communication for instance and then subsequently we'll do um db operation for instance so if you use context you can pass specific contacts for this specific request and the contacts will be aware of which request it belongs to so you can do fancy stuff that we will see later on like you can store values inside a context but but more importantly the context package exposes tools for you to control the flow so what i mean by that is for instance for any reason we we have a timeout at the db layer and so the contacts can the context for that request can relay that information to the http layer for instance and cancel it for for instance another example is uh for instance um a user is canceling the request so then this cancellation can be propagated via the contacts and stop a long expensive db query for instance so um you will often often see that some libraries and sdks are exposing methods asking for context so let's crack on with uh the honest tract of the package which is the context so this is the scope holder that i was telling you about so you have two ways to instantiate this struct so first one is dot background this is a package method so let's just have a look to do that so you can see that it's simply saying context background so then if we try to see which methods are available on this um instance you can see four methods that we will dig deeper later on um the other way to instantiate a context is to use to do so you can see that it's just rendering a context to do here if you're building an application so context background will be um how you create a context from scratch at the beginning of your request and to do will be used when you're one of your component needs to use the context but you don't know yet uh how you're gonna pass it first instance method that we're gonna see is the dot value method so this one just allows you to inspect your your contacts um to ask if it has um the value for this key basically so the key here is my key so you can see that the ctx doesn't have this uh my key value basically so then um package method that we're gonna see is with value so with value is taking apparent context uh key and then the value for that game so we're gonna populate the key my key with the value one two three and um with value is rendering back a context so we're gonna call it ctx2 and we will just use the the instance method that we saw previously that value on context2 for the key nike basically here you can see what's a ctx2 looks like it's a context from with value basically so then if we run that you can see that one two three is the value for the key my key on the context two only okay so so let's have a look now to this um package method with cancel so with cancel is taking um parent contacts so let's send our ctx for instance and then it's returning a context and a function a cancel function so we can um [Music] so now let's have a look to an instance method called air standing for error on this context so we will log this error before calling the console function and then after to see what's going on so you can see that the first one it's rendering no error and second log line after invoking the cancel function you can see that it's a contacts consult and the package is exposing a constant called context cancer so you can see that the error is equal to this context cancer so you saw here how to cancel a context and how does it reflect um on the on the scope holder basically on the context so next package method is the with deadline so with deadline is taking a parent contacts and a time so here we're just gonna send t and t will be now and then now after uh one second for instance so with deadline is rendering back a context and a cancel function again which is the which has the same purpose of the previous package method with console that we saw so here we can log the contacts and let's have a look so what will happen the context will be cancelled after one second basically so we're gonna sleep for two seconds and we will see which errors are coming back to us so you see it's sleeping and then we have this error context deadline exceeded which is different from context cancer that we saw before so then here you have this constant from the context package which is deadline exceeded and we can test that it's indeed this error which is coming back um so here um i just want to show you that the consultant cancel function is exactly the same as we saw before so if i'm cancelling the context directly at line 21 we should have two concer two contacts uh cancel basically it's false because the context was cancelled and it wasn't a um deadline exceeded basically there you go so we just saw how to cancel uh context by giving a precise time basically so i want to show you another instance method that you have available on the context which is deadline basically so we're gonna do ctx2 dot deadline so the deadline method is um rendering to you the time of the deadline and a boolean which is going to tell you if there is a deadline or not basically so you can print them so the deadline and then the okay so here you can see that um the deadline is set up one second after we launched the uh this routine and the okay is true basically there you go so 13 36 13 against 36 12 basically one second after and there is a deadline which is due to this line 19. now let's have a look to this package method with timeouts so it's taking a parent context and now a duration not a time so we're gonna call it timeout and we're just gonna put um one second timer for instance so time second times one and then we're just gonna assign um to context two and then you can see that same pattern again you can have a cancel function which i'm not gonna show you because it's exactly the same as the width cancel and with deadline so if we just uh log the the error on the context 2 and wait two seconds so you can see that um it's rendering back for the second log line it's rendering back um context deadline exceeded error so with timeout is exactly the same thing as with deadline but it's using a duration instead of um time so to demonstrate the last property that you can have on the context um i will need a function which is taking a context and then it's gonna it's just gonna be a select statement um with um receivers channel so basically this function will return whatever is the first case statement uh fulfilling the condition basically so the first condition is the time two seconds have gone after the calling this function and the second segment will be one second after this function was invoked so if i just quickly invoke that function it's after not after func so if i'm invoking that function with just our background initial context so we should see it should end up after one second which is going to be the first condition met basically after one second you saw the delay as well so um now let's build the third case statement uh with our last uh instant method which is uh dot done basically so dot done is a receiver channel so it's the same tab as time after and if this context done message is received we will just print a log line with boom context is done so here the rerun uh just um the function just re-render that case because that was the first condition met when we invoked the function so the context is not done basically so now let's uh just reach off a little bit our previews uh with timeout context so um let's remove the second case statement as well and let's um modify the first one with three seconds so um let's reshuffle that as well so still a timeout of one second we will sleep for two seconds so then the timeout should be hit so the context two will erode with a context deadline exceeded basically and then we will invoke our function so you see that we ended up on the second case because the third first case with the three seconds has not been met because we just waited for two seconds basically so if you remember at the beginning of the video i was explaining that context can help to control the flow so here we're just gonna derive um a context from context2 the context with the timeout from the previous example i'm just deriving a context with a value so nothing fancy in term of timeout so then context free is just um children from context to so we're gonna invoke uh our function context 2 and context 3 so obviously context 2 will um hit its timeout and let's see what's going on from the child context free point of view right so you can see that uh buff contacts was terminated so now you should have all the basics on the context package so you won't be afraid anymore to see them appearing in different libraries and sdks so to go further on the subject i invite you to see how the different timeout patterns are implemented and different patterns on the control for strategies as well and i also invite you to deep dive and see how famous sdk are implementing or manipulating those contacts so a good example is the aws sdk golang sdk so thanks for watching and happy coding
Info
Channel: Panpito
Views: 7,769
Rating: undefined out of 5
Keywords: tutorial, go, golang, coding, software, how to, learning, software engineering, context, lib, sdk
Id: RH_lcxKMgN4
Channel Id: undefined
Length: 15min 45sec (945 seconds)
Published: Sat Oct 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.