Skip to content

Commit 6775592

Browse files
committed
Remove gopkg.in/tomb.v2 dependency
`gopkg.in/tomb.v2` is inactive for long time and incus use less function of it. Signed-off-by: JUN JIE NAN <[email protected]>
1 parent 0068ddd commit 6775592

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-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: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55
"net"
66
"net/http"
77
"sync"
8+
"sync/atomic"
89
"time"
910

10-
tomb "gopkg.in/tomb.v2"
11-
1211
"github.com/lxc/incus/v6/internal/linux"
1312
"github.com/lxc/incus/v6/internal/server/endpoints/listeners"
1413
"github.com/lxc/incus/v6/internal/util"
@@ -157,7 +156,7 @@ func Up(config *Config) (*Endpoints, error) {
157156
// Endpoints are in charge of bringing up and down the HTTP endpoints for
158157
// serving the REST API.
159158
type Endpoints struct {
160-
tomb *tomb.Tomb // Controls the HTTP servers shutdown.
159+
tomb *Tomb // Controls the HTTP servers shutdown.
161160
mu sync.RWMutex // Serialize access to internal state.
162161
listeners map[kind]net.Listener // Activer listeners by endpoint type.
163162
servers map[kind]*http.Server // HTTP servers by endpoint type.
@@ -425,7 +424,7 @@ func (e *Endpoints) serve(kind kind) {
425424
// Defer the creation of the tomb, so Down() doesn't wait on it unless
426425
// we actually have spawned at least a server.
427426
if e.tomb == nil {
428-
e.tomb = &tomb.Tomb{}
427+
e.tomb = &Tomb{}
429428
}
430429

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

0 commit comments

Comments
 (0)