Files
tasks/internal/goro/example_group_test.go
Michael SnowdenandGitHub 6ab1dd8df7 Use time.NewTimer instead of time.After (#4596)
<!-- Describe what has changed in this PR -->
**What changed?**
I replaced all usages of `time.After` with `time.NewTimer`. I also added
a linter rule which bans usage of `time.After`.

<!-- Tell your future self why have you made these changes -->
**Why?**
`time.After` leaks resources. It's unclear what resources exactly
because `startTimer` is [implemented in
src/runtime](https://go.dev/src/runtime/time.go#L206), and the docs just
say "The underlying Timer is not recovered by the garbage collector
until the timer fires."

In any case, `NewTimer` is strictly more efficient, and it's not much
harder to use.

<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
**How did you test it?**
I verified that I can create an issue with it:
<img width="1415" alt="image"
src="https://github.com/temporalio/temporal/assets/5942963/315b6839-bdd4-4127-a3dc-1db966755366">


<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
**Potential risks**
This is a bit more verbose for people to use. People also may forget to
call `.Stop()`.

<!-- Is this PR a hotfix candidate or require that a notification be
sent to the broader community? (Yes/No) -->
**Is hotfix candidate?**
No
2023-07-10 10:43:14 -07:00

97 lines
2.7 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package goro_test
import (
"context"
"fmt"
"time"
"go.temporal.io/server/internal/goro"
)
type ExampleService struct {
gorogrp goro.Group // goroutines managed in here
}
func (svc *ExampleService) Start() {
// launch two background goroutines
svc.gorogrp.Go(svc.backgroundLoop1)
svc.gorogrp.Go(svc.backgroundLoop2)
}
func (svc *ExampleService) Stop() {
// stop all goroutines in the goro.Group
svc.gorogrp.Cancel() // interrupt the background goroutines
svc.gorogrp.Wait() // wait for the background goroutines to finish
}
func (svc *ExampleService) backgroundLoop1(ctx context.Context) error {
fmt.Println("starting backgroundLoop1")
defer fmt.Println("stopping backgroundLoop1")
for {
timer := time.NewTimer(1 * time.Minute)
select {
case <-timer.C:
// do something every minute
case <-ctx.Done():
timer.Stop()
return nil
}
}
}
func (svc *ExampleService) backgroundLoop2(ctx context.Context) error {
fmt.Println("starting backgroundLoop2")
defer fmt.Println("stopping backgroundLoop2")
for {
timer := time.NewTimer(10 * time.Second)
select {
case <-timer.C:
// do something every 10 seconds
case <-ctx.Done():
timer.Stop()
return nil
}
}
}
func ExampleGroup() {
var svc ExampleService
svc.Start()
svc.Stop()
// it is safe to call svc.Stop() multiple times
svc.Stop()
svc.Stop()
svc.Stop()
// Unordered output:
// starting backgroundLoop1
// starting backgroundLoop2
// stopping backgroundLoop1
// stopping backgroundLoop2
}