From aa506ae3b4950dc8b8e18a3f56fc7b91750dc146 Mon Sep 17 00:00:00 2001 From: Michael Jarvis Date: Thu, 5 Oct 2023 13:08:47 -0500 Subject: [PATCH] Add module support, clean up deprecated functions --- go.mod | 14 ++++++++++++++ go.sum | 21 +++++++++++++++++++++ pool_test.go | 28 +++++++++++++++------------- 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fe68f48 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module github.com/processout/grpc-go-pool + +go 1.21.1 + +require google.golang.org/grpc v1.58.2 + +require ( + github.com/golang/protobuf v1.5.3 // indirect + golang.org/x/net v0.12.0 // indirect + golang.org/x/sys v0.10.0 // indirect + golang.org/x/text v0.11.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect + google.golang.org/protobuf v1.31.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..fe42a52 --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50= +golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 h1:bVf09lpb+OJbByTj913DRJioFFAjf/ZGxEz7MajTp2U= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM= +google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I= +google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= diff --git a/pool_test.go b/pool_test.go index 4e04524..42f02b6 100644 --- a/pool_test.go +++ b/pool_test.go @@ -7,11 +7,12 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/connectivity" + "google.golang.org/grpc/credentials/insecure" ) func TestNew(t *testing.T) { p, err := New(func() (*grpc.ClientConn, error) { - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) }, 1, 3, 0) if err != nil { t.Errorf("The pool returned an error: %s", err.Error()) @@ -95,7 +96,7 @@ func TestNew(t *testing.T) { func TestTimeout(t *testing.T) { p, err := New(func() (*grpc.ClientConn, error) { - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) }, 1, 1, 0) if err != nil { t.Errorf("The pool returned an error: %s", err.Error()) @@ -110,9 +111,10 @@ func TestTimeout(t *testing.T) { } // We want to fetch a second one, with a timeout. If the timeout was - // ommitted, the pool would wait indefinitely as it'd wait for another + // omitted, the pool would wait indefinitely as it'd wait for another // client to get back into the queue - ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond)) + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond)) + defer cancel() _, err2 := p.Get(ctx) if err2 != ErrTimeout { t.Errorf("Expected error \"%s\" but got \"%s\"", ErrTimeout, err2.Error()) @@ -121,7 +123,7 @@ func TestTimeout(t *testing.T) { func TestMaxLifeDuration(t *testing.T) { p, err := New(func() (*grpc.ClientConn, error) { - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) }, 1, 1, 0, 1) if err != nil { t.Errorf("The pool returned an error: %s", err.Error()) @@ -145,7 +147,7 @@ func TestMaxLifeDuration(t *testing.T) { count := 0 p, err = New(func() (*grpc.ClientConn, error) { count++ - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) }, 1, 1, 0, time.Minute) if err != nil { t.Errorf("The pool returned an error: %s", err.Error()) @@ -176,7 +178,7 @@ func TestMaxLifeDuration(t *testing.T) { func TestPoolClose(t *testing.T) { p, err := New(func() (*grpc.ClientConn, error) { - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) }, 1, 1, 0) if err != nil { t.Errorf("The pool returned an error: %s", err.Error()) @@ -200,7 +202,7 @@ func TestPoolClose(t *testing.T) { } } -func TestContextCancelation(t *testing.T) { +func TestContextCancellation(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -210,7 +212,7 @@ func TestContextCancelation(t *testing.T) { return nil, ctx.Err() default: - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) } }, 1, 1, 0) @@ -230,7 +232,7 @@ func TestContextTimeout(t *testing.T) { // wait for the deadline to pass case <-time.After(time.Millisecond): - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) } }, 1, 1, 0) @@ -242,7 +244,7 @@ func TestContextTimeout(t *testing.T) { func TestGetContextTimeout(t *testing.T) { p, err := New(func() (*grpc.ClientConn, error) { - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) }, 1, 1, 0) if err != nil { @@ -271,7 +273,7 @@ func TestGetContextFactoryTimeout(t *testing.T) { // wait for the deadline to pass case <-time.After(time.Millisecond): - return grpc.Dial("example.com", grpc.WithInsecure()) + return grpc.Dial("example.com", grpc.WithTransportCredentials(insecure.NewCredentials())) } }, 1, 1, 0) @@ -280,7 +282,7 @@ func TestGetContextFactoryTimeout(t *testing.T) { t.Errorf("The pool returned an error: %s", err.Error()) } - // mark as unhealty the available conn + // mark as unhealthy the available conn c, err := p.Get(context.Background()) if err != nil { t.Errorf("Get returned an error: %s", err.Error())