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

Поделиться
HTML-код
  • Опубликовано: 12 ноя 2024

Комментарии • 18

  • @MarioCarrion
    @MarioCarrion  3 года назад

    *Blog* mariocarrion.com/
    *Building Microservices in Go* ruclips.net/p/PL7yAAGMOat_Fn8sAXIk0WyBfK_sT1pohu
    *Go Tools and Packages* ruclips.net/p/PL7yAAGMOat_HEEOvH99agDs_5g51A0Ls3
    *Testing in Go* ruclips.net/p/PL7yAAGMOat_HSeW4zF0uRL9EaHadE4ZZq
    *Keep it up!*

  • @hectorclara8894
    @hectorclara8894 3 года назад +1

    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  3 года назад

      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 3 года назад

      @@MarioCarrion El processo duele D:

  • @robertjmccabe
    @robertjmccabe 2 года назад

    I like your examples. They are very clear and to the point.

  • @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 года назад +1

    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.

  • @SirRFI
    @SirRFI 3 года назад +1

    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 года назад +1

      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.

  • @ИгорьТ-х6м
    @ИгорьТ-х6м 2 года назад

    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  2 года назад

      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.

  • @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!

  • @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