Skip to content

Commit

Permalink
Cleaning + mark #76 as irrelevant.
Browse files Browse the repository at this point in the history
  • Loading branch information
teivah committed Oct 27, 2024
1 parent 4dbc774 commit 006b240
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 94 deletions.
75 changes: 2 additions & 73 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ status: new

This page is a summary of the mistakes in the [100 Go Mistakes and How to Avoid Them book](book.md). Meanwhile, it's also open to the community. If you believe that a common Go mistake should be added, please create an [issue](https://github.com/teivah/100-go-mistakes/issues/new?assignees=&labels=community+mistake&template=community_mistake.md&title=).

???+ success "Support"

This website is 100% free as I wanted to provide to the Go community as much free content as possible from my book. If you find the content useful and want to support my work, feel free to contribute 🙂:

<script type="text/javascript" src="https://cdnjs.buymeacoffee.com/1.0.0/button.prod.min.js" data-name="bmc-button" data-slug="teivah" data-color="#FFDD00" data-emoji="☕" data-font="Cookie" data-text="Buy me a coffee" data-outline-color="#000000" data-font-color="#000000" data-coffee-color="#ffffff" ></script>

![](img/inside-cover.png)

???+ warning "Beta"
Expand Down Expand Up @@ -1988,74 +1982,9 @@ ticker = time.NewTicker(1000 * time.Nanosecond)

### `time.After` and memory leaks (#76)

???+ info "TL;DR"

Avoiding calls to `time.After` in repeated functions (such as loops or HTTP handlers) can avoid peak memory consumption. The resources created by `time.After` are released only when the timer expires.

Developers often use `time.After` in loops or HTTP handlers repeatedly to implement the timing function. But it can lead to unintended peak memory consumption due to the delayed release of resources, just like the following code:

```go
func consumer(ch <-chan Event) {
for {
select {
case event := <-ch:
handle(event)
case <-time.After(time.Hour):
log.Println("warning: no messages received")
}
}
}
```

The source code of the function time.After is as follows:

```go
func After(d Duration) <-chan Time {
return NewTimer(d).C
}
```

As we see, it returns receive-only channel.

When `time.After` is used in a loop or repeated context, a new channel is created in each iteration. If these channels are not properly closed or if their associated timers are not stopped, they can accumulate and consume memory. The resources associated with each timer and channel are only released when the timer expires or the channel is closed.

To avoid this happening, We can use context's timeout setting instead of `time.After`, like below:

```go
func consumer(ch <-chan Event) {
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Hour)
select {
case event := <-ch:
cancel()
handle(event)
case <-ctx.Done():
log.Println("warning: no messages received")
}
}
}
```

We can also use `time.NewTimer` like so:

```go
func consumer(ch <-chan Event) {
timerDuration := 1 * time.Hour
timer := time.NewTimer(timerDuration)

for {
timer.Reset(timerDuration)
select {
case event := <-ch:
handle(event)
case <-timer.C:
log.Println("warning: no messages received")
}
}
}
```
???+ warning

[:simple-github: Source code](https://github.com/teivah/100-go-mistakes/tree/master/src/10-standard-lib/76-time-after/main.go)
This mistake isn't relevant anymore from Go 1.23 ([details](https://go.dev/wiki/Go123Timer)).

### JSON handling common mistakes (#77)

Expand Down
5 changes: 0 additions & 5 deletions site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3436,11 +3436,6 @@

<h1 id="common-go-mistakes">Common Go Mistakes</h1>
<p>This page is a summary of the mistakes in the <a href="book/">100 Go Mistakes and How to Avoid Them book</a>. Meanwhile, it's also open to the community. If you believe that a common Go mistake should be added, please create an <a href="https://github.com/teivah/100-go-mistakes/issues/new?assignees=&amp;labels=community+mistake&amp;template=community_mistake.md&amp;title=">issue</a>.</p>
<details class="success" open="open">
<summary>Support</summary>
<p>This website is 100% free as I wanted to provide to the Go community as much free content as possible from my book. If you find the content useful and want to support my work, feel free to contribute 🙂:</p>
<p><script type="text/javascript" src="https://cdnjs.buymeacoffee.com/1.0.0/button.prod.min.js" data-name="bmc-button" data-slug="teivah" data-color="#FFDD00" data-emoji="" data-font="Cookie" data-text="Buy me a coffee" data-outline-color="#000000" data-font-color="#000000" data-coffee-color="#ffffff" ></script></p>
</details>
<p><a class="glightbox" href="img/inside-cover.png" data-type="image" data-width="auto" data-height="auto" data-desc-position="bottom"><img alt="" src="img/inside-cover.png" /></a></p>
<details class="warning" open="open">
<summary>Beta</summary>
Expand Down
2 changes: 1 addition & 1 deletion site/search/search_index.json

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions site/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,77 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://100go.co/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/20-slice/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/28-maps-memory-leaks/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/5-interface-pollution/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/56-concurrency-faster/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/89-benchmarks/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/9-generics/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/92-false-sharing/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/98-profiling-execution-tracing/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/book/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/chapter-1/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/external/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/ja/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/pt-br/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://100go.co/zh/</loc>
<lastmod>2024-10-13</lastmod>
<lastmod>2024-10-27</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
Binary file modified site/sitemap.xml.gz
Binary file not shown.

0 comments on commit 006b240

Please sign in to comment.