-
Notifications
You must be signed in to change notification settings - Fork 1
/
2024-06-25-go-patterns.html
748 lines (733 loc) · 67.6 KB
/
2024-06-25-go-patterns.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="This a personal note for the [[https://www.youtube.com/watch?v=IdCbMO0Ey9I][Go lecture]].">
<link rel="alternate"
type="application/rss+xml"
href="https://chenyo.me/rss.xml"
title="RSS feed for https://chenyo.me">
<title>Go patterns</title>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'],['\\(','\\)']]}});
</script>
<meta name="author" content="chenyo">
<meta name="referrer" content="no-referrer">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="assets/style.css" type="text/css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"/>
<link rel="favicon" type="image/x-icon" href="favicon.ico">
<script src="assets/search.js"></script></head>
<body>
<div id="preamble" class="status">
<header>
<h1><a href="https://chenyo.me">Chenyo's org-static-blog</a></h1>
<nav>
<a href="https://chenyo.me">Home</a>
<a href="archive.html">Archive</a>
<a href="tags.html">Tags</a>
<div id="search-container">
<input type="text" id="search-input" placeholder="Search anywhere...">
<i class="fas fa-search search-icon"></i>
</div>
</nav>
</header></div>
<div id="content">
<div class="post-date">25 Jun 2024</div><h1 class="post-title"><a href="https://chenyo.me/2024-06-25-go-patterns.html">Go patterns</a></h1>
<nav id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org9576a2c">1. Concurrency vs Parallelism</a></li>
<li><a href="#org8f67138">2. Use goroutines for states</a>
<ul>
<li><a href="#org7a554b2">2.1. Matching a regex</a></li>
<li><a href="#org4a64cda">2.2. When the state variable cannot be avoided</a></li>
</ul>
</li>
<li><a href="#orgcf04cb6">3. Pattern 1: publish/subscribe server</a>
<ul>
<li><a href="#org67bf6ba">3.1. Options for slow goroutines</a></li>
</ul>
</li>
<li><a href="#orgbfbac1b">4. Pattern 2: work scheduler</a></li>
<li><a href="#orgcce2727">5. Pattern 3: replicated service client</a></li>
<li><a href="#orga057c7a">6. Pattern 4: Protocol multiplexer</a></li>
</ul>
</div>
</nav>
<p>
This a personal note for the <a href="https://www.youtube.com/watch?v=IdCbMO0Ey9I">Russ Cox guest lecture</a>.
</p>
<div id="outline-container-org9576a2c" class="outline-2">
<h2 id="org9576a2c"><span class="section-number-2">1.</span> Concurrency vs Parallelism</h2>
<div class="outline-text-2" id="text-1">
<ul class="org-ul">
<li>Concurrency: write a program to handle lot of things at once
<ul class="org-ul">
<li>not necessarily faster</li>
</ul></li>
<li>Parallelism: the program itself can do a lot of computations at once</li>
</ul>
</div>
</div>
<div id="outline-container-org8f67138" class="outline-2">
<h2 id="org8f67138"><span class="section-number-2">2.</span> Use goroutines for states</h2>
<div class="outline-text-2" id="text-2">
</div>
<div id="outline-container-org7a554b2" class="outline-3">
<h3 id="org7a554b2"><span class="section-number-3">2.1.</span> Matching a regex</h3>
<div class="outline-text-3" id="text-2-1">
<ul class="org-ul">
<li>return if a given string matches a regex: start with <code>"</code>, contains arbitrary escape sequence and ends with <code>"</code></li>
<li><p>
unclear logic: store states in the data
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #dcaeea;">state</span> := <span style="color: #da8548; font-weight: bold;">0</span>
<span class="linenr"> 2: </span><span style="color: #51afef;">for</span> {
<span class="linenr"> 3: </span> <span style="color: #dcaeea;">c</span> := <span style="color: #c678dd;">read</span>()
<span class="linenr"> 4: </span> <span style="color: #51afef;">switch</span> state {
<span class="linenr"> 5: </span> <span style="color: #51afef;">case</span> <span style="color: #da8548; font-weight: bold;">0</span>:
<span class="linenr"> 6: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">first char must be "</span>
<span class="linenr"> 7: </span> <span style="color: #51afef;">if</span> c != <span style="color: #98be65;">'"'</span> {
<span class="linenr"> 8: </span> <span style="color: #51afef;">return</span> <span style="color: #a9a1e1;">false</span>
<span class="linenr"> 9: </span> }
<span class="linenr">10: </span> state = <span style="color: #da8548; font-weight: bold;">1</span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">match the next char</span>
<span class="linenr">11: </span> <span style="color: #51afef;">case</span> <span style="color: #da8548; font-weight: bold;">1</span>:
<span class="linenr">12: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">ending with " matches</span>
<span class="linenr">13: </span> <span style="color: #51afef;">if</span> c == <span style="color: #98be65;">'"'</span> {
<span class="linenr">14: </span> <span style="color: #51afef;">return</span> <span style="color: #a9a1e1;">true</span>
<span class="linenr">15: </span> }
<span class="linenr">16: </span> <span style="color: #51afef;">if</span> c == <span style="color: #98be65;">'\\'</span> {
<span class="linenr">17: </span> state = <span style="color: #da8548; font-weight: bold;">2</span>
<span class="linenr">18: </span> } <span style="color: #51afef;">else</span> {
<span class="linenr">19: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">transition to state 1 to match next char</span>
<span class="linenr">20: </span> state = <span style="color: #da8548; font-weight: bold;">1</span>
<span class="linenr">21: </span> }
<span class="linenr">22: </span> <span style="color: #51afef;">case</span> <span style="color: #da8548; font-weight: bold;">2</span>:
<span class="linenr">23: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">read the char, discard it and</span>
<span class="linenr">24: </span> state = <span style="color: #da8548; font-weight: bold;">1</span>
<span class="linenr">25: </span> }
<span class="linenr">26: </span>}
</pre>
</div></li>
<li><p>
clear logic: store states in the code
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">no variable to store state</span>
<span class="linenr"> 2: </span><span style="color: #51afef;">if</span> <span style="color: #c678dd;">read</span>() != <span style="color: #98be65;">'"'</span> {
<span class="linenr"> 3: </span> <span style="color: #51afef;">return</span> <span style="color: #a9a1e1;">false</span>
<span class="linenr"> 4: </span>}
<span class="linenr"> 5: </span><span style="color: #51afef;">var</span> <span style="color: #dcaeea;">c</span> <span style="color: #ECBE7B;">rune</span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">c is a Unicode, alias to int32</span>
<span class="linenr"> 6: </span><span style="color: #51afef;">for</span> c != <span style="color: #98be65;">'"'</span> {
<span class="linenr"> 7: </span> c = <span style="color: #c678dd;">read</span>()
<span class="linenr"> 8: </span> <span style="color: #51afef;">if</span> c == <span style="color: #98be65;">'\\'</span> {
<span class="linenr"> 9: </span> <span style="color: #c678dd;">read</span>() <span style="color: #5B6268;">// </span><span style="color: #5B6268;">skip the next char</span>
<span class="linenr">10: </span> }
<span class="linenr">11: </span>}
<span class="linenr">12: </span><span style="color: #51afef;">return</span> <span style="color: #a9a1e1;">true</span>
</pre>
</div></li>
</ul>
</div>
</div>
<div id="outline-container-org4a64cda" class="outline-3">
<h3 id="org4a64cda"><span class="section-number-3">2.2.</span> When the state variable cannot be avoided</h3>
<div class="outline-text-3" id="text-2-2">
<ul class="org-ul">
<li><p>
the function needs to return the state
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">quoter</span> <span style="color: #51afef;">struct</span> {
<span class="linenr"> 2: </span> state <span style="color: #ECBE7B;">int</span>
<span class="linenr"> 3: </span>}
<span class="linenr"> 4: </span>
<span class="linenr"> 5: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">q</span> *<span style="color: #ECBE7B;">quoter</span>) <span style="color: #c678dd;">Init</span>() {
<span class="linenr"> 6: </span> r.state = <span style="color: #da8548; font-weight: bold;">0</span>
<span class="linenr"> 7: </span>}
<span class="linenr"> 8: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">proess each char based on current state</span>
<span class="linenr"> 9: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">q</span> *<span style="color: #ECBE7B;">quoter</span>) <span style="color: #c678dd;">Write</span>(<span style="color: #dcaeea;">c</span> <span style="color: #ECBE7B;">rune</span>) <span style="color: #ECBE7B;">Status</span> {
<span class="linenr">10: </span> <span style="color: #51afef;">switch</span> q.state {
<span class="linenr">11: </span> <span style="color: #51afef;">case</span> <span style="color: #da8548; font-weight: bold;">0</span>:
<span class="linenr">12: </span> <span style="color: #51afef;">if</span> c != <span style="color: #98be65;">'"'</span> {
<span class="linenr">13: </span> <span style="color: #51afef;">return</span> BadInput
<span class="linenr">14: </span> }
<span class="linenr">15: </span> q.state = <span style="color: #da8548; font-weight: bold;">1</span>
<span class="linenr">16: </span> <span style="color: #51afef;">case</span> <span style="color: #da8548; font-weight: bold;">1</span>:
<span class="linenr">17: </span> <span style="color: #51afef;">if</span> c == <span style="color: #98be65;">'"'</span> {
<span class="linenr">18: </span> <span style="color: #51afef;">return</span> Success
<span class="linenr">19: </span> }
<span class="linenr">20: </span> <span style="color: #51afef;">if</span> c == <span style="color: #98be65;">'\\'</span> {
<span class="linenr">21: </span> q.state = <span style="color: #da8548; font-weight: bold;">2</span>
<span class="linenr">22: </span> } <span style="color: #51afef;">else</span> {
<span class="linenr">23: </span> q.state = <span style="color: #da8548; font-weight: bold;">1</span>
<span class="linenr">24: </span> }
<span class="linenr">25: </span> <span style="color: #51afef;">case</span> <span style="color: #da8548; font-weight: bold;">2</span>:
<span class="linenr">26: </span> q.state = <span style="color: #da8548; font-weight: bold;">1</span>
<span class="linenr">27: </span> }
<span class="linenr">28: </span> <span style="color: #51afef;">return</span> NeedMoreInput
<span class="linenr">29: </span>}
</pre>
</div></li>
<li><p>
use additional goroutines to hold states
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">quoter</span> <span style="color: #51afef;">struct</span> {
<span class="linenr"> 2: </span> char <span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">rune</span>
<span class="linenr"> 3: </span> status <span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Status</span>
<span class="linenr"> 4: </span>}
<span class="linenr"> 5: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">q</span> *<span style="color: #ECBE7B;">quoter</span>) <span style="color: #c678dd;">Init</span>() {
<span class="linenr"> 6: </span> q.char = <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">rune</span>)
<span class="linenr"> 7: </span> q.status = <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Status</span>)
<span class="linenr"> 8: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">need to make sure why and when the goroutine will exit</span>
<span class="linenr"> 9: </span> <span style="color: #51afef;">go</span> q.<span style="color: #c678dd;">parse</span>()
<span class="linenr">10: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">blocks until it receives an initial status from parse()</span>
<span class="linenr">11: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">to ensure that parse() is ready, i.e., q.status = NeedMoreInput</span>
<span class="linenr">12: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">before Write() is called</span>
<span class="linenr">13: </span> <-q.status
<span class="linenr">14: </span>}
<span class="linenr">15: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">Write sends the next char to q.char, which will be receivecd by parse()</span>
<span class="linenr">16: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">the status is a public state accessible by the user</span>
<span class="linenr">17: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">q</span> *<span style="color: #ECBE7B;">quoter</span>) <span style="color: #c678dd;">Write</span>(<span style="color: #dcaeea;">r</span> <span style="color: #ECBE7B;">rune</span>) <span style="color: #ECBE7B;">Status</span> {
<span class="linenr">18: </span> q.char <- c
<span class="linenr">19: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">wait for the result</span>
<span class="linenr">20: </span> <span style="color: #51afef;">return</span> <-q.status
<span class="linenr">21: </span>}
<span class="linenr">22: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">q</span> *<span style="color: #ECBE7B;">quoteReader</span>) <span style="color: #c678dd;">parse</span>() {
<span class="linenr">23: </span> <span style="color: #51afef;">if</span> q.<span style="color: #c678dd;">read</span>() != <span style="color: #98be65;">'"'</span> {
<span class="linenr">24: </span> q.status <- SyntaxError
<span class="linenr">25: </span> <span style="color: #51afef;">return</span>
<span class="linenr">26: </span> }
<span class="linenr">27: </span> <span style="color: #51afef;">var</span> <span style="color: #dcaeea;">c</span> <span style="color: #ECBE7B;">rune</span>
<span class="linenr">28: </span> <span style="color: #51afef;">for</span> c!= <span style="color: #98be65;">'"'</span> {
<span class="linenr">29: </span> c = q.<span style="color: #c678dd;">read</span>()
<span class="linenr">30: </span> <span style="color: #51afef;">if</span> c == <span style="color: #98be65;">'\\'</span> {
<span class="linenr">31: </span> q.<span style="color: #c678dd;">read</span>()
<span class="linenr">32: </span> }
<span class="linenr">33: </span> }
<span class="linenr">34: </span> q.status <- Done
<span class="linenr">35: </span>}
<span class="linenr">36: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">a helper function used in parse() to return the next char in q.char</span>
<span class="linenr">37: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">q</span> *<span style="color: #ECBE7B;">quoter</span>) <span style="color: #c678dd;">read</span>() <span style="color: #ECBE7B;">int</span> {
<span class="linenr">38: </span> q.status <- NeedMoreInput
<span class="linenr">39: </span> <span style="color: #51afef;">return</span> <- q.char
<span class="linenr">40: </span>}
<span class="linenr">41: </span><span style="color: #51afef;">func</span> <span style="color: #c678dd;">main</span>() {
<span class="linenr">42: </span> <span style="color: #dcaeea;">q</span> := &<span style="color: #ECBE7B;">quoter</span>{}
<span class="linenr">43: </span> q.<span style="color: #c678dd;">Init</span>()
<span class="linenr">44: </span>
<span class="linenr">45: </span> <span style="color: #dcaeea;">input</span> := <span style="color: #98be65;">`"Hello, \"World\""`</span>
<span class="linenr">46: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">_</span>, <span style="color: #dcaeea;">c</span> := <span style="color: #51afef;">range</span> input {
<span class="linenr">47: </span> <span style="color: #dcaeea;">status</span> := q.<span style="color: #c678dd;">Write</span>(c)
<span class="linenr">48: </span> }
<span class="linenr">49: </span>}
</pre>
</div></li>
<li>check goroutine blockage
<ul class="org-ul">
<li><code>Ctrl-\</code> sends <code>SIGQUIT</code></li>
<li>use the HTTP server’s <code>/debug/pprof/goroutine</code> if importing <code>net/http</code></li>
</ul></li>
</ul>
</div>
</div>
</div>
<div id="outline-container-orgcf04cb6" class="outline-2">
<h2 id="orgcf04cb6"><span class="section-number-2">3.</span> Pattern 1: publish/subscribe server</h2>
<div class="outline-text-2" id="text-3">
<ul class="org-ul">
<li>the information goes one way: server -> client</li>
<li>close a channel to signal no new values will be sent</li>
<li><p>
prefer <code>defer</code> when unlocking the mutex
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">Server</span> <span style="color: #51afef;">struct</span> {
<span class="linenr"> 2: </span> mu <span style="color: #ECBE7B;">sync.Mutex</span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">protect sub</span>
<span class="linenr"> 3: </span> sub <span style="color: #51afef;">map</span>[<span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>]<span style="color: #ECBE7B;">bool</span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">whether a channel should be closed</span>
<span class="linenr"> 4: </span>}
<span class="linenr"> 5: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">Init</span>() {
<span class="linenr"> 6: </span> s.sub = <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">map</span>[<span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>]<span style="color: #ECBE7B;">bool</span>)
<span class="linenr"> 7: </span>}
<span class="linenr"> 8: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">publish an event to all subscribed channel</span>
<span class="linenr"> 9: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">Publish</span>(<span style="color: #dcaeea;">e</span> <span style="color: #ECBE7B;">Event</span>) {
<span class="linenr">10: </span> s.mu.<span style="color: #c678dd;">Lock</span>() <span style="color: #5B6268;">// </span><span style="color: #5B6268;">each method could be called by many clients</span>
<span class="linenr">11: </span> <span style="color: #51afef;">defer</span> s.mu.<span style="color: #c678dd;">Unlock</span>()
<span class="linenr">12: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">need mutex here since it needs to read s.sub state</span>
<span class="linenr">13: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">c</span> := <span style="color: #51afef;">range</span> s.sub {
<span class="linenr">14: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">if a goroutine consumes the channel events too slow</span>
<span class="linenr">15: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">then a new event publish has to wait</span>
<span class="linenr">16: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">before it can send to the channel</span>
<span class="linenr">17: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">can add channel buffer to mitigate this</span>
<span class="linenr">18: </span> c <- e
<span class="linenr">19: </span> }
<span class="linenr">20: </span>}
<span class="linenr">21: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">a channel starts to subscribe</span>
<span class="linenr">22: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">Subscribe</span>(<span style="color: #dcaeea;">c</span> <span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>) {
<span class="linenr">23: </span> s.mu.<span style="color: #c678dd;">Lock</span>()
<span class="linenr">24: </span> <span style="color: #51afef;">defer</span> s.mu.<span style="color: #c678dd;">Unlock</span>()
<span class="linenr">25: </span> <span style="color: #51afef;">if</span> s.sub[c] {
<span class="linenr">26: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">the mutex wil also be unlocked with defer</span>
<span class="linenr">27: </span> <span style="color: #c678dd;">panic</span>(<span style="color: #98be65;">"pubsub: already subscribed"</span>) }
<span class="linenr">28: </span> s.sub[c] = <span style="color: #a9a1e1;">true</span>
<span class="linenr">29: </span>}
<span class="linenr">30: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">a channel cancels the subscription</span>
<span class="linenr">31: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">Cancel</span>(<span style="color: #dcaeea;">c</span> <span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>) {
<span class="linenr">32: </span> s.mu.<span style="color: #c678dd;">Lock</span>()
<span class="linenr">33: </span> <span style="color: #51afef;">defer</span> s.mu.<span style="color: #c678dd;">Unlock</span>()
<span class="linenr">34: </span> <span style="color: #51afef;">if</span> <span style="color: #51afef; font-weight: bold;">!</span>s.sub[c] {
<span class="linenr">35: </span> <span style="color: #c678dd;">panic</span>(<span style="color: #98be65;">"pubsub: not subscribed"</span>)
<span class="linenr">36: </span> }
<span class="linenr">37: </span> <span style="color: #c678dd;">close</span>(c)
<span class="linenr">38: </span> <span style="color: #c678dd;">delete</span>(s.sub, c)
<span class="linenr">39: </span>}
</pre>
</div></li>
</ul>
</div>
<div id="outline-container-org67bf6ba" class="outline-3">
<h3 id="org67bf6ba"><span class="section-number-3">3.1.</span> Options for slow goroutines</h3>
<div class="outline-text-3" id="text-3-1">
<ul class="org-ul">
<li>slow down event generation</li>
<li>drop events if it cannot be sent, e.g., <code>os/signal</code>, <code>runtime/pprof</code></li>
<li><p>
queue events, e.g., add a <code>helper</code> between the server and each client, which also separates the concerns
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">func</span> <span style="color: #c678dd;">helper</span>(<span style="color: #dcaeea;">in</span> <-<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Event</span>, <span style="color: #dcaeea;">out</span> <span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>) {
<span class="linenr"> 2: </span> <span style="color: #51afef;">var</span> <span style="color: #dcaeea;">q</span> []<span style="color: #ECBE7B;">Event</span>
<span class="linenr"> 3: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">if the in is closed, flash out the pending events in q</span>
<span class="linenr"> 4: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">and close out</span>
<span class="linenr"> 5: </span> <span style="color: #51afef;">for</span> in != <span style="color: #a9a1e1;">nil</span> || <span style="color: #c678dd;">len</span>(q) > <span style="color: #da8548; font-weight: bold;">0</span> {
<span class="linenr"> 6: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">decide whether and what to send</span>
<span class="linenr"> 7: </span> <span style="color: #51afef;">var</span> <span style="color: #dcaeea;">sendOut</span> <span style="color: #51afef;">chan</span><- <span style="color: #dcaeea;">Event</span>
<span class="linenr"> 8: </span> <span style="color: #51afef;">var</span> <span style="color: #dcaeea;">next</span> <span style="color: #ECBE7B;">Event</span>
<span class="linenr"> 9: </span> <span style="color: #51afef;">if</span> <span style="color: #c678dd;">len</span>(q) > <span style="color: #da8548; font-weight: bold;">0</span> {
<span class="linenr">10: </span> sendOut = out
<span class="linenr">11: </span> next = q[<span style="color: #da8548; font-weight: bold;">0</span>]
<span class="linenr">12: </span> }
<span class="linenr">13: </span> <span style="color: #51afef;">select</span> {
<span class="linenr">14: </span> <span style="color: #51afef;">case</span> <span style="color: #dcaeea;">e</span>, <span style="color: #dcaeea;">ok</span> := <-in: <span style="color: #5B6268;">// </span><span style="color: #5B6268;">never reaches here after in = nil</span>
<span class="linenr">15: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">ok tells whether in is closed</span>
<span class="linenr">16: </span> <span style="color: #51afef;">if</span> <span style="color: #51afef; font-weight: bold;">!</span>ok {
<span class="linenr">17: </span> in = <span style="color: #a9a1e1;">nil</span>
<span class="linenr">18: </span> <span style="color: #51afef;">break</span>
<span class="linenr">19: </span> }
<span class="linenr">20: </span> q = <span style="color: #c678dd;">append</span>(q, e)
<span class="linenr">21: </span> <span style="color: #51afef;">case</span> sendOut <- next: <span style="color: #5B6268;">// </span><span style="color: #5B6268;">if len(q) == 0, sendOut = nil</span>
<span class="linenr">22: </span> q = q[<span style="color: #da8548; font-weight: bold;">1</span>:]
<span class="linenr">23: </span> }
<span class="linenr">24: </span> }
<span class="linenr">25: </span> <span style="color: #c678dd;">close</span>(out)
<span class="linenr">26: </span>}
</pre>
</div></li>
<li><p>
convert mutexes into goroutines, not suitable for Raft where state transition is complex
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">Server</span> <span style="color: #51afef;">struct</span> {
<span class="linenr"> 2: </span> publish <span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Event</span>
<span class="linenr"> 3: </span> subscribe <span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">subReq</span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">a channel to queue unhandled subscription</span>
<span class="linenr"> 4: </span> cancel <span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">subReq</span>
<span class="linenr"> 5: </span>}
<span class="linenr"> 6: </span><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">subReq</span> <span style="color: #51afef;">struct</span> {
<span class="linenr"> 7: </span> c <span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>
<span class="linenr"> 8: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">a signal of whether an operation succeeds</span>
<span class="linenr"> 9: </span> ok <span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">bool</span>
<span class="linenr">10: </span>}
<span class="linenr">11: </span>
<span class="linenr">12: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">Init</span>() {
<span class="linenr">13: </span> s.publish = <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Event</span>)
<span class="linenr">14: </span> s.subscribe = <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">subReq</span>)
<span class="linenr">15: </span> s.cancel = <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">subReq</span>)
<span class="linenr">16: </span> <span style="color: #51afef;">go</span> s.<span style="color: #c678dd;">loop</span>()
<span class="linenr">17: </span>}
<span class="linenr">18: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">Publish</span>(<span style="color: #dcaeea;">e</span> <span style="color: #ECBE7B;">Event</span>) {
<span class="linenr">19: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">no mutex is required here</span>
<span class="linenr">20: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">as it does not read state</span>
<span class="linenr">21: </span> s.publish <- e
<span class="linenr">22: </span>}
<span class="linenr">23: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">Subscribe</span>(<span style="color: #dcaeea;">c</span> <span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>) {
<span class="linenr">24: </span> <span style="color: #dcaeea;">r</span> := <span style="color: #ECBE7B;">subReq</span>{<span style="color: #a9a1e1;">c</span>: c, <span style="color: #a9a1e1;">ok</span>: <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">bool</span>)}
<span class="linenr">25: </span> s.subscribe <- r
<span class="linenr">26: </span> <span style="color: #51afef;">if</span> <span style="color: #51afef; font-weight: bold;">!</span><-r.ok { <span style="color: #5B6268;">// </span><span style="color: #5B6268;">wait for loop() handle result</span>
<span class="linenr">27: </span> <span style="color: #c678dd;">panic</span>(<span style="color: #98be65;">"pubsub: already subscribed"</span>)
<span class="linenr">28: </span> }
<span class="linenr">29: </span>}
<span class="linenr">30: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">Cancel</span>(<span style="color: #dcaeea;">c</span> <span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>) {
<span class="linenr">31: </span> <span style="color: #dcaeea;">r</span> := <span style="color: #ECBE7B;">subReq</span>{<span style="color: #a9a1e1;">c</span>: c, <span style="color: #a9a1e1;">ok</span>: <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">bool</span>)}
<span class="linenr">32: </span> s.cancel <- r
<span class="linenr">33: </span> <span style="color: #51afef;">if</span> <span style="color: #51afef; font-weight: bold;">!</span><-r.ok {
<span class="linenr">34: </span> <span style="color: #c678dd;">panic</span>(<span style="color: #98be65;">"pubusb: not subscribed"</span>)
<span class="linenr">35: </span> }
<span class="linenr">36: </span>}
<span class="linenr">37: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">s</span> *<span style="color: #ECBE7B;">Server</span>) <span style="color: #c678dd;">loop</span>() {
<span class="linenr">38: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">now sub is a local variable, no lock is needed</span>
<span class="linenr">39: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">sub maps from a subscribed channel to a helper channel</span>
<span class="linenr">40: </span> <span style="color: #dcaeea;">sub</span> := <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">map</span>[<span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>]<span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Event</span>)
<span class="linenr">41: </span> <span style="color: #51afef;">for</span> {
<span class="linenr">42: </span> <span style="color: #51afef;">select</span> {
<span class="linenr">43: </span> <span style="color: #51afef;">case</span> <span style="color: #dcaeea;">e</span> := <-s.publish:
<span class="linenr">44: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">_</span>, <span style="color: #dcaeea;">h</span> := <span style="color: #51afef;">range</span> sub {
<span class="linenr">45: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">the event is published to a helper channel</span>
<span class="linenr">46: </span> h <- e
<span class="linenr">47: </span> }
<span class="linenr">48: </span> <span style="color: #51afef;">case</span> <span style="color: #dcaeea;">r</span> := <-s.subscribe:
<span class="linenr">49: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">the helper channel exists</span>
<span class="linenr">50: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">meaning the subscriber has been handled before</span>
<span class="linenr">51: </span> <span style="color: #51afef;">if</span> sub[r.c] != <span style="color: #a9a1e1;">nil</span> {
<span class="linenr">52: </span> r.ok <- <span style="color: #a9a1e1;">false</span>
<span class="linenr">53: </span> <span style="color: #51afef;">break</span>
<span class="linenr">54: </span> }
<span class="linenr">55: </span> h = <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Event</span>)
<span class="linenr">56: </span> <span style="color: #51afef;">go</span> <span style="color: #c678dd;">helper</span>(h, r.c)
<span class="linenr">57: </span> sub[r.c] = h
<span class="linenr">58: </span> r.ok <- <span style="color: #a9a1e1;">true</span>
<span class="linenr">59: </span> <span style="color: #51afef;">case</span> <span style="color: #dcaeea;">c</span> := <-s.cancel:
<span class="linenr">60: </span> <span style="color: #51afef;">if</span> <span style="color: #51afef; font-weight: bold;">!</span>sub[r.c] == <span style="color: #a9a1e1;">nil</span>{
<span class="linenr">61: </span> r.ok <- <span style="color: #a9a1e1;">false</span>
<span class="linenr">62: </span> <span style="color: #51afef;">break</span>
<span class="linenr">63: </span> }
<span class="linenr">64: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">close the helper channel</span>
<span class="linenr">65: </span> <span style="color: #c678dd;">close</span>(sub[r.c])
<span class="linenr">66: </span> <span style="color: #c678dd;">delete</span>(sub, r.c)
<span class="linenr">67: </span> r.ok <- <span style="color: #a9a1e1;">true</span>
<span class="linenr">68: </span> }
<span class="linenr">69: </span> }
<span class="linenr">70: </span>}
</pre>
</div></li>
</ul>
</div>
</div>
</div>
<div id="outline-container-orgbfbac1b" class="outline-2">
<h2 id="orgbfbac1b"><span class="section-number-2">4.</span> Pattern 2: work scheduler</h2>
<div class="outline-text-2" id="text-4">
<ul class="org-ul">
<li><p>
\(M\) tasks assigned to \(N\) servers/workers, \( M >> N\).
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">func</span> <span style="color: #c678dd;">Schedule</span>(<span style="color: #dcaeea;">servers</span> []<span style="color: #ECBE7B;">string</span>, <span style="color: #dcaeea;">numTask</span> <span style="color: #ECBE7B;">int</span>,
<span class="linenr"> 2: </span> <span style="color: #dcaeea;">call</span> <span style="color: #51afef;">func</span>(<span style="color: #dcaeea;">srv</span> <span style="color: #ECBE7B;">string</span>, <span style="color: #dcaeea;">task</span> <span style="color: #ECBE7B;">int</span>)) {
<span class="linenr"> 3: </span>
<span class="linenr"> 4: </span> <span style="color: #dcaeea;">idle</span> := <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">string</span>, <span style="color: #c678dd;">len</span>(servers))
<span class="linenr"> 5: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">initialize a channel of idle servers</span>
<span class="linenr"> 6: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">_</span>, <span style="color: #dcaeea;">srv</span> := <span style="color: #51afef;">range</span> servers {
<span class="linenr"> 7: </span> idle <- srv
<span class="linenr"> 8: </span> }
<span class="linenr"> 9: </span>
<span class="linenr">10: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">task</span> := <span style="color: #da8548; font-weight: bold;">0</span>, task < numTask; task++ {
<span class="linenr">11: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">if using task in the for loop rather than a local task,</span>
<span class="linenr">12: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">there is a race: the loop goes on before the goroutinue starts,</span>
<span class="linenr">13: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">so that some tasks are skipped.</span>
<span class="linenr">14: </span> <span style="color: #dcaeea;">task</span> := task
<span class="linenr">15: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">if moving srv := <- idle inside goroutine</span>
<span class="linenr">16: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">a lot of goroutines are created simoutaneously and hung</span>
<span class="linenr">17: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">due to non-idle server</span>
<span class="linenr">18: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">leaving it outside so that a goroutine is only created when</span>
<span class="linenr">19: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">there is an idle server (but it slows down the main loop)</span>
<span class="linenr">20: </span> <span style="color: #dcaeea;">srv</span> := <-idle
<span class="linenr">21: </span> <span style="color: #51afef;">go</span> <span style="color: #51afef;">func</span>() {
<span class="linenr">22: </span> <span style="color: #c678dd;">call</span>(srv, task) <span style="color: #5B6268;">// </span><span style="color: #5B6268;">server does the task</span>
<span class="linenr">23: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">serve finishes the task and becomes idle again</span>
<span class="linenr">24: </span> idle <- srv
<span class="linenr">25: </span> }()
<span class="linenr">26: </span> }
<span class="linenr">27: </span>
<span class="linenr">28: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">determine when all tasks are done / all servers are idle</span>
<span class="linenr">29: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">this is used to prevent early exit when all tasks have been assigned</span>
<span class="linenr">30: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">but the last servers have not finished</span>
<span class="linenr">31: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">i</span> :=<span style="color: #da8548; font-weight: bold;">0</span>; i < <span style="color: #c678dd;">len</span>(servers); i++ {
<span class="linenr">32: </span> <-idle
<span class="linenr">33: </span> }
<span class="linenr">34: </span>}
</pre>
</div></li>
<li><p>
Optimization for the above code: while the task loop creates goroutines \(M\) times, actually there are only at most \(N\) active goroutines at any time.
</p>
<ul class="org-ul">
<li>Better to spin off a goroutine for each server.</li>
<li>The number of servers can be dynamic.</li>
</ul>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">func</span> <span style="color: #c678dd;">Schedule</span>(<span style="color: #dcaeea;">servers</span> <span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">string</span>, <span style="color: #dcaeea;">numTask</span> <span style="color: #ECBE7B;">int</span>,
<span class="linenr"> 2: </span> <span style="color: #dcaeea;">call</span> <span style="color: #51afef;">func</span>(<span style="color: #dcaeea;">srv</span> <span style="color: #ECBE7B;">string</span>, <span style="color: #dcaeea;">task</span> <span style="color: #ECBE7B;">int</span>)) {
<span class="linenr"> 3: </span>
<span class="linenr"> 4: </span> <span style="color: #dcaeea;">work</span> := <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">int</span>) <span style="color: #5B6268;">// </span><span style="color: #5B6268;">a queue of all works yet to be done</span>
<span class="linenr"> 5: </span> <span style="color: #dcaeea;">done</span> := <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">bool</span>) <span style="color: #5B6268;">// </span><span style="color: #5B6268;">a queue of all done tasks</span>
<span class="linenr"> 6: </span> <span style="color: #dcaeea;">exit</span> := <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">bool</span>) <span style="color: #5B6268;">// </span><span style="color: #5B6268;">signal when should not pull new servers</span>
<span class="linenr"> 7: </span>
<span class="linenr"> 8: </span> <span style="color: #dcaeea;">runTasks</span> := <span style="color: #51afef;">func</span>(<span style="color: #dcaeea;">srv</span> <span style="color: #ECBE7B;">string</span>) {
<span class="linenr"> 9: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">keep polling until work is closed</span>
<span class="linenr">10: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">task</span> := <span style="color: #51afef;">range</span> work {
<span class="linenr">11: </span> <span style="color: #51afef;">if</span> <span style="color: #c678dd;">call</span>(srv, task) {
<span class="linenr">12: </span> done <- <span style="color: #a9a1e1;">true</span>
<span class="linenr">13: </span> } <span style="color: #51afef;">else</span> {
<span class="linenr">14: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">repush the task if it failed</span>
<span class="linenr">15: </span> work <- task
<span class="linenr">16: </span> }
<span class="linenr">17: </span> }
<span class="linenr">18: </span> }
<span class="linenr">19: </span>
<span class="linenr">20: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">use a goroutine to avoid hanging when</span>
<span class="linenr">21: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">no server is available</span>
<span class="linenr">22: </span> <span style="color: #51afef;">go</span> <span style="color: #51afef;">func</span>() {
<span class="linenr">23: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">_</span>, <span style="color: #dcaeea;">srv</span> := <span style="color: #51afef;">range</span> servers {
<span class="linenr">24: </span> <span style="color: #51afef;">for</span> {
<span class="linenr">25: </span> <span style="color: #51afef;">select</span> {
<span class="linenr">26: </span> <span style="color: #51afef;">case</span> <span style="color: #dcaeea;">src</span> := <-servers:
<span class="linenr">27: </span> <span style="color: #51afef;">go</span> <span style="color: #c678dd;">runTasks</span>(srv)
<span class="linenr">28: </span> <span style="color: #51afef;">case</span> <-exit:
<span class="linenr">29: </span> <span style="color: #51afef;">return</span>
<span class="linenr">30: </span> }
<span class="linenr">31: </span> }
<span class="linenr">32: </span> }
<span class="linenr">33: </span> }()
<span class="linenr">34: </span>
<span class="linenr">35: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">The following code has a deadlock!</span>
<span class="linenr">36: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">In the runTasks, the server pushes to done channel when a task is done.</span>
<span class="linenr">37: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">However, the done channel is only pulled when the main routine has</span>
<span class="linenr">38: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">pushed all tasks and close the work channel.</span>
<span class="linenr">39: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">Therefore any server hangs when trying push the second done work.</span>
<span class="linenr">40: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">for taks := 0; task < numTask; task++ {</span>
<span class="linenr">41: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">work <- task</span>
<span class="linenr">42: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">}</span>
<span class="linenr">43: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">// signal no more task so that servers know</span>
<span class="linenr">44: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">// when to termiante</span>
<span class="linenr">45: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">close(work)</span>
<span class="linenr">46: </span>
<span class="linenr">47: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">// wait until all tasks are done</span>
<span class="linenr">48: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">for i := 0; i < numTask; i++ {</span>
<span class="linenr">49: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;"><-done</span>
<span class="linenr">50: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">}</span>
<span class="linenr">51: </span>
<span class="linenr">52: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">fix 1: one can switch between work and donw channel</span>
<span class="linenr">53: </span> <span style="color: #dcaeea;">i</span> := <span style="color: #da8548; font-weight: bold;">0</span>
<span class="linenr">54: </span><span style="color: #a9a1e1;">WorkLoop</span>:
<span class="linenr">55: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">task</span> := <span style="color: #da8548; font-weight: bold;">0</span>; task < numTask; task++ {
<span class="linenr">56: </span> <span style="color: #51afef;">for</span> {
<span class="linenr">57: </span> <span style="color: #51afef;">select</span> {
<span class="linenr">58: </span> <span style="color: #51afef;">case</span> work <- task:
<span class="linenr">59: </span> <span style="color: #51afef;">continue</span> <span style="color: #a9a1e1;">WorkLoop</span>
<span class="linenr">60: </span> <span style="color: #51afef;">case</span> <-done:
<span class="linenr">61: </span> i++
<span class="linenr">62: </span> }
<span class="linenr">63: </span> }
<span class="linenr">64: </span> }
<span class="linenr">65: </span>
<span class="linenr">66: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">wait for the last assigned tasks to be done</span>
<span class="linenr">67: </span> <span style="color: #51afef;">for</span> ; i < numTask; i++ {
<span class="linenr">68: </span> <-done
<span class="linenr">69: </span> }
<span class="linenr">70: </span>
<span class="linenr">71: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">only close work channel in the end,</span>
<span class="linenr">72: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">in case some tasks failed and need to be redo</span>
<span class="linenr">73: </span> <span style="color: #c678dd;">close</span>(work)
<span class="linenr">74: </span> exit <- <span style="color: #a9a1e1;">true</span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">stop pulling new servers</span>
<span class="linenr">75: </span>
<span class="linenr">76: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">fix 2: move the work assignment to a separate go routine</span>
<span class="linenr">77: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">go func() {</span>
<span class="linenr">78: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">for task := ; task < numTask; task++ {</span>
<span class="linenr">79: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">work <- task</span>
<span class="linenr">80: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">}</span>
<span class="linenr">81: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">close(work)</span>
<span class="linenr">82: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">}()</span>
<span class="linenr">83: </span>
<span class="linenr">84: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">fix 3: increase buffer for the work channel</span>
<span class="linenr">85: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">work := make(chan int, numTask)</span>
<span class="linenr">86: </span>}
</pre>
</div></li>
</ul>
</div>
</div>
<div id="outline-container-orgcce2727" class="outline-2">
<h2 id="orgcce2727"><span class="section-number-2">5.</span> Pattern 3: replicated service client</h2>
<div class="outline-text-2" id="text-5">
<ul class="org-ul">
<li><p>
A client replicates its requests to multiple servers, waits for the first reply and changes its preferred server.
</p>
<div class="org-src-container">
<pre class="src src-go"><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">c</span> *<span style="color: #ECBE7B;">Client</span>) <span style="color: #c678dd;">Call</span>(<span style="color: #dcaeea;">args</span> <span style="color: #ECBE7B;">Args</span>) <span style="color: #ECBE7B;">Reply</span> {
<span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">result</span> <span style="color: #51afef;">struct</span> {
serverID <span style="color: #ECBE7B;">int</span>
reply <span style="color: #ECBE7B;">Reply</span>
}
<span style="color: #51afef;">const</span> <span style="color: #a9a1e1;">timeout</span> = <span style="color: #da8548; font-weight: bold;">1</span> * time.Second
<span style="color: #dcaeea;">t</span> := time.<span style="color: #c678dd;">NewTimer</span>(timeout)
<span style="color: #51afef;">defer</span> t.<span style="color: #c678dd;">Stop</span>()
<span style="color: #5B6268;">// </span><span style="color: #5B6268;">a channel for all servers to send reply</span>
<span style="color: #5B6268;">// </span><span style="color: #5B6268;">so that even if the client has received a reply</span>
<span style="color: #5B6268;">// </span><span style="color: #5B6268;">other later replies don't hang</span>
<span style="color: #dcaeea;">done</span> := <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">result</span>, <span style="color: #c678dd;">len</span>(c.servers))
c.mu.<span style="color: #c678dd;">Lock</span>()
<span style="color: #dcaeea;">prefer</span> := c.prefer
c.mu.<span style="color: #c678dd;">Unlock</span>()
<span style="color: #51afef;">var</span> <span style="color: #dcaeea;">r</span> <span style="color: #ECBE7B;">result</span>
<span style="color: #51afef;">for</span> <span style="color: #dcaeea;">off</span> := <span style="color: #da8548; font-weight: bold;">0</span>; off < <span style="color: #c678dd;">len</span>(c.servers); off++ {
<span style="color: #5B6268;">// </span><span style="color: #5B6268;">start from the preferred server</span>
<span style="color: #dcaeea;">id</span> := (prefer + off) % <span style="color: #c678dd;">len</span>(c.servers)
<span style="color: #51afef;">go</span> <span style="color: #51afef;">func</span>() {
done <- <span style="color: #ECBE7B;">result</span>{id, c.<span style="color: #c678dd;">callOne</span>(c.servers[id], arfs)}
}()
<span style="color: #5B6268;">// </span><span style="color: #5B6268;">now wait for either a done signal or a timeout</span>
<span style="color: #5B6268;">// </span><span style="color: #5B6268;">if it is done, don't send to other servers</span>
<span style="color: #5B6268;">// </span><span style="color: #5B6268;">otherwise, reset the timer and sends to the next server</span>
<span style="color: #51afef;">select</span> {
<span style="color: #51afef;">case</span> r = <-done:
<span style="color: #51afef;">goto</span> <span style="color: #a9a1e1;">Done</span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">use a goto if it makes code clear</span>
<span style="color: #51afef;">case</span> <-t.C:
<span style="color: #5B6268;">// </span><span style="color: #5B6268;">timeout</span>
t.<span style="color: #c678dd;">Reset</span>(timeout)
}
}
r = <-done <span style="color: #5B6268;">// </span><span style="color: #5B6268;">wait for the first reply even if it is a RPC timeout</span>
<span style="color: #a9a1e1;">Done</span>:
c.mu.<span style="color: #c678dd;">Lock</span>()
c.prefer = r.serverID <span style="color: #5B6268;">// </span><span style="color: #5B6268;">update preference</span>
c.mu.<span style="color: #c678dd;">Unlock</span>()
<span style="color: #51afef;">return</span> r.reply
}
</pre>
</div></li>
</ul>
</div>
</div>
<div id="outline-container-orga057c7a" class="outline-2">
<h2 id="orga057c7a"><span class="section-number-2">6.</span> Pattern 4: Protocol multiplexer</h2>
<div class="outline-text-2" id="text-6">
<ul class="org-ul">
<li><p>
A multiplexer sits in front of a service and forward messages between multiple clients and the service, e.g., an RPC.
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">ProtocolMux</span> <span style="color: #51afef;">interface</span> {
<span class="linenr"> 2: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">A mux is binded to a specific service</span>
<span class="linenr"> 3: </span> <span style="color: #c678dd;">Init</span>(<span style="color: #ECBE7B;">Service</span>)
<span class="linenr"> 4: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">A client uses this method to send message to the service</span>
<span class="linenr"> 5: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">and wait for the service reply</span>
<span class="linenr"> 6: </span> <span style="color: #c678dd;">Call</span>(<span style="color: #ECBE7B;">Msg</span>) <span style="color: #ECBE7B;">Msg</span>
<span class="linenr"> 7: </span>}
<span class="linenr"> 8: </span>
<span class="linenr"> 9: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">Methods that a service exposes to let a mux use</span>
<span class="linenr">10: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">Underlining messgae processing are in the implementation</span>
<span class="linenr">11: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">of the actual service struct</span>
<span class="linenr">12: </span><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">Service</span> <span style="color: #51afef;">interface</span> {
<span class="linenr">13: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">A tag is a muxing identifier in the request or reply message,</span>
<span class="linenr">14: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">e.g., which client channel to send the reply</span>
<span class="linenr">15: </span> <span style="color: #c678dd;">ReadTag</span>(<span style="color: #ECBE7B;">Msg</span>) <span style="color: #ECBE7B;">int64</span>
<span class="linenr">16: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">Send a request message to the service</span>
<span class="linenr">17: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">multiple sends cannot be called concurrently</span>
<span class="linenr">18: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">probably due to only a single channel between</span>
<span class="linenr">19: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">mux and the service (serialization)</span>
<span class="linenr">20: </span> <span style="color: #c678dd;">Send</span>(<span style="color: #ECBE7B;">Msg</span>)
<span class="linenr">21: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">Waits and return the reply message,</span>
<span class="linenr">22: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">multiple recvs cannot be called concurrently</span>
<span class="linenr">23: </span> <span style="color: #c678dd;">Recv</span>() <span style="color: #ECBE7B;">Msg</span>
<span class="linenr">24: </span>}
</pre>
</div></li>
<li><p>
The mux maintains a channel to queue unsent requests and a channel to queue unsent replies.
</p>
<div class="org-src-container">
<pre class="src src-go"><span class="linenr"> 1: </span><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">Mux</span> <span style="color: #51afef;">struct</span> {
<span class="linenr"> 2: </span> srv <span style="color: #ECBE7B;">Service</span>
<span class="linenr"> 3: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">stores unsent requests</span>
<span class="linenr"> 4: </span> send <span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Msg</span>
<span class="linenr"> 5: </span> mu <span style="color: #ECBE7B;">sync.Mutex</span>
<span class="linenr"> 6: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">maps channel tag to channel</span>
<span class="linenr"> 7: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">whose replies have not been sent out</span>
<span class="linenr"> 8: </span> pending <span style="color: #51afef;">map</span>[<span style="color: #ECBE7B;">int64</span>]<span style="color: #51afef;">chan</span><- <span style="color: #ECBE7B;">Msg</span>
<span class="linenr"> 9: </span>}
<span class="linenr">10: </span>
<span class="linenr">11: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">m</span> *<span style="color: #ECBE7B;">Mux</span>) <span style="color: #c678dd;">Init</span>(<span style="color: #dcaeea;">srv</span> <span style="color: #ECBE7B;">Service</span>) {
<span class="linenr">12: </span> m.srv = srv
<span class="linenr">13: </span> m.pending = <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">map</span>[<span style="color: #ECBE7B;">int64</span>]<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Msg</span>)
<span class="linenr">14: </span> <span style="color: #51afef;">go</span> m.<span style="color: #c678dd;">sendLoop</span>()
<span class="linenr">15: </span> <span style="color: #51afef;">go</span> m.<span style="color: #c678dd;">recvLoop</span>()
<span class="linenr">16: </span>}
<span class="linenr">17: </span>
<span class="linenr">18: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">sending out queued requests</span>
<span class="linenr">19: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">m</span> *<span style="color: #ECBE7B;">Mux</span>) <span style="color: #ECBE7B;">sendLoop</span> {
<span class="linenr">20: </span> <span style="color: #51afef;">for</span> <span style="color: #dcaeea;">args</span> := <span style="color: #51afef;">range</span> m.send {
<span class="linenr">21: </span> m.srv.<span style="color: #c678dd;">Send</span>(args)
<span class="linenr">22: </span> }
<span class="linenr">23: </span>}
<span class="linenr">24: </span>
<span class="linenr">25: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">m</span> *<span style="color: #ECBE7B;">Mux</span>) <span style="color: #c678dd;">recvLoop</span>() {
<span class="linenr">26: </span> <span style="color: #51afef;">for</span> {
<span class="linenr">27: </span> <span style="color: #dcaeea;">reply</span> := m.srv.<span style="color: #c678dd;">Recv</span>()
<span class="linenr">28: </span> <span style="color: #dcaeea;">tag</span> := m.srv.<span style="color: #c678dd;">ReadTag</span>(reply)
<span class="linenr">29: </span> m.mu.<span style="color: #c678dd;">Lock</span>()
<span class="linenr">30: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">get the reply channel</span>
<span class="linenr">31: </span> <span style="color: #dcaeea;">done</span> := m.pending[tag]
<span class="linenr">32: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">clear the channel since the message loop</span>
<span class="linenr">33: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">is complete</span>
<span class="linenr">34: </span> <span style="color: #c678dd;">delete</span>(m.pending, tag)
<span class="linenr">35: </span> m.mu.<span style="color: #c678dd;">Unlock</span>()
<span class="linenr">36: </span>
<span class="linenr">37: </span> <span style="color: #51afef;">if</span> done == <span style="color: #a9a1e1;">nil</span> {
<span class="linenr">38: </span> <span style="color: #c678dd;">panic</span>(<span style="color: #98be65;">"unexpected reply"</span>)
<span class="linenr">39: </span> }
<span class="linenr">40: </span> done <- reply
<span class="linenr">41: </span> }
<span class="linenr">42: </span>
<span class="linenr">43: </span>}
<span class="linenr">44: </span>
<span class="linenr">45: </span><span style="color: #5B6268;">// </span><span style="color: #5B6268;">Clients call this method concurrently</span>
<span class="linenr">46: </span><span style="color: #51afef;">func</span> (<span style="color: #dcaeea;">m</span> *<span style="color: #ECBE7B;">Mux</span>) <span style="color: #c678dd;">Call</span>(<span style="color: #dcaeea;">args</span> <span style="color: #ECBE7B;">Msg</span>) (<span style="color: #dcaeea;">reply</span> <span style="color: #ECBE7B;">Msg</span>) {
<span class="linenr">47: </span> <span style="color: #dcaeea;">tag</span> := m.srv.<span style="color: #c678dd;">ReadTag</span>(args)
<span class="linenr">48: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">to record which message should reply</span>
<span class="linenr">49: </span> <span style="color: #5B6268;">// </span><span style="color: #5B6268;">to which client</span>
<span class="linenr">50: </span> <span style="color: #dcaeea;">done</span> := <span style="color: #c678dd;">make</span>(<span style="color: #51afef;">chan</span> <span style="color: #ECBE7B;">Msg</span>, <span style="color: #da8548; font-weight: bold;">1</span>)
<span class="linenr">51: </span> m.mu.<span style="color: #c678dd;">Lock</span>()
<span class="linenr">52: </span> <span style="color: #51afef;">if</span> m.pending[tag] != <span style="color: #a9a1e1;">nil</span> {
<span class="linenr">53: </span> m.mu.<span style="color: #c678dd;">Unlock</span>()
<span class="linenr">54: </span> <span style="color: #c678dd;">panic</span>(<span style="color: #98be65;">"duplicate request"</span>)
<span class="linenr">55: </span> }
<span class="linenr">56: </span> m.pending[tag] = done
<span class="linenr">57: </span> m.mu.<span style="color: #c678dd;">Unlock</span>()
<span class="linenr">58: </span> m.send <- args
<span class="linenr">59: </span> <span style="color: #51afef;">return</span> <-done <span style="color: #5B6268;">// </span><span style="color: #5B6268;">hang until a reply is received</span>
<span class="linenr">60: </span>}
</pre>
</div></li>
</ul>
</div>
</div>
<div class="taglist"><a href="https://chenyo.me/tags.html">Tags</a>: <a href="https://chenyo.me/tag-go.html">go</a> <a href="https://chenyo.me/tag-design-pattern.html">design-pattern</a> <a href="https://chenyo.me/tag-study.html">study</a> </div></div>
<div id="postamble" class="status"><div id="search-results"></div>
<footer>
<div class="footer-content">
<div class="footer-left">
<p>© 2024 chenyo. Some rights reserved.</p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">
<img alt="Creative Commons License" style="border-width: 0"
src="https://i.creativecommons.org/l/by-nc/4.0/88x31.png"/>
</a>
</div>
<div class="social-links">
<a href="https://t.me/chenyo17" target="_blank" rel="noopener noreferrer">
<i class="fab fa-telegram"></i>
</a>
<a href="https://github.com/chenyo-17" target="_blank" rel="noopener noreferrer">
<i class="fab fa-github"></i>
</a>
</div>
</footer></div>
</body>
</html>