mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
<!-- Describe what has changed in this PR --> **What changed?** I removed the common.Daemon interface. <!-- Tell your future self why have you made these changes --> **Why?** The main reason is that it makes it nearly impossible to figure out where a Start or Stop method of a concrete type is actually called. Before: <img width="1254" alt="image" src="https://github.com/temporalio/temporal/assets/5942963/bdbe5d97-0d60-4690-a873-bf9797441284"> After: <img width="724" alt="image" src="https://github.com/temporalio/temporal/assets/5942963/8bdcb01a-b5e9-4ba7-8f9a-ccbc6bc6a5d9"> Conversely, it makes it harder to find the actual implementation for a method at its callsite. Before: <img width="851" alt="image" src="https://github.com/temporalio/temporal/assets/5942963/c651b90b-430c-4a93-951e-21fc26e1f309"> After: <img width="908" alt="image" src="https://github.com/temporalio/temporal/assets/5942963/2e877c3f-7a63-48b8-a83a-302b93be92f3"> Finally, we actually don't use this abstraction for anything. We don't need to ever view something as an abstract background process--only fx needs to define and consume an abstraction like that. Most of our callsites of Start and Stop already have a more concrete type available. It's a useful abstraction *conceptually*, but, in code, it just makes it impossible to find where things are used or defined. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** I verified that it's much easier to find where a Start or Stop method is defined / called. To write this, I removed the embedded common.Daemon from all interfaces, looked for unresolved-reference errors, and then I added the method to the type of whatever variable was complaining. That way, the methods are present at the most concrete level possible, which will ensure that there are as few choices as possible when finding references or definitions. <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** Maybe we were doing something weird with reflection for this or there's some other use case I'm unaware of. <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?** No.
79 lines
2.9 KiB
Go
79 lines
2.9 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
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
)
|
|
|
|
// Group manages a set of long-running goroutines. Goroutines are spawned
|
|
// individually with Group.Go and after that interrupted and waited-on as a
|
|
// single unit. The expected use-case for this type is as a member field of a
|
|
// background process type that spawns one or more goroutines in its Start()
|
|
// function and then stops those same goroutines in its Stop() function.
|
|
// The zero-value of this type is valid. A Group must not be copied after
|
|
// first use.
|
|
type Group struct {
|
|
initOnce sync.Once
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
// Go spawns a goroutine whose lifecycle will be managed via this Group object.
|
|
// All functions passed to Go on the same instance of Group will be given the
|
|
// same `context.Context` object, which will be used to indicate cancellation.
|
|
// If the supplied func does not abide by `ctx.Done()` of the provided
|
|
// `context.Context` then `Wait()` on this `Group` will hang until all functions
|
|
// exit on their own (possibly never).
|
|
// NOTE: Errors returned by the supplied function are ignored.
|
|
func (g *Group) Go(f func(ctx context.Context) error) {
|
|
g.initOnce.Do(g.init)
|
|
g.wg.Add(1)
|
|
go func() {
|
|
defer g.wg.Done()
|
|
_ = f(g.ctx)
|
|
}()
|
|
}
|
|
|
|
// Cancel cancels the `context.Context` that was passed to all goroutines
|
|
// spawned via `Go` on this `Group`.
|
|
func (g *Group) Cancel() {
|
|
g.initOnce.Do(g.init)
|
|
g.cancel()
|
|
}
|
|
|
|
// Wait blocks waiting for all goroutines spawned via `Go` on this `Group`
|
|
// instance to complete. If `Go` has not been called then this function returns
|
|
// immediately.
|
|
func (g *Group) Wait() {
|
|
g.wg.Wait()
|
|
}
|
|
|
|
func (g *Group) init() {
|
|
g.ctx, g.cancel = context.WithCancel(context.Background())
|
|
}
|