diff --git a/client/client.go b/client/client.go index 525680654..fe944e867 100644 --- a/client/client.go +++ b/client/client.go @@ -29,28 +29,13 @@ package client import ( "context" - "os" - "os/signal" - "syscall" "time" - v1 "github.com/docker/api/backend/v1" "google.golang.org/grpc" "google.golang.org/grpc/backoff" -) -// NewContext is a context that is canceled when a signal is -// sent to the process -func NewContext() (context.Context, func()) { - ctx, cancel := context.WithCancel(context.Background()) - s := make(chan os.Signal) - signal.Notify(s, syscall.SIGTERM, syscall.SIGINT) - go func() { - <-s - cancel() - }() - return ctx, cancel -} + v1 "github.com/docker/api/backend/v1" +) // New returns a GRPC client func New(address string, timeout time.Duration) (*Client, error) { diff --git a/cmd/example.go b/cmd/example.go index 371cb32db..f2916f33a 100644 --- a/cmd/example.go +++ b/cmd/example.go @@ -35,6 +35,8 @@ import ( "time" "github.com/docker/api/client" + "github.com/docker/api/util" + "github.com/golang/protobuf/ptypes/empty" "github.com/pkg/errors" "github.com/urfave/cli/v2" @@ -45,7 +47,7 @@ var exampleCommand = cli.Command{ Usage: "sample command using backend, to be removed later", Action: func(clix *cli.Context) error { // return information for the current context - ctx, cancel := client.NewContext() + ctx, cancel := util.NewSigContext() defer cancel() // get our current context diff --git a/example/backend/main.go b/example/backend/main.go index ee3f25678..95fe9fcb8 100644 --- a/example/backend/main.go +++ b/example/backend/main.go @@ -33,13 +33,14 @@ import ( "net" "os" - v1 "github.com/docker/api/backend/v1" - "github.com/docker/api/client" - "github.com/docker/api/server" "github.com/golang/protobuf/ptypes/empty" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" + + v1 "github.com/docker/api/backend/v1" + "github.com/docker/api/server" + apiUtil "github.com/docker/api/util" ) func main() { @@ -66,7 +67,7 @@ func main() { return nil } app.Action = func(clix *cli.Context) error { - ctx, cancel := client.NewContext() + ctx, cancel := apiUtil.NewSigContext() defer cancel() // create a new GRPC server with the provided server package diff --git a/util/util.go b/util/util.go new file mode 100644 index 000000000..9ad70d3d6 --- /dev/null +++ b/util/util.go @@ -0,0 +1,48 @@ +/* + Copyright (c) 2019 Docker 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 util + +import ( + "context" + "os" + "os/signal" + "syscall" +) + +// NewSigContext is a context that is canceled when a signal is +// sent to the process +func NewSigContext() (context.Context, func()) { + ctx, cancel := context.WithCancel(context.Background()) + s := make(chan os.Signal) + signal.Notify(s, syscall.SIGTERM, syscall.SIGINT) + go func() { + <-s + cancel() + }() + return ctx, cancel +}