Learning Golang: Context package: Cancellations, Deadlines and Request-scoped values

  Рет қаралды 11,537

Mario Carrion

Mario Carrion

Күн бұрын

Welcome! Let's learn and talk about the context package, specifically about how to implement deadlines, cancellation and request-scoped values using the functions defined in the package:
* context.WithDeadline and context.WithTimeout
* context.WithCancel and
* context.WithValues
The code is available here: github.com/MarioCarrion/video...
* Blog: mariocarrion.com/2021/05/31/l...
---
* Previous Episode: "Golang Microservices: Events and Background Jobs using RabbitMQ": • Golang Microservices: ...
* Playlist "Building Microservices in Go/Golang": • Building Microservices...
* Playlist "Golang Tools and Packages": • Golang/Go Tools and Pa...
* Playlist "Testing in Go": • Testing in Golang/Go
00:00 Intro
00:56 What is the context package?
01:19 Context Deadlines
04:06 Cancellation Signals
07:32 Request-scoped values
10:02 Best practices
11:17 Parting words
---
Who am I:
Hello👋🏼! I'm Mario, a Hands-on Software Architect and Lead Backend Engineer with more than 16 years of professional experience building all kinds of software including on-premise Industrial Automation Systems, Linux Accessibility Desktop and Browser Components as well as Distributed Advertising Microservices.
Every week I will share with you different topics I've learned while working for small startups and large companies including the processes I've followed over the years for successfully delivering complex enterprise systems from start to end.
Subscribe if you like Software Development, Software Architecture and Systems Design!
Keep it up. Don't give up!
#golang #microservices #programming
--- Our affiliate links below
Shop our favorite products → www.amazon.com/shop/rubycarrion
Vlogging setup → rubycarrion.com/camerasforvlo...
Get a 30 day FREE trial of Epidemic Sound → www.epidemicsound.com/referra...
Try Amazon Prime 30-Day FREE trial → amzn.to/3s0el1R
I love getting Cash Back and think you will too! Join for free and get $20 when you spend $20 → www.rakuten.com/r/RUBYRA132?e...
Sign up to start using vidIQ to grow your KZfaq channel for FREE → vidiq.com/rubycarrion
--- Our Vlog Channel
/ rubycarrion

Пікірлер: 18
@MarioCarrion
@MarioCarrion 3 жыл бұрын
*Blog* mariocarrion.com/ *Building Microservices in Go* kzfaq.info/sun/PL7yAAGMOat_Fn8sAXIk0WyBfK_sT1pohu *Go Tools and Packages* kzfaq.info/sun/PL7yAAGMOat_HEEOvH99agDs_5g51A0Ls3 *Testing in Go* kzfaq.info/sun/PL7yAAGMOat_HSeW4zF0uRL9EaHadE4ZZq *Keep it up!*
@robertjmccabe
@robertjmccabe Жыл бұрын
I like your examples. They are very clear and to the point.
@hectorclara8894
@hectorclara8894 2 жыл бұрын
Hola Mario, Thanks for making such great videos, they have helped me in my current job with working with golang. I come from JS / Ruby land and typed languages can be quite confusing but I see the appeal. Your videos are great for me personally and I have learned a lot and applied it to my career. It's really great to see someone who has such passion for their work, I hope to be like that one day, since I'm pretty junior, I admire those who can "think quickly" and come up with solutions, I hope to be that kind of person one day.
@MarioCarrion
@MarioCarrion 2 жыл бұрын
Hola Hector! I'm glad you're finding the videos useful. IMO they key is all about practice and understanding there's always a learning curve, you just have to trust the process.
@hectorclara8894
@hectorclara8894 2 жыл бұрын
@@MarioCarrion El processo duele D:
@renatospaka
@renatospaka 2 жыл бұрын
Thank you for this class. Finally I understood what context is and how to work with them. No, it is not dark sorcery...
@MarioCarrion
@MarioCarrion 2 жыл бұрын
Thanks for watching Renato. Take care.
@SirRFI
@SirRFI 3 жыл бұрын
Do you have to use select-case syntax for this, or are there other options? Here's an example I have in mind: func main creates context with deadline, like in the example, then creates anonymous function (which does some job, for example waits 1 second) and calls it with goroutine, then blocks at ctx.Done() (assuming it's blocking operation), waiting for deadline (for example 2 seconds). How can I make the anonymous function mark that the task has been completed? Should I call cancel()? What happens to the "defer cancel()" in such scenario?
@MarioCarrion
@MarioCarrion 3 жыл бұрын
There's no need to use "select" for "context.WithCancel". "WithCancel" acts more like "Whenever the returned CancelFunc is called everything using the derived context will cancelled", so if there are any calls using the derived context then all of them will be cancelled. Regarding the other question, perhaps you may want to explore the package "golang.org/x/sync/errgroup" it allows coordinating goroutines much more easily.
@HasanAYousef
@HasanAYousef 2 жыл бұрын
Great, a side question, what is the theme you are using at your terminal?
@MarioCarrion
@MarioCarrion 2 жыл бұрын
I'm using the default theme in iTerm2, but I'm using starship (starship.rs/) as well, if you mean vim I'm using "NLKNguyen/papercolor-theme"; hopefully that answers the question. Best!
@user-mw4mt2mt1r
@user-mw4mt2mt1r Жыл бұрын
So, basically the cancel() is working as a return statement for a goroutines? Which allows them to call the code ending the goroutine from outside?
@MarioCarrion
@MarioCarrion Жыл бұрын
Yes, although I'd rephrase it a bit: cancel() will tell all goroutines using the same context to "finish"; and it's up to the goroutines to honor that.
@SirRFI
@SirRFI 3 жыл бұрын
I think that the explanation for WithDeadline and WithTimeout in the example is overlapping (if that is proper term) - you showed how it works, but not what is the difference. The documentation says "WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).", and the way I understand is: WithDeadline asks you for absolute timestamp when it should expire. It can be expressed as "it should be done by 2021-05-10" (specific date). It can be also calculated off something, like relatively to current time - "if it's not done in 10 minutes, I am leaving". WithTimeout is abstraction or wrapper for WithDeadline, which covers common scenario of setting the deadline from current time, so you don't have to do "time.Now().Add(duration)" every time by hand - you only say how much time you are giving it (the "if it's not done in 10 minutes, I am leaving" scenario). Is this correct?
@MarioCarrion
@MarioCarrion 3 жыл бұрын
In the diagrams I meant to explain the parameters, but really in the end both of them work the same, and yes, you're right "WithTimeout" adds the value to "time.Now()" and calls "WithDeadline" (see go.googlesource.com/go/+/go1.16.3/src/context/context.go#502), it's sort of like a helper.
@ngoctrung531
@ngoctrung531 2 жыл бұрын
the video image is too poor, you need to fix it more
@MarioCarrion
@MarioCarrion 2 жыл бұрын
I'm not sure what you mean, but perhaps you should change your youtube settings to use 1080p next time. Cheers.
@alejandrofernandez1201
@alejandrofernandez1201 2 жыл бұрын
Amigo sos argentino? tu inglés me suena muy argentino jajaj
Learning Golang: Introduction to Benchmarks
18:19
Mario Carrion
Рет қаралды 3,5 М.
Golang Microservices: Events Streaming using Apache Kafka
12:23
Mario Carrion
Рет қаралды 27 М.
Little girl's dream of a giant teddy bear is about to come true #shorts
00:32
No empty
00:35
Mamasoboliha
Рет қаралды 9 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 6 МЛН
Golang 1.21: What is new?
11:34
Mario Carrion
Рет қаралды 4,1 М.
The Fastest Way To Work With Lists in C#! | .NET Tips 6
0:44
Nick Chapsas
Рет қаралды 141 М.
How the Golang Context Package Works
12:12
Tiago
Рет қаралды 4,5 М.
[Golang] context
15:45
Panpito
Рет қаралды 12 М.
Learn Go context from code and its original blog post
6:45
Bitwise Man
Рет қаралды 6 М.
Golang 1.19: What is new?
7:37
Mario Carrion
Рет қаралды 8 М.
13 - Gin Handler Timeout Middleware
28:38
JacoboCode
Рет қаралды 2 М.
Как удвоить напряжение? #электроника #умножитель
1:00
Hi Dev! – Электроника
Рет қаралды 1,1 МЛН
Kumanda İle Bilgisayarı Yönetmek #shorts
0:29
Osman Kabadayı
Рет қаралды 2,3 МЛН
Опасность фирменной зарядки Apple
0:57
SuperCrastan
Рет қаралды 11 МЛН