|
14 | 14 | package pubsublite
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "net/url" |
17 | 20 | "time"
|
18 | 21 |
|
| 22 | + "google.golang.org/api/option" |
| 23 | + "google.golang.org/api/option/internaloption" |
19 | 24 | "google.golang.org/grpc/codes"
|
| 25 | + "google.golang.org/grpc/metadata" |
20 | 26 | "google.golang.org/grpc/status"
|
21 | 27 |
|
| 28 | + vkit "cloud.google.com/go/pubsublite/apiv1" |
22 | 29 | gax "github.com/googleapis/gax-go/v2"
|
23 | 30 | )
|
24 | 31 |
|
@@ -98,3 +105,70 @@ func isRetryableStreamError(err error, isEligible func(codes.Code) bool) bool {
|
98 | 105 | }
|
99 | 106 | return isEligible(s.Code())
|
100 | 107 | }
|
| 108 | + |
| 109 | +const ( |
| 110 | + pubsubLiteDefaultEndpoint = "-pubsublite.googleapis.com:443" |
| 111 | + routingMetadataHeader = "x-goog-request-params" |
| 112 | +) |
| 113 | + |
| 114 | +func defaultClientOptions(region string) []option.ClientOption { |
| 115 | + return []option.ClientOption{ |
| 116 | + internaloption.WithDefaultEndpoint(region + pubsubLiteDefaultEndpoint), |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func newAdminClient(ctx context.Context, region string, opts ...option.ClientOption) (*vkit.AdminClient, error) { |
| 121 | + if err := validateRegion(region); err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + options := append(defaultClientOptions(region), opts...) |
| 125 | + return vkit.NewAdminClient(ctx, options...) |
| 126 | +} |
| 127 | + |
| 128 | +func newPublisherClient(ctx context.Context, region string, opts ...option.ClientOption) (*vkit.PublisherClient, error) { |
| 129 | + if err := validateRegion(region); err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + options := append(defaultClientOptions(region), opts...) |
| 133 | + return vkit.NewPublisherClient(ctx, options...) |
| 134 | +} |
| 135 | + |
| 136 | +func newSubscriberClient(ctx context.Context, region string, opts ...option.ClientOption) (*vkit.SubscriberClient, error) { |
| 137 | + if err := validateRegion(region); err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + options := append(defaultClientOptions(region), opts...) |
| 141 | + return vkit.NewSubscriberClient(ctx, options...) |
| 142 | +} |
| 143 | + |
| 144 | +func newCursorClient(ctx context.Context, region string, opts ...option.ClientOption) (*vkit.CursorClient, error) { |
| 145 | + if err := validateRegion(region); err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + options := append(defaultClientOptions(region), opts...) |
| 149 | + return vkit.NewCursorClient(ctx, options...) |
| 150 | +} |
| 151 | + |
| 152 | +func newPartitionAssignmentClient(ctx context.Context, region string, opts ...option.ClientOption) (*vkit.PartitionAssignmentClient, error) { |
| 153 | + if err := validateRegion(region); err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + options := append(defaultClientOptions(region), opts...) |
| 157 | + return vkit.NewPartitionAssignmentClient(ctx, options...) |
| 158 | +} |
| 159 | + |
| 160 | +func addTopicRoutingMetadata(ctx context.Context, topic TopicPath, partition int) context.Context { |
| 161 | + md, _ := metadata.FromOutgoingContext(ctx) |
| 162 | + md = md.Copy() |
| 163 | + val := fmt.Sprintf("partition=%d&topic=%s", partition, url.QueryEscape(topic.String())) |
| 164 | + md[routingMetadataHeader] = append(md[routingMetadataHeader], val) |
| 165 | + return metadata.NewOutgoingContext(ctx, md) |
| 166 | +} |
| 167 | + |
| 168 | +func addSubscriptionRoutingMetadata(ctx context.Context, subs SubscriptionPath, partition int) context.Context { |
| 169 | + md, _ := metadata.FromOutgoingContext(ctx) |
| 170 | + md = md.Copy() |
| 171 | + val := fmt.Sprintf("partition=%d&subscription=%s", partition, url.QueryEscape(subs.String())) |
| 172 | + md[routingMetadataHeader] = append(md[routingMetadataHeader], val) |
| 173 | + return metadata.NewOutgoingContext(ctx, md) |
| 174 | +} |
0 commit comments