GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Pub/Sub client should allow connecting to different backends without changes in code · Issue #2731 · googleapis/google-cloud-java

Pub/Sub client should allow connecting to different backends without changes in code #2731

New issue
Copy link
New issue
Copy link
Closed
Closed
Pub/Sub client should allow connecting to different backends without changes in code#2731
Copy link
Assignees
pongad
Labels
api: pubsubIssues related to the Pub/Sub API.priority: p2Moderately-important priority. Fix may not be included in next release.triaged for GAtype: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@kir-titievsky
kir-titievsky
opened on Dec 21, 2017
Issue body actions

Current Pub/Sub client can be made to use an emulator service instead of pubsub.googleapis.com as the API service. The way this is implemented, however, appears to require the users to both set an environment variable AND change the client code. It seems to me this makes hermetic testing difficult. I think, as an application developer, I would be much happier if I could test my code against an emulator or an alternate backend without touching the code itself. So the feature request here is that:

The code required to use the emulator currently looks kind of like this:

$export PUBSUB_EMULATOR_HOST=10.0.0.200:1234
 String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
    ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext(true).build();
    try {
      TransportChannelProvider channelProvider =
          FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
      CredentialsProvider credentialsProvider = NoCredentialsProvider.create();

      TopicName topicName = TopicName.of("my-project-id", "my-topic-id");
      // Set the channel and credentials provider when creating a `Publisher`.
      // Similarly for Subscriber
      Publisher publisher =
          Publisher.newBuilder(topicName)
              .setChannelProvider(channelProvider)
              .setCredentialsProvider(credentialsProvider)
              .build();

Might we make it look identical to "normal production code" used to accomplish the same thing? That is:

      TopicName topicName = TopicName.of("my-project-id", "my-topic-id");
      // Create a publisher instance with default settings bound to the topic
      publisher = Publisher.newBuilder(topicName).build();

and all the channel building would happen automatically? I presume all but the hostport line are already part of the client library code base.

Activity

  1. andreencarnacao-focusbc commented on Dec 22, 2017

    @andreencarnacao-focusbc
    andreencarnacao-focusbc
    on Dec 22, 2017
    More actions

    The problem seems to be that when google client library tries to instantiate the default channel does not take into account if the environment variable PUBSUB_EMULATOR_HOST is setted. I am going to do a pull request, but further development on top of it might be needed

  2. danoscarmike
    added
    api: pubsubIssues related to the Pub/Sub API.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on Jan 3, 2018
  3. danoscarmike
    assigned
    garrettjonesgoogle
    and
    pongad
    and unassigned
    garrettjonesgoogle
    on Jan 3, 2018
  4. pongad commented on Jan 3, 2018

    @pongad
    pongad
    on Jan 3, 2018
    Contributor
    More actions

    Some history: we thought it was somewhat dangerous to do this. For example, a small mistake like PUBSUB_EMULATOR_HOTS=1.2.3.4 delete-all-topics might delete a bunch of your production topics. It's a judgement call whether the benefits outweighs the risk, but I sympathize with the view that auto-detecting the env var is a bit of a foot-gun.

    I think we should go with @jba's suggestion: we'll add a detectEmulator method. The method will look up the env var and use the emulator is the var is set. If the var is not set, we'll connect to production host. Users who are OK with the aforementioned risk can simply call the method unconditionally. In this way, the proverbial gun would at least have safety on.

    @kir-titievsky @garrettjonesgoogle Are you OK with this? @andreencarnacao-focusbc Please let us know what you think.

  5. kir-titievsky commented on Jan 3, 2018

    @kir-titievsky
    kir-titievsky
    on Jan 3, 2018
    Author
    More actions

    I think that would work, with couple questions:

    1. Could we name the variable something more generic than "emulator": e.g. detectServiceHost?
    2. Is there any special machinery required to make this work with both IP addresses and hosnames? For example, if a user might want to set the emulator to point to pubsub.googleapis.com explicitly, using the environment variable when testing multiple scenarios.
  6. garrettjonesgoogle commented on Jan 4, 2018

    @garrettjonesgoogle
    garrettjonesgoogle
    on Jan 4, 2018
    Contributor
    More actions

    I second @kir-titievsky 's suggestion of a more general approach.

  7. pongad commented on Jan 4, 2018

    @pongad
    pongad
    on Jan 4, 2018
    Contributor
    More actions

    This is a little difficult. The emulator "speaks" plain text and doesn't do SSL encryption. So, either the method automatically sets plain text, assuming that the host in an emulator, or we'll need more than one method.

    I have a few designs in my head:

    Publisher.newBuilder(topicName).detectEmulator().build();

    where detectEmulator reads the env var. If the env var isn't empty, we turn off encryption and set the host.

    Publisher.newBuilder(topicName)
      .detectEmulator(System.getenv("PUBSUB_EMULATOR_HOST"))
      .build();

    Similar to (1) but we don't hard code what the variable is. If the string isn't empty, turn off encryption and set the host.

    String var = System.getenv("PUBSUB_EMULATOR_HOST");
    Publisher.newBuilder(topicName)
      .setHost(var)
      .setPlainText(var != null && !var.isEmpty())
      .build();

    Seems a little weird. setHost is a general-looking method it should error if var is empty or null.

    String var = System.getenv("PUBSUB_EMULATOR_HOST");
    Publisher.Builder builder = Publisher.newBuilder(topicName);
    if (var != null && !var.isEmpty()) {
      builder.setHost(var).setPlaintext(true);
    }
    builder.build();

    This works, but it's only marginally better than what we have right now.
    Adding convenience methods like this is a bit of a feature duplication, and too much duplication leads to confusion. So, the method has to be convenient enough to justify the cost.

    My opinion is still that (3) and (4) don't add enough value to justify the duplication. (1) and (2) might; they add more convenience and specific enough to not add much confusion.

    Is there any other design I'm missing?

  8. kir-titievsky commented on Jan 10, 2018

    @kir-titievsky
    kir-titievsky
    on Jan 10, 2018
    Author
    More actions

    Fix post GA.

  9. danoscarmike
    added
    triaged for GA
    on Jan 11, 2018
  10. pongad commented on Feb 1, 2018

    @pongad
    pongad
    on Feb 1, 2018
    Contributor
    More actions

    This feature request is already in the feature backlog wiki. I'll close this for now.

  11. pongad
    closed this as completedon Feb 1, 2018
  12. theacodes
    unassigned
    pongad
    on Sep 28, 2018
  13. oande commented on May 20, 2020

    @oande
    oande
    on May 20, 2020
    More actions

    Sorry for resurrecting this issue @pongad @kir-titievsky , but I couldn't find any news regarding this "foot-gun". 🙂I think it'd save a bit of boilerplate code.

    we thought it was somewhat dangerous to do this. For example, a small mistake like PUBSUB_EMULATOR_HOTS=1.2.3.4 delete-all-topics might delete a bunch of your production topics.

    If I understand this correctly, the perceived risk essentially boils down to developers who might use the emulator env var with production credentials at the same time? Perhaps the detectEmulator method could be documented with a warning about that

  14. JustinBeckwith
    assigned
    pongad
    on Feb 1, 2021
  15. suztomo
    added a commit that references this issue on Mar 9, 2026

    chore: add add samples for sessionID support in Load jobs (#2731)

    fbb2bbc
  16. chingor13
    added 3 commits that reference this issue on Mar 24, 2026

    fix: only append `.api.grpc` suffix to group id if the artifact id st…

    b791c84

    chore(main): release 2.40.1 (#2729)

    5697127

    test(deps): update dependency org.junit:junit-bom to v5.11.1 (#2731)

    1dbb366
  17. meltsufin
    added a commit that references this issue on May 8, 2026

    Revert "deps: update googleapis/google-cloud-java action to v1.86.0 (#…

    79b176c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @pongad
    pongad

Labels

api: pubsubIssues related to the Pub/Sub API.priority: p2Moderately-important priority. Fix may not be included in next release.triaged for GAtype: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.