Skip to content

Wrapping Transport #78

Closed
Closed

Description

@anuraaga

I was looking at how to wrap the client's Transport, for example to apply an auth token middleware at the HTTP level. With a normal http.Client, I would usually reassign it like this

client := &http.Client{}
client.Transport = auth.RoundTripper(client.Transport)

But I think that will prevent closing the httplb client properly

https://github.com/bufbuild/httplb/blob/main/client.go#L74

Would it make sense to keep a pointer to the original transport alongside Transport to allow wrapping the later with I guess conventional patterns? Or alternatively, add a client option to wrap Transport in a similar way?

Activity

  1. jhump commented on Apr 28, 2025

    @jhump
    Member

    @anuraaga, thanks for bringing attention to this. The way it would have to be done currently is to actually make a copy of the embedded client, mutate its transport field, and then use that client copy instead of the original embedded one. You may still need to keep a reference to the original *httplb.Client, just to close it (even though it's not what is directly being used for HTTP calls).

    But I just opened #79 to make this more intuitive, so you can use the same technique you'd usually use to wrap the transport with middleware.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions