A REAL usecase of Golang Go Routines! - Golang Concurrency Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys Sam here today I'm going to be going through a really simple goang go routine concurrency use case that will hopefully help you guys that are new to go and looking to understand some real use cases of go routines in your go code this first of all is an endpoint in an application in that code post application which I've done a series about uh on the channel um and I needed to essentially increment a view count on a post when someone uh goes to a post right but obviously what I don't want to have to do as from a backend perspective is when a user goes to a post I don't want to have to you know get the post firstly which is what this is doing here go and get the post get all the comments I mean at the moment this is just like thrown in to get the comments but eventually this would join together anyway so this this could be sped up anyway but um for now I'm getting the post and then I go and get all the comments I've got two database queries here which I will join to one eventually ignore that for now um so that's a fairly quote unquote like expensive slowish operation right and then obviously the next thing I have to do is then go and increment The View count for this post but what I don't want to have to do from a user's perspective on the front end is I don't want to have to load the post load the comments and then load the um then increment The View count and return that to a user right because that means they've then got a wait for something which they don't care about they don't care that I want to increment a view count so why would they want to wait you know for that to happen right so this is where some really basic go concurrency comes into play right so firstly um just to go over what we got here I have a tasks I have a task weight group um on the service right so this is a service method well technically it's just attached to an API method on this case but picture this is like your service or like your API Etc right this embeds a weight group which if you know about weight groups can go is you can basic you can Implement a counter of tasks and then what you can then you tell go that a task is finished and then further down in your code you'd also have a wait to wait for all your task to finish right so what I have going on here is I spin up a task to say right I've got a new task to add I tell go when it's done using the defer function very simple obviously spinning up the go routine just before that and then I increment The View count in this routine right so what this does is the API endpoint so the request itself isn't waiting for the task to finish but the main thread so the like in main.go where I actually spin up the service so if I jump to uh if I jump to main.go uh should be in here I have this long task weight group right and this is just passed into the API and then I after I start the server I wait for all of the long tasks to finish before then a server can can actually be cancelled right um and I can probably handle that a little bit nicer to make sure it safely everything finishes safely right and with a better Timeout on the context Etc but so if we go over this again now so we know that the service has this long tasks uh variable here which is passed into the API so every long task will finish before an API can shut down but a long task does not have to finish before a request right so spin up a go routine add a task and then go and update the view count so to break down how this will basically work a user will request it on this thread here what will happen is the code will go and get the post go get the comments and then this routine here will be spun up so it'll be on a different like Fred over here technically not actually Freds and go but we all we all know that um so another routine over here this routine doesn't care if this one finishes it's just been spun up so this one can now go ahead and increment The View count and do whatever else it needs to do but this one can then go ahead and actually return the post to the user right and it doesn't have to wait for the view count to be incremented um but obviously we do want to make sure this is finished before a service is stopped Etc and that is why we have a weit group here just to make sure it safely finishes um and that brings me on to context so as you probably know as a go developer and go you pass in a context everywhere right a request has a context um long lived operation should have context passed into it uh so for example this request has a context right so this is the context of this request when this request finishes and a response is turned to a user the context is like cancelled um which we don't want to pass in the request context to any operations in our other routine right we just want to spin up a new context because we know that this this um operation should not wait for the request to finish right it and it shouldn't cancel if it does finish so what we do simply is create a new context and pass that into our method that way it can go ahead finish its operations without worrying about the request context finishing um but yeah I just wanted to make a very quick video it's a short one but it's just a really powerful way of spinning up of doing an operation on an API method especially if you're building an app that you want to just get out there quick and you don't want to worry too much about performance because you may not have the users yet to really care about performance because that's the main thing right you don't really need to care out performance until you actually have users um but just as like a really quick thing you can throw in end points spin up a routine and do background task on it like for example another really good use case of this might be a user might sign up and it will hit your sign up endpoint you don't want to have to wait for the endpoint to respond to send that email to him you can straight away just spin up another routine and then go ahead and um you know notify that user with an email inev routine it doesn't have to finish its request Etc you get the idea um hopefully this video made some sense and it helps you with your goang end points and hopefully this also shows you a realistic approach to go concurrency and some actually useful use cases in where you can use go routines in your go code thanks for watching see you later on
Info
Channel: samvcodes
Views: 6,122
Rating: undefined out of 5
Keywords:
Id: iv5aRdslb5k
Channel Id: undefined
Length: 6min 3sec (363 seconds)
Published: Thu Oct 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.