Skip to content

Commit 8387f79

Browse files
authored
chore(pubsub/v2): use client hooks for emulator (#14487)
Fixes #10577
1 parent a07a26b commit 8387f79

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

pubsub/v2/apiv1/emulator_hook.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pubsub
16+
17+
import (
18+
"context"
19+
"os"
20+
21+
"google.golang.org/api/option"
22+
"google.golang.org/api/option/internaloption"
23+
"google.golang.org/grpc"
24+
"google.golang.org/grpc/credentials/insecure"
25+
)
26+
27+
func init() {
28+
newTopicAdminClientHook = emulatorHook
29+
newSubscriptionAdminClientHook = emulatorHook
30+
newSchemaClientHook = emulatorHook
31+
}
32+
33+
func emulatorHook(ctx context.Context, params clientHookParams) ([]option.ClientOption, error) {
34+
if addr := os.Getenv("PUBSUB_EMULATOR_HOST"); addr != "" {
35+
return []option.ClientOption{
36+
option.WithEndpoint(addr),
37+
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
38+
option.WithoutAuthentication(),
39+
option.WithTelemetryDisabled(),
40+
internaloption.SkipDialSettingsValidation(),
41+
}, nil
42+
}
43+
return nil, nil
44+
}

pubsub/v2/pubsub.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ import (
2929
"cloud.google.com/go/pubsub/v2/internal"
3030
gax "github.com/googleapis/gax-go/v2"
3131
"google.golang.org/api/option"
32-
"google.golang.org/api/option/internaloption"
3332
"google.golang.org/grpc"
34-
"google.golang.org/grpc/credentials/insecure"
3533
"google.golang.org/grpc/keepalive"
3634
)
3735

@@ -168,18 +166,7 @@ func NewClientWithConfig(ctx context.Context, projectID string, config *ClientCo
168166
return nil, ErrEmptyProjectID
169167
}
170168
var o []option.ClientOption
171-
// Environment variables for gcloud emulator:
172-
// https://cloud.google.com/sdk/gcloud/reference/beta/emulators/pubsub/
173-
if addr := os.Getenv("PUBSUB_EMULATOR_HOST"); addr != "" {
174-
emulatorOpts := []option.ClientOption{
175-
option.WithEndpoint(addr),
176-
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
177-
option.WithoutAuthentication(),
178-
option.WithTelemetryDisabled(),
179-
internaloption.SkipDialSettingsValidation(),
180-
}
181-
opts = append(emulatorOpts, opts...)
182-
} else {
169+
if os.Getenv("PUBSUB_EMULATOR_HOST") == "" {
183170
numConns := runtime.GOMAXPROCS(0)
184171
if numConns > 4 {
185172
numConns = 4

0 commit comments

Comments
 (0)