Skip to content

Commit 07df7d8

Browse files
authored
fix(pubsub): check both client errors on Close (#2867)
* fix(pubsub): check both client errors on Close * fix code formatting * don't check error from client.Close * call both Close before returning error * address review comments
1 parent 3a1457f commit 07df7d8

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

pubsub/go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
433433
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
434434
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
435435
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
436+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
436437
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
437438
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
438439
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

pubsub/pubsub.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,11 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
7878
o = append(o, opts...)
7979
pubc, err := vkit.NewPublisherClient(ctx, o...)
8080
if err != nil {
81-
return nil, fmt.Errorf("pubsub: %v", err)
81+
return nil, fmt.Errorf("pubsub(publisher): %v", err)
8282
}
8383
subc, err := vkit.NewSubscriberClient(ctx, o...)
8484
if err != nil {
85-
// Should never happen, since we are passing in the connection.
86-
// If it does, we cannot close, because the user may have passed in their
87-
// own connection originally.
88-
return nil, fmt.Errorf("pubsub: %v", err)
85+
return nil, fmt.Errorf("pubsub(subscriber): %v", err)
8986
}
9087
pubc.SetGoogleClientInfo("gccl", version.Repo)
9188
return &Client{
@@ -101,10 +98,15 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
10198
// If the client is available for the lifetime of the program, then Close need not be
10299
// called at exit.
103100
func (c *Client) Close() error {
104-
// Return the first error, because the first call closes the connection.
105-
err := c.pubc.Close()
106-
_ = c.subc.Close()
107-
return err
101+
pubErr := c.pubc.Close()
102+
subErr := c.subc.Close()
103+
if pubErr != nil {
104+
return fmt.Errorf("pubsub subscriber closing error: %v", pubErr)
105+
}
106+
if subErr != nil {
107+
return fmt.Errorf("pubsub publisher closing error: %v", subErr)
108+
}
109+
return nil
108110
}
109111

110112
func (c *Client) fullyQualifiedProjectName() string {

pubsub/streaming_pull_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,12 @@ func TestStreamingPull_ClosedClient(t *testing.T) {
315315
// wait for receives to happen
316316
time.Sleep(100 * time.Millisecond)
317317

318-
err := client.Close()
319-
if err != nil {
320-
t.Fatal(err)
321-
}
318+
// Intentionally don't check the returned err here. In the fake,
319+
// closing either the publisher/subscriber client will cause the
320+
// server to clean up resources, which is different than in the
321+
// live service. With the fake, client.Close() will return a
322+
// "the client connection is closing" error with the second Close.
323+
client.Close()
322324

323325
// wait for things to close
324326
time.Sleep(100 * time.Millisecond)
@@ -327,7 +329,7 @@ func TestStreamingPull_ClosedClient(t *testing.T) {
327329
case recvErr := <-recvFinished:
328330
s, ok := status.FromError(recvErr)
329331
if !ok {
330-
t.Fatalf("Expected a gRPC failure, got %v", err)
332+
t.Fatalf("Expected a gRPC failure, got %v", recvErr)
331333
}
332334
if s.Code() != codes.Canceled {
333335
t.Fatalf("Expected canceled, got %v", s.Code())

0 commit comments

Comments
 (0)