Skip to content

Commit 739db44

Browse files
committed
refactor
1 parent 945b6b2 commit 739db44

6 files changed

Lines changed: 24 additions & 64 deletions

File tree

storage/grpc_client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type grpcStorageClient struct {
127127

128128
// configFeatureAttributes tracks client-level features that are enabled for this
129129
// client instance, represented as an 8-bit bitmask.
130-
configFeatureAttributes uint8
130+
configFeatureAttributes uint32
131131
}
132132

133133
func enableClientMetrics(ctx context.Context, s *settings, config storageConfig) (*metricsContext, error) {
@@ -251,13 +251,13 @@ func (c *grpcStorageClient) prepareDirectPathMetadata(ctx context.Context, targe
251251
features |= c.configFeatureAttributes
252252
// Merge all existing headers for this key from metadata.
253253
for _, existing := range md[featureTrackerHeaderName] {
254-
if decoded, err := decodeUint8(existing); err == nil {
254+
if decoded, err := decodeUint32(existing); err == nil {
255255
features |= decoded
256256
}
257257
}
258258

259259
if features > 0 {
260-
md.Set(featureTrackerHeaderName, encodeUint8(features))
260+
md.Set(featureTrackerHeaderName, encodeUint32(features))
261261
}
262262

263263
return metadata.NewOutgoingContext(ctx, md), nil

storage/grpc_client_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,24 +500,24 @@ func TestPrepareDirectPathMetadata(t *testing.T) {
500500
func TestPrepareDirectPathMetadata_FeatureTracking(t *testing.T) {
501501
tests := []struct {
502502
desc string
503-
configFeatures uint8
504-
contextFeatures []featureCode
505-
wantFeatures uint8
503+
configFeatures uint32
504+
contextFeatures []trackedFeature
505+
wantFeatures uint32
506506
}{
507507
{
508508
desc: "no features",
509509
wantFeatures: 0,
510510
},
511511
{
512512
desc: "config features only",
513-
configFeatures: uint8(featurePCU),
514-
wantFeatures: uint8(featurePCU),
513+
configFeatures: uint32(1 << featurePCU),
514+
wantFeatures: uint32(1 << featurePCU),
515515
},
516516
{
517517
desc: "merged features",
518-
configFeatures: uint8(featurePCU),
519-
contextFeatures: []featureCode{featureMultiStream},
520-
wantFeatures: uint8(featurePCU) | uint8(featureMultiStream),
518+
configFeatures: uint32(1 << featurePCU),
519+
contextFeatures: []trackedFeature{featureMultistreamInMRD},
520+
wantFeatures: uint32(1<<featurePCU) | uint32(1<<featureMultistreamInMRD),
521521
},
522522
}
523523

@@ -555,7 +555,7 @@ func TestPrepareDirectPathMetadata_FeatureTracking(t *testing.T) {
555555
t.Fatalf("features header missing, want %d", tc.wantFeatures)
556556
}
557557

558-
decoded, err := decodeUint8(got[0])
558+
decoded, err := decodeUint32(got[0])
559559
if err != nil {
560560
t.Fatalf("failed to decode features: %v", err)
561561
}

storage/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (o *ObjectHandle) NewMultiRangeDownloader(ctx context.Context, opts ...MRDO
266266
opt.apply(params)
267267
}
268268
if params.minConnections > 1 || params.maxConnections > 1 {
269-
spanCtx = addFeatureAttributes(spanCtx, featureMultiStream)
269+
spanCtx = addFeatureAttributes(spanCtx, featureMultistreamInMRD)
270270
}
271271
// This call will return the *MultiRangeDownloader with the .impl field set.
272272
return o.c.tc.NewMultiRangeDownloader(spanCtx, params, storageOpts...)

storage/tracked_features.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package storage
1616

1717
// trackedFeature represents a specific client feature being tracked, represented
1818
// as a bit in a bitmask. Each feature corresponds to a specific bit position.
19-
type trackedFeature uint
19+
type trackedFeature uint32
2020

2121
const (
2222
featureMultistreamInMRD trackedFeature = 0

storage/tracker.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,47 @@ package storage
1616

1717
import (
1818
"context"
19-
"encoding/base64"
2019

2120
"github.com/googleapis/gax-go/v2/callctx"
2221
)
2322

2423
const featureTrackerHeaderName = "x-goog-storage-go-features"
2524

26-
// featureCode represents a specific client feature being tracked, represented
27-
// as a bit in a bitmask.
28-
type featureCode uint8
29-
30-
const (
31-
featureMultiStream featureCode = 1
32-
featurePCU featureCode = 2
33-
)
34-
3525
// addFeatureAttributes adds the specified feature codes to the context.
3626
// Features are stored as a bitmask in the callctx headers and will be
3727
// injected into the outgoing request headers by the transport.
38-
func addFeatureAttributes(ctx context.Context, features ...featureCode) context.Context {
28+
func addFeatureAttributes(ctx context.Context, features ...trackedFeature) context.Context {
3929
if len(features) == 0 {
4030
return ctx
4131
}
4232

4333
current := getFeatureAttributes(ctx)
4434
updated := current
4535
for _, f := range features {
46-
updated |= uint8(f)
36+
updated |= (1 << f)
4737
}
4838

4939
if updated == current {
5040
return ctx
5141
}
5242

53-
return callctx.SetHeaders(ctx, featureTrackerHeaderName, encodeUint8(updated))
43+
return callctx.SetHeaders(ctx, featureTrackerHeaderName, encodeUint32(uint32(updated)))
5444
}
5545

5646
// getFeatureAttributes extracts and merges all feature attributes present in the context.
5747
// It returns a bitmask represented as a uint8.
58-
func getFeatureAttributes(ctx context.Context) uint8 {
48+
func getFeatureAttributes(ctx context.Context) uint32 {
5949
ctxHeaders := callctx.HeadersFromContext(ctx)
6050
if vals := ctxHeaders[featureTrackerHeaderName]; len(vals) > 0 {
6151
// If multiple values are present in the context (e.g. from nested calls),
6252
// merge them into a single bitmask.
63-
var merged uint8
53+
var merged uint32
6454
for _, v := range vals {
65-
if decoded, err := decodeUint8(v); err == nil {
55+
if decoded, err := decodeUint32(v); err == nil {
6656
merged |= decoded
6757
}
6858
}
6959
return merged
7060
}
7161
return 0
7262
}
73-
74-
// encodeUint8 encodes a uint8 bitmask into a base64 string for header injection.
75-
func encodeUint8(u uint8) string {
76-
return base64.StdEncoding.EncodeToString([]byte{u})
77-
}
78-
79-
// decodeUint8 decodes a base64 string back into a uint8 bitmask.
80-
func decodeUint8(s string) (uint8, error) {
81-
b, err := base64.StdEncoding.DecodeString(s)
82-
if err != nil || len(b) < 1 {
83-
return 0, err
84-
}
85-
return b[0], nil
86-
}

storage/tracker_test.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ func TestAddFeatureAttributes(t *testing.T) {
2828
}
2929

3030
// Add a single feature.
31-
ctx = addFeatureAttributes(ctx, featureMultiStream)
32-
if got := getFeatureAttributes(ctx); got != uint8(featureMultiStream) {
33-
t.Errorf("getFeatureAttributes(MultiStream) = %d; want %d", got, featureMultiStream)
31+
ctx = addFeatureAttributes(ctx, featureMultistreamInMRD)
32+
if got := getFeatureAttributes(ctx); got != uint32(1<<featureMultistreamInMRD) {
33+
t.Errorf("getFeatureAttributes(MultiStream) = %d; want %d", got, featureMultistreamInMRD)
3434
}
3535

3636
// Add another feature (merge).
3737
ctx = addFeatureAttributes(ctx, featurePCU)
38-
want := uint8(featureMultiStream) | uint8(featurePCU)
38+
want := uint32(1<<featureMultistreamInMRD) | uint32(1<<featurePCU)
3939
if got := getFeatureAttributes(ctx); got != want {
4040
t.Errorf("getFeatureAttributes(MultiStream | PCU) = %d; want %d", got, want)
4141
}
@@ -46,19 +46,3 @@ func TestAddFeatureAttributes(t *testing.T) {
4646
t.Errorf("getFeatureAttributes(MultiStream | PCU | PCU) = %d; want %d", got, want)
4747
}
4848
}
49-
50-
func TestEncodeDecodeUint8(t *testing.T) {
51-
tests := []uint8{0, 1, 2, 127, 128, 255}
52-
53-
for _, want := range tests {
54-
encoded := encodeUint8(want)
55-
got, err := decodeUint8(encoded)
56-
if err != nil {
57-
t.Errorf("decodeUint8(%q) error: %v", encoded, err)
58-
continue
59-
}
60-
if got != want {
61-
t.Errorf("decodeUint8(encodeUint8(%d)) = %d; want %d", want, got, want)
62-
}
63-
}
64-
}

0 commit comments

Comments
 (0)