1 00:00:06,580 --> 00:00:10,490 - So our next basic and our last basic channel pattern here 2 00:00:10,490 --> 00:00:12,080 is wait for finished. 3 00:00:12,080 --> 00:00:13,270 And wait for finished is going to show us 4 00:00:13,270 --> 00:00:15,390 how we can signal without data. 5 00:00:15,390 --> 00:00:17,150 Now I just want to make it clear that 6 00:00:17,150 --> 00:00:17,983 what I'm showing you 7 00:00:17,983 --> 00:00:21,030 would really be better served with a wait group. 8 00:00:21,030 --> 00:00:23,340 A wait group began as a way of kind of doing 9 00:00:23,340 --> 00:00:28,080 a signaling without data for that type of orchestration 10 00:00:28,080 --> 00:00:29,527 and would be cleaner here 11 00:00:29,527 --> 00:00:32,450 but I'm going to show you and walk through this code 12 00:00:32,450 --> 00:00:34,990 just so you can see the mechanics. 13 00:00:34,990 --> 00:00:36,100 Again, I'm going to want to stress that. 14 00:00:36,100 --> 00:00:38,460 A wait group would make this code cleaner 15 00:00:38,460 --> 00:00:39,900 but I need to show you the mechanics 16 00:00:39,900 --> 00:00:40,960 so later on when we talk about 17 00:00:40,960 --> 00:00:43,930 cancellation and deadlines with the context package, 18 00:00:43,930 --> 00:00:45,800 it'll give you a little bit more sense. 19 00:00:45,800 --> 00:00:47,860 Now look on line 75. 20 00:00:47,860 --> 00:00:49,600 We're using the make again, 21 00:00:49,600 --> 00:00:52,010 channel empty struct and it's unbuffered. 22 00:00:52,010 --> 00:00:55,060 So we have an unbuffered channel which means guarantees, 23 00:00:55,060 --> 00:00:58,590 except that the data type isn't string here, 24 00:00:58,590 --> 00:01:00,460 it's the empty struct. 25 00:01:00,460 --> 00:01:02,460 And we're going to use the empty struct 26 00:01:02,460 --> 00:01:06,520 to denote from a code readability, signaling without data. 27 00:01:06,520 --> 00:01:09,430 Since the empty struct is about no data, 28 00:01:09,430 --> 00:01:10,263 we're going to use this. 29 00:01:10,263 --> 00:01:11,230 And that basically means that 30 00:01:11,230 --> 00:01:14,060 all we're really going to be doing is closing the channel 31 00:01:14,060 --> 00:01:15,723 and there's no real guarantees with this 32 00:01:15,723 --> 00:01:17,730 as I'm going to show you. 33 00:01:17,730 --> 00:01:21,400 But anytime you see a make chan empty struct, 34 00:01:21,400 --> 00:01:24,300 now we're going to be thinking, signaling without data. 35 00:01:24,300 --> 00:01:25,660 We're going to turn the lights off. 36 00:01:25,660 --> 00:01:28,150 Now, let's go back and look at what we're doing. 37 00:01:28,150 --> 00:01:31,500 We are the manager, here we are 38 00:01:31,500 --> 00:01:34,100 and we're running in our path of execution 39 00:01:34,100 --> 00:01:36,470 and right away we go and we launch that 40 00:01:36,470 --> 00:01:39,220 employee path of execution or go routine. 41 00:01:39,220 --> 00:01:41,170 And once we do that again, 42 00:01:41,170 --> 00:01:44,040 we're asking this go routine to do some work. 43 00:01:44,040 --> 00:01:47,290 So they already know what they're supposed to do. 44 00:01:47,290 --> 00:01:52,290 So now we come back and end up right there on line 83. 45 00:01:53,410 --> 00:01:56,160 Notice again on line 83, we're doing a channel receive. 46 00:01:56,160 --> 00:01:57,300 This is very similar 47 00:01:57,300 --> 00:01:59,860 to what we were just doing before, right? 48 00:01:59,860 --> 00:02:01,780 And here we go on the channel receive, 49 00:02:01,780 --> 00:02:04,590 which means we're now in a blocking call, right? 50 00:02:04,590 --> 00:02:06,270 We're right here blocking 51 00:02:06,270 --> 00:02:10,630 because we're in our channel receive operation. 52 00:02:10,630 --> 00:02:12,030 Now I want you to notice something 53 00:02:12,030 --> 00:02:14,130 about the channel receive operation. 54 00:02:14,130 --> 00:02:18,140 Up until now, all I've done is allowed you to see 55 00:02:18,140 --> 00:02:22,260 that we can receive the value that's being sent. 56 00:02:22,260 --> 00:02:25,520 But when we talk about cancellation and deadlines 57 00:02:25,520 --> 00:02:28,750 or signaling without data, there is no data to receive. 58 00:02:28,750 --> 00:02:30,070 And that's why what you're seeing is 59 00:02:30,070 --> 00:02:32,830 the second form of the channel receive. 60 00:02:32,830 --> 00:02:34,630 You can always get data out of it 61 00:02:34,630 --> 00:02:36,187 if you're signaling with data, 62 00:02:36,187 --> 00:02:39,360 but we can get a second flag, which is a buoyant flag, 63 00:02:39,360 --> 00:02:41,754 that I use WD for, with data. 64 00:02:41,754 --> 00:02:44,380 This is my with data flag 65 00:02:44,380 --> 00:02:46,380 and it's a part of the signaling mechanics. 66 00:02:46,380 --> 00:02:47,213 And what it's telling me is 67 00:02:47,213 --> 00:02:49,930 that if I receive successfully off this channel 68 00:02:49,930 --> 00:02:53,000 and the with data flag is true, then I have data. 69 00:02:53,000 --> 00:02:54,340 We signaled with data. 70 00:02:54,340 --> 00:02:56,900 But if the with data flag is false, 71 00:02:56,900 --> 00:02:59,420 then we receive because the channel is closed, 72 00:02:59,420 --> 00:03:00,990 because the lights are out. 73 00:03:00,990 --> 00:03:03,140 Now I don't really need to store this variable, 74 00:03:03,140 --> 00:03:04,670 I just wanted to display it on the screen 75 00:03:04,670 --> 00:03:06,380 if you decided to run this. 76 00:03:06,380 --> 00:03:08,090 Ideally in these scenarios, 77 00:03:08,090 --> 00:03:11,110 we're probably doing nothing more than this, 78 00:03:11,110 --> 00:03:13,797 which is I'm just going to block on the receive 79 00:03:13,797 --> 00:03:15,719 and I don't care what comes out of it. 80 00:03:15,719 --> 00:03:16,740 But what we're doing, 81 00:03:16,740 --> 00:03:18,340 I'm just showing you the with data flag. 82 00:03:18,340 --> 00:03:20,690 So here we are right, like we talked about here, 83 00:03:20,690 --> 00:03:24,230 I'm blocked right now waiting, waiting. 84 00:03:24,230 --> 00:03:25,510 How long am I waiting? 85 00:03:25,510 --> 00:03:28,030 I don't know, it's unknown because we're signaling 86 00:03:28,030 --> 00:03:29,660 even if it's signaling without data, 87 00:03:29,660 --> 00:03:32,490 we're signaling with guarantees here right? 88 00:03:32,490 --> 00:03:34,920 I have no idea how long we're going to be waiting. 89 00:03:34,920 --> 00:03:36,872 Eventually the work finishes 90 00:03:36,872 --> 00:03:41,520 and this time what happens is once the work finishes, 91 00:03:41,520 --> 00:03:43,926 we don't signal with data, 92 00:03:43,926 --> 00:03:47,733 on line 79, we close the channel. 93 00:03:47,733 --> 00:03:52,570 What we now do here is close the channel. 94 00:03:52,570 --> 00:03:55,150 We're turning the lights off. 95 00:03:55,150 --> 00:03:57,810 Now when we close the channel, 96 00:03:57,810 --> 00:04:00,900 that close is going to happen nano seconds 97 00:04:00,900 --> 00:04:02,850 before the receive. 98 00:04:02,850 --> 00:04:05,460 It's kind of like the send happens before the receive, 99 00:04:05,460 --> 00:04:07,030 this time the close happens. 100 00:04:07,030 --> 00:04:10,600 So the close is going to complete first 101 00:04:10,600 --> 00:04:14,250 and then we're going to be able to move on second. 102 00:04:14,250 --> 00:04:17,220 So that channel receive will immediately unblock 103 00:04:17,220 --> 00:04:19,930 once the close operation is done. 104 00:04:19,930 --> 00:04:23,750 So the close is going to happen before the channel receive 105 00:04:23,750 --> 00:04:25,320 but what we've kind of done is just said 106 00:04:25,320 --> 00:04:27,430 the work is done and you don't need anything 107 00:04:27,430 --> 00:04:29,070 so turn the lights off. 108 00:04:29,070 --> 00:04:32,470 Oh, we've seen the state change, now we get to move on. 109 00:04:32,470 --> 00:04:35,400 This is still better done with wait groups, 110 00:04:35,400 --> 00:04:37,700 and I'd ask us to be using wait groups here. 111 00:04:37,700 --> 00:04:39,560 When we start talking about cancellation and deadlines, 112 00:04:39,560 --> 00:04:42,380 I'm going to show you some examples of that. 113 00:04:42,380 --> 00:04:43,680 We're going to be really using 114 00:04:43,680 --> 00:04:46,260 this idea of a unbuffered channel, 115 00:04:46,260 --> 00:04:48,655 where the data is the empty struct, 116 00:04:48,655 --> 00:04:51,520 where we're really saying here that 117 00:04:51,520 --> 00:04:54,160 what we're going to want to do is signal without data 118 00:04:54,160 --> 00:04:57,020 and we want to be able to do that to many, many go routines. 119 00:04:57,020 --> 00:05:00,830 If I had many, many go routines at one point, 120 00:05:00,830 --> 00:05:04,850 all sitting on this same channel receive, 121 00:05:04,850 --> 00:05:07,470 if I had all of these go routines blocked 122 00:05:07,470 --> 00:05:10,540 on the same channel receive, at the same time, 123 00:05:10,540 --> 00:05:13,850 then basically on the close, all of these, 124 00:05:13,850 --> 00:05:17,370 all of these would then be able to unblock immediately 125 00:05:17,370 --> 00:05:18,570 and keep running. 126 00:05:18,570 --> 00:05:20,690 Okay, so this is our wait for finished. 127 00:05:20,690 --> 00:05:22,840 So we've looked at our wait for task, 128 00:05:22,840 --> 00:05:25,420 which is going to be really like pool related right? 129 00:05:25,420 --> 00:05:26,850 The go routine gets lost, 130 00:05:26,850 --> 00:05:29,870 it's now waiting for us to tell it what to do. 131 00:05:29,870 --> 00:05:32,910 Then we also have, we had the wait for task, 132 00:05:32,910 --> 00:05:34,810 then we're going to have to wait for result. 133 00:05:34,810 --> 00:05:37,950 This is like a fan out pattern or drop pattern, 134 00:05:37,950 --> 00:05:39,450 where we launch these go routines, 135 00:05:39,450 --> 00:05:40,990 they know already what they have to do, 136 00:05:40,990 --> 00:05:43,340 we're waiting for them to reply back with 137 00:05:43,340 --> 00:05:45,180 whatever the result of their work is 138 00:05:45,180 --> 00:05:46,500 and I'm going to have this wait for finished, 139 00:05:46,500 --> 00:05:48,400 for cancellation and deadlines, 140 00:05:48,400 --> 00:05:50,030 where we're going to use this to be able 141 00:05:50,030 --> 00:05:51,757 to tell a go routine, 142 00:05:51,757 --> 00:05:53,780 "hey, stop doing what you're doing," 143 00:05:53,780 --> 00:05:57,300 or be able to like indicate a wide scale, 144 00:05:57,300 --> 00:05:59,200 that maybe we should be shutting down.