Skip to content

Commit

Permalink
continued go pattern post
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyo-17 committed Jun 27, 2024
1 parent 0da73ef commit 1d519a3
Show file tree
Hide file tree
Showing 9 changed files with 1,150 additions and 135 deletions.
200 changes: 188 additions & 12 deletions 2024-06-25-go-patterns.html

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions 2024-06-26-watched:-argo.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org9f9987e">1. Why did I watch it</a></li>
<li><a href="#org04d2aca">2. What is it about</a></li>
<li><a href="#orgf75f771">3. Did I like it</a></li>
<li><a href="#org82e980a">1. Why did I watch it</a></li>
<li><a href="#orgd876739">2. What is it about</a></li>
<li><a href="#orgacbe90d">3. Did I like it</a></li>
</ul>
</div>
</nav>

<figure id="org01a338e">
<figure id="org3c67866">
<img src="https://miro.medium.com/v2/resize:fit:1100/format:webp/1*IFB2V5vjsVKtHcxBdCfQSQ.jpeg" alt="1*IFB2V5vjsVKtHcxBdCfQSQ.jpeg" align="center" width="300px">

<figcaption><span class="figure-number">Figure 1: </span>Argo poster</figcaption>
</figure>
<div id="outline-container-org9f9987e" class="outline-2">
<h2 id="org9f9987e"><span class="section-number-2">1.</span> Why did I watch it</h2>
<div id="outline-container-org82e980a" class="outline-2">
<h2 id="org82e980a"><span class="section-number-2">1.</span> Why did I watch it</h2>
<div class="outline-text-2" id="text-1">
<p>
This week, I found myself craving a movie with a &ldquo;Homeland&rdquo; theme.
Expand All @@ -39,8 +39,8 @@ <h2 id="org9f9987e"><span class="section-number-2">1.</span> Why did I watch it<
</p>
</div>
</div>
<div id="outline-container-org04d2aca" class="outline-2">
<h2 id="org04d2aca"><span class="section-number-2">2.</span> What is it about</h2>
<div id="outline-container-orgd876739" class="outline-2">
<h2 id="orgd876739"><span class="section-number-2">2.</span> What is it about</h2>
<div class="outline-text-2" id="text-2">
<p>
It has little thing to do with &ldquo;Homeland&rdquo;.
Expand All @@ -51,8 +51,8 @@ <h2 id="org04d2aca"><span class="section-number-2">2.</span> What is it about</h
</p>
</div>
</div>
<div id="outline-container-orgf75f771" class="outline-2">
<h2 id="orgf75f771"><span class="section-number-2">3.</span> Did I like it</h2>
<div id="outline-container-orgacbe90d" class="outline-2">
<h2 id="orgacbe90d"><span class="section-number-2">3.</span> Did I like it</h2>
<div class="outline-text-2" id="text-3">
<p>
I guess?
Expand All @@ -61,27 +61,23 @@ <h2 id="orgf75f771"><span class="section-number-2">3.</span> Did I like it</h2>
What really amazed me was when I found the actor of Tony was also the director, and also wrote &ldquo;Good Will Hunting (1997)&rdquo;, what a genius!
But I guessed I preferred &ldquo;Civil war (2024)&rdquo; (my last movie) to this one.
The reason?
I felt &ldquo;Argo&rdquo; could have delved deeper into the absurdities of war rather than solely focusing on a heroic narrative.
</p>


<figure id="orgd60b618">
<figure id="org57a2017">
<img src="https://m.media-amazon.com/images/M/MV5BOTI0MzcxMTYtZDVkMy00NjY1LTgyMTYtZmUxN2M3NmQ2NWJhXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_.jpg" alt="MV5BOTI0MzcxMTYtZDVkMy00NjY1LTgyMTYtZmUxN2M3NmQ2NWJhXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_.jpg" align="center" width="300px">

<figcaption><span class="figure-number">Figure 2: </span>Good Will Hunting poster</figcaption>
</figure>



<figure id="orgbe56d28">
<figure id="orgf1e12ae">
<img src="https://posterspy.com/wp-content/uploads/2024/04/Civil-War-Kirsten.jpg" alt="Civil-War-Kirsten.jpg" align="center" width="300px">

<figcaption><span class="figure-number">Figure 3: </span>Civil War</figcaption>
<figcaption><span class="figure-number">Figure 3: </span>Civil War poster</figcaption>
</figure>


<p>
I felt &ldquo;Argo&rdquo; could have delved deeper into the absurdities of war rather than solely focusing on a heroic narrative.
</p>
</div>
</div>
<div class="taglist"><a href="https://chenyo-17.github.io/org-static-blog/tags.html">Tags</a>: <a href="https://chenyo-17.github.io/org-static-blog/tag-movie-review.html">movie-review</a> </div></div>
Expand Down
232 changes: 202 additions & 30 deletions index.html

Large diffs are not rendered by default.

151 changes: 151 additions & 0 deletions posts/2024-06-25-go-patterns.org
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,154 @@ func main() {
}
}
#+end_src
- check goroutine blockage
- ~Ctrl-\~ sends ~SIGQUIT~
- use the HTTP server's ~/debug/pprof/goroutine~ if importing ~net/http~


* Pattern 1: publish/subscribe server
- the information goes one way: server -> client
- close a channel to signal no new values will be sent
- prefer ~defer~ when unlocking the mutex
#+begin_src go -n
type Server struct {
mu sync.Mutex // protect sub
sub map[chan<- Event]bool // whether a channel should be closed
}
func (s *Server) Init() {
s.sub = make(map[chan<- Event]bool)
}
// publish an event to all subscribed channel
func (s *Server) Publish(e Event) {
s.mu.Lock() // each method could be called many clients
defer s.mu.Unlock()
for c := range s.sub {
// if a goroutine consumes the channel events too slow
// then a new event publish has to wait before it can send to the channel
// can add channel buffer
c <- e
}
}
// a channel starts to subscribe
func (s *Server) Subscribe(c chan<- Event) {
s.mu.Lock()
defer s.mu.Unlock()
if s.sub[c] {
panic("pubsub: already subscribed") // the mutex wil also be unlocked with defer
}
s.sub[c] = true
}
// a channel cancels the subscription
func (s *Server) Cancel(c chan<- Event) {
s.mu.Lock()
defer s.mu.Unlock()
if !s.sub[c] {
panic("pubsub: not subscribed")
}
close(c)
delete(s.sub, c)
}
#+end_src

** Options for slow goroutines
- slow down event generation
- drop events if it cannot be sent, e.g., ~os/signal~, ~runtime/pprof~
- queue events, e.g., add a ~helper~ between the server and each client, which also separates the concerns
#+begin_src go -n
func helper(in <-chan Event, out chan<- Event) {
var q []Event
// if the in is closed, flash out the pending events in q
// and close out
for in != nil || len(q) > 0 {
// decide whether and what to send
var sendOut chan<- Event
var next Event
if len(q) > 0 {
sendOut = out
next = q[0]
}
select {
case e, ok := <-in: // never reaches here after in = nil
// ok tells whether in is closed
if !ok {
in = nil
break
}
q = append(q, e)
case sendOut <- next: // if len(q) == 0, sendOut = nil
q = q[1:]
}
}
close(out)
}
#+end_src
- convert mutexes into goroutines, not suitable for Raft where state transition is complex
#+begin_src go -n
type Server struct {
publish chan Event
subscribe chan subReq
cancel chan subReq
}
type subReq struct {
c chan<- Event
// a signal of whether an operation succeeds
ok chan bool
}

func (s *Server) Init() {
s.publish = make(chan Event)
s.subscribe = make(chan subReq)
s.cancel = make(chan subReq)
go s.loop()
}
func (s *Server) Publish(e Event) {
s.publish <- e
}
func (s *Server) Subscribe(c chan<- Event) {
r := subReq{c: c, ok: make(chan bool)}
s.subscribe <- r
if !<-r.ok {
panic("pubsub: already subscribed")
}
}
func (s *Server) Cancel(c chan<- Event) {
r := subReq{c: c, ok: make(chan bool)}
s.cancel <- r
if !<-r.ok {
panic("pubusb: not subscribed")
}
}
func (s *Server) loop() {
// now sub is a local variable, no lock is needed
// sub maps from a subscribed channel to a helper channel
sub := make(map[chan<- Event]chan<- Event)
for {
select {
case e := <-s.publish:
for _, h := range sub {
// the event is published to a helper channel
h <- e
}
case r := <-s.subscribe:
// the helper channel exists
if sub[r.c] != nil {
r.ok <- false
break
}
h = make(chan Event)
go helper(h, r.c)
sub[r.c] = h
r.ok <- true
case c := <-s.cancel:
if !sub[r.c] == nil{
r.ok <- false
break
}
// close the helper channel
close(sub[r.c])
delete(sub, r.c)
r.ok <- true
}
}
}
#+end_src
4 changes: 2 additions & 2 deletions posts/2024-06-26-watched:-argo.org
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ There were some moments where I was not fully convinced, but I accept them if th
What really amazed me was when I found the actor of Tony was also the director, and also wrote "Good Will Hunting (1997)", what a genius!
But I guessed I preferred "Civil war (2024)" (my last movie) to this one.
The reason?
I felt "Argo" could have delved deeper into the absurdities of war rather than solely focusing on a heroic narrative.

#+CAPTION: Good Will Hunting poster
#+ATTR_HTML: :align center
#+ATTR_HTML: :width 300px
[[https://m.media-amazon.com/images/M/MV5BOTI0MzcxMTYtZDVkMy00NjY1LTgyMTYtZmUxN2M3NmQ2NWJhXkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_.jpg]]


#+CAPTION: Civil War
#+CAPTION: Civil War poster
#+ATTR_HTML: :align center
#+ATTR_HTML: :width 300px
[[https://posterspy.com/wp-content/uploads/2024/04/Civil-War-Kirsten.jpg]]


I felt "Argo" could have delved deeper into the absurdities of war rather than solely focusing on a heroic narrative.
Loading

0 comments on commit 1d519a3

Please sign in to comment.