Parcourir la source

Merge pull request #18 from ulyssessouza/refactoring-context

Refactor NewContext
Ulysses Souza il y a 5 ans
Parent
commit
9c6a9957aa
4 fichiers modifiés avec 58 ajouts et 22 suppressions
  1. 2 17
      client/client.go
  2. 3 1
      cmd/example.go
  3. 5 4
      example/backend/main.go
  4. 48 0
      util/util.go

+ 2 - 17
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) {

+ 3 - 1
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

+ 5 - 4
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

+ 48 - 0
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
+}