Skip to content

Commit 3540539

Browse files
authored
Merge pull request #2031 from nanjj/tomb
Remove gopkg.in/tomb.v2 dependency
2 parents 5b9b5b2 + 4f30ef3 commit 3540539

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ require (
6161
golang.org/x/text v0.24.0
6262
golang.org/x/tools v0.32.0
6363
google.golang.org/protobuf v1.36.6
64-
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
6564
gopkg.in/yaml.v2 v2.4.0
6665
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e
6766
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,6 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
919919
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
920920
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
921921
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
922-
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 h1:yiW+nvdHb9LVqSHQBXfZCieqV4fzYhNBql77zY0ykqs=
923-
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637/go.mod h1:BHsqpu/nsuzkT5BpiH1EMZPLyqSMM8JbIavyFACoFNk=
924922
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
925923
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
926924
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

internal/server/endpoints/endpoints.go

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"sync"
88
"time"
99

10-
tomb "gopkg.in/tomb.v2"
11-
1210
"github.com/lxc/incus/v6/internal/linux"
1311
"github.com/lxc/incus/v6/internal/server/endpoints/listeners"
1412
"github.com/lxc/incus/v6/internal/util"
@@ -157,7 +155,7 @@ func Up(config *Config) (*Endpoints, error) {
157155
// Endpoints are in charge of bringing up and down the HTTP endpoints for
158156
// serving the REST API.
159157
type Endpoints struct {
160-
tomb *tomb.Tomb // Controls the HTTP servers shutdown.
158+
tomb *Tomb // Controls the HTTP servers shutdown.
161159
mu sync.RWMutex // Serialize access to internal state.
162160
listeners map[kind]net.Listener // Activer listeners by endpoint type.
163161
servers map[kind]*http.Server // HTTP servers by endpoint type.
@@ -425,7 +423,7 @@ func (e *Endpoints) serve(kind kind) {
425423
// Defer the creation of the tomb, so Down() doesn't wait on it unless
426424
// we actually have spawned at least a server.
427425
if e.tomb == nil {
428-
e.tomb = &tomb.Tomb{}
426+
e.tomb = &Tomb{}
429427
}
430428

431429
e.tomb.Go(func() error {
@@ -501,3 +499,59 @@ var descriptions = map[kind]string{
501499
vmvsock: "VM socket",
502500
storageBuckets: "Storage buckets socket",
503501
}
502+
503+
// Tomb tracks the lifecycle of one or more goroutines.
504+
type Tomb struct {
505+
wg sync.WaitGroup
506+
count int
507+
mutex sync.RWMutex
508+
errOnce sync.Once
509+
err error
510+
}
511+
512+
func (g *Tomb) add(delta int) {
513+
g.mutex.Lock()
514+
defer g.mutex.Unlock()
515+
g.count += delta
516+
if g.count >= 0 {
517+
g.wg.Add(delta)
518+
}
519+
}
520+
521+
// Go runs f in a new goroutine and tracks its termination.
522+
func (g *Tomb) Go(f func() error) {
523+
g.add(1)
524+
525+
go func() {
526+
defer g.add(-1)
527+
528+
err := f()
529+
if err != nil {
530+
g.errOnce.Do(func() {
531+
g.err = err
532+
})
533+
}
534+
}()
535+
}
536+
537+
// Kill marks all running goroutings done.
538+
func (g *Tomb) Kill(err error) {
539+
if err != nil {
540+
g.errOnce.Do(func() {
541+
g.err = err
542+
})
543+
}
544+
545+
g.mutex.RLock()
546+
count := g.count
547+
g.mutex.RUnlock()
548+
if count != 0 {
549+
g.add(-count)
550+
}
551+
}
552+
553+
// Wait blocks until all goroutines have finished running.
554+
func (g *Tomb) Wait() error {
555+
g.wg.Wait()
556+
return g.err
557+
}

0 commit comments

Comments
 (0)