mirror of
https://github.com/kopia/kopia.git
synced 2026-05-24 14:44:47 -04:00
refactor(general): levarage stdlib's context.WithoutCancel(ctx) (#4159)
Removes `internal/ctxutil` package with equivalent implementation
This commit is contained in:
@@ -20,7 +20,6 @@
|
||||
htpasswd "github.com/tg123/go-htpasswd"
|
||||
|
||||
"github.com/kopia/kopia/internal/auth"
|
||||
"github.com/kopia/kopia/internal/ctxutil"
|
||||
"github.com/kopia/kopia/internal/server"
|
||||
"github.com/kopia/kopia/repo"
|
||||
)
|
||||
@@ -256,11 +255,12 @@ func (c *commandServerStart) run(ctx context.Context) (reterr error) {
|
||||
if c.serverStartShutdownWhenStdinClosed {
|
||||
log(ctx).Info("Server will close when stdin is closed...")
|
||||
|
||||
ctxutil.GoDetached(ctx, func(ctx context.Context) {
|
||||
go func() {
|
||||
ctx := context.WithoutCancel(ctx)
|
||||
// consume all stdin and close the server when it closes
|
||||
io.Copy(io.Discard, os.Stdin) //nolint:errcheck
|
||||
shutdownHTTPServer(ctx, httpServer)
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
onExternalConfigReloadRequest(srv.Refresh)
|
||||
|
||||
3
internal/cache/cache_storage.go
vendored
3
internal/cache/cache_storage.go
vendored
@@ -8,7 +8,6 @@
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/kopia/kopia/internal/ctxutil"
|
||||
"github.com/kopia/kopia/internal/ospath"
|
||||
"github.com/kopia/kopia/repo/blob"
|
||||
"github.com/kopia/kopia/repo/blob/filesystem"
|
||||
@@ -45,7 +44,7 @@ func NewStorageOrNil(ctx context.Context, cacheDir string, maxBytes int64, subdi
|
||||
}
|
||||
}
|
||||
|
||||
fs, err := filesystem.New(ctxutil.Detach(ctx), &filesystem.Options{
|
||||
fs, err := filesystem.New(context.WithoutCancel(ctx), &filesystem.Options{
|
||||
Path: contentCacheDir,
|
||||
Options: sharded.Options{
|
||||
DirectoryShards: []int{2},
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
// Package ctxutil implements utilities for manipulating context.
|
||||
package ctxutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type detachedContext struct {
|
||||
// inherit most methods from context.Background()
|
||||
context.Context //nolint:containedctx
|
||||
wrapped context.Context //nolint:containedctx
|
||||
}
|
||||
|
||||
// Detach returns a context that inheris provided context's values but not deadline or cancellation.
|
||||
func Detach(ctx context.Context) context.Context {
|
||||
return detachedContext{context.Background(), ctx}
|
||||
}
|
||||
|
||||
// GoDetached invokes the provided function in a goroutine where the context is detached.
|
||||
func GoDetached(ctx context.Context, fun func(ctx context.Context)) {
|
||||
go func() {
|
||||
fun(Detach(ctx))
|
||||
}()
|
||||
}
|
||||
|
||||
func (d detachedContext) Value(key interface{}) interface{} {
|
||||
return d.wrapped.Value(key)
|
||||
}
|
||||
@@ -10,7 +10,6 @@
|
||||
"time"
|
||||
|
||||
"github.com/kopia/kopia/internal/clock"
|
||||
"github.com/kopia/kopia/internal/ctxutil"
|
||||
"github.com/kopia/kopia/repo/logging"
|
||||
)
|
||||
|
||||
@@ -66,7 +65,7 @@ func Start(ctx context.Context, getItems GetItemsFunc, opts Options) *Scheduler
|
||||
go func() {
|
||||
defer s.wg.Done()
|
||||
|
||||
s.run(ctxutil.Detach(ctx))
|
||||
s.run(context.WithoutCancel(ctx))
|
||||
}()
|
||||
|
||||
return s
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
"github.com/kopia/kopia/internal/auth"
|
||||
"github.com/kopia/kopia/internal/clock"
|
||||
"github.com/kopia/kopia/internal/ctxutil"
|
||||
"github.com/kopia/kopia/internal/mount"
|
||||
"github.com/kopia/kopia/internal/passwordpersist"
|
||||
"github.com/kopia/kopia/internal/scheduler"
|
||||
@@ -362,7 +361,7 @@ func (s *Server) handleRequestPossiblyNotConnected(isAuthorized isAuthorizedFunc
|
||||
// process the request while ignoring the cancellation signal
|
||||
// to ensure all goroutines started by it won't be canceled
|
||||
// when the request finishes.
|
||||
ctx = ctxutil.Detach(ctx)
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
|
||||
if isAuthorized(ctx, rc) {
|
||||
v, err = f(ctx, rc)
|
||||
@@ -585,7 +584,7 @@ func (s *Server) SetRepository(ctx context.Context, rep repo.Repository) error {
|
||||
s.maint = nil
|
||||
}
|
||||
|
||||
s.sched = scheduler.Start(ctxutil.Detach(ctx), s.getSchedulerItems, scheduler.Options{
|
||||
s.sched = scheduler.Start(context.WithoutCancel(ctx), s.getSchedulerItems, scheduler.Options{
|
||||
TimeNow: clock.Now,
|
||||
Debug: s.options.DebugScheduler,
|
||||
RefreshChannel: s.schedulerRefresh,
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
"github.com/kopia/kopia/fs"
|
||||
"github.com/kopia/kopia/fs/localfs"
|
||||
"github.com/kopia/kopia/internal/clock"
|
||||
"github.com/kopia/kopia/internal/ctxutil"
|
||||
"github.com/kopia/kopia/internal/serverapi"
|
||||
"github.com/kopia/kopia/internal/uitask"
|
||||
"github.com/kopia/kopia/repo"
|
||||
@@ -159,7 +158,7 @@ func (s *sourceManager) start(ctx context.Context, isLocal bool) {
|
||||
|
||||
func (s *sourceManager) run(ctx context.Context, isLocal bool) {
|
||||
// make sure we run in a detached context, which ignores outside cancellation and deadline.
|
||||
ctx = ctxutil.Detach(ctx)
|
||||
ctx = context.WithoutCancel(ctx)
|
||||
|
||||
s.setStatus("INITIALIZING")
|
||||
defer s.setStatus("STOPPED")
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
"google.golang.org/grpc/credentials"
|
||||
|
||||
"github.com/kopia/kopia/internal/clock"
|
||||
"github.com/kopia/kopia/internal/ctxutil"
|
||||
"github.com/kopia/kopia/internal/gather"
|
||||
apipb "github.com/kopia/kopia/internal/grpcapi"
|
||||
"github.com/kopia/kopia/internal/retry"
|
||||
@@ -739,7 +738,7 @@ func (r *grpcRepositoryClient) WriteContent(ctx context.Context, data gather.Byt
|
||||
// clone so that caller can reuse the buffer
|
||||
clone := data.ToByteSlice()
|
||||
|
||||
if err := r.doWriteAsync(ctxutil.Detach(ctx), contentID, clone, prefix, comp); err != nil {
|
||||
if err := r.doWriteAsync(context.WithoutCancel(ctx), contentID, clone, prefix, comp); err != nil {
|
||||
return content.EmptyID, err
|
||||
}
|
||||
|
||||
@@ -897,7 +896,7 @@ func (r *grpcRepositoryClient) getOrEstablishInnerSession(ctx context.Context) (
|
||||
r.innerSessionAttemptCount++
|
||||
|
||||
v, err := retry.WithExponentialBackoff(ctx, "establishing session", func() (*grpcInnerSession, error) {
|
||||
sess, err := cli.Session(ctxutil.Detach(ctx))
|
||||
sess, err := cli.Session(context.WithoutCancel(ctx))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Session()")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user