Skip to content

Commit f482670

Browse files
committed
feat: added new grpc sync config option
Added a new config option `maxMsgSize` which will allow users to override default message size 4Mb. Signed-off-by: Pradeep Mishra <pradeepbbl@gmail.com> Signed-off-by: pradeepbbl <pradeepbbl@gmail.com>
1 parent 8f1dbba commit f482670

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

core/pkg/sync/builder/syncbuilder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func (sb *SyncBuilder) newGRPC(config sync.SourceConfig, logger *logger.Logger)
166166
ProviderID: config.ProviderID,
167167
Secure: config.TLS,
168168
Selector: config.Selector,
169+
MaxMsgSize: config.MaxMsgSize,
169170
}
170171
}
171172

core/pkg/sync/builder/syncbuilder_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ func Test_SyncsFromFromConfig(t *testing.T) {
173173
},
174174
wantErr: false,
175175
},
176+
{
177+
name: "grpc-with-msg-size",
178+
args: args{
179+
logger: lg,
180+
sources: []sync.SourceConfig{
181+
{
182+
URI: "grpc://host:port",
183+
Provider: syncProviderGrpc,
184+
ProviderID: "myapp",
185+
CertPath: "/tmp/ca.cert",
186+
Selector: "source=database",
187+
MaxMsgSize: 10,
188+
},
189+
},
190+
},
191+
wantSyncs: []sync.ISync{
192+
&grpc.Sync{},
193+
},
194+
wantErr: false,
195+
},
176196
{
177197
name: "combined",
178198
injectFunc: func(builder *SyncBuilder) {

core/pkg/sync/grpc/grpc_sync.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Sync struct {
4848
Secure bool
4949
Selector string
5050
URI string
51+
MaxMsgSize int
5152

5253
client FlagSyncServiceClient
5354
ready bool
@@ -62,7 +63,16 @@ func (g *Sync) Init(_ context.Context) error {
6263
}
6364

6465
// Derive reusable client connection
65-
rpcCon, err := grpc.NewClient(g.URI, grpc.WithTransportCredentials(tCredentials))
66+
// Set MaxMsgSize if passed
67+
var rpcCon *grpc.ClientConn
68+
69+
if g.MaxMsgSize > 0 {
70+
g.Logger.Info(fmt.Sprintf("setting max receive message size %d bytes default 4MB", g.MaxMsgSize))
71+
rpcCon, err = grpc.NewClient(g.URI, grpc.WithTransportCredentials(tCredentials), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(g.MaxMsgSize)))
72+
} else {
73+
rpcCon, err = grpc.NewClient(g.URI, grpc.WithTransportCredentials(tCredentials))
74+
}
75+
6676
if err != nil {
6777
err := fmt.Errorf("error initiating grpc client connection: %w", err)
6878
g.Logger.Error(err.Error())

core/pkg/sync/grpc/grpc_sync_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"google.golang.org/grpc/credentials"
2525
"google.golang.org/grpc/credentials/insecure"
2626
"google.golang.org/grpc/test/bufconn"
27+
"go.uber.org/zap"
28+
"go.uber.org/zap/zaptest/observer"
2729
)
2830

2931
func Test_InitWithMockCredentialBuilder(t *testing.T) {
@@ -78,6 +80,32 @@ func Test_InitWithMockCredentialBuilder(t *testing.T) {
7880
}
7981
}
8082

83+
func Test_InitWithSizeOverride(t *testing.T) {
84+
observedZapCore, observedLogs := observer.New(zap.InfoLevel)
85+
observedLogger := zap.New(observedZapCore)
86+
87+
mockCtrl := gomock.NewController(t)
88+
mockCredentialBulder := credendialsmock.NewMockBuilder(mockCtrl)
89+
90+
mockCredentialBulder.EXPECT().
91+
Build(gomock.Any(), gomock.Any()).
92+
Return(insecure.NewCredentials(), nil)
93+
94+
95+
grpcSync := Sync{
96+
URI: "grpc-target",
97+
Logger: logger.NewLogger(observedLogger, false),
98+
CredentialBuilder: mockCredentialBulder,
99+
MaxMsgSize: 10,
100+
101+
}
102+
103+
grpcSync.Init(context.Background())
104+
105+
require.Equal(t, "setting max receive message size 10 bytes default 4MB", observedLogs.All()[0].Message)
106+
107+
}
108+
81109
func Test_ReSyncTests(t *testing.T) {
82110
const target = "localBufCon"
83111

core/pkg/sync/isync.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ type SourceConfig struct {
7373
ProviderID string `json:"providerID,omitempty"`
7474
Selector string `json:"selector,omitempty"`
7575
Interval uint32 `json:"interval,omitempty"`
76+
MaxMsgSize int `json:"maxMsgSize,omitempty"`
7677
}

docs/reference/sync-configuration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Alternatively, these configurations can be passed to flagd via config file, spec
3838
| providerID | optional `string` | Value binds to grpc connection's providerID field. gRPC server implementations may use this to identify connecting flagd instance |
3939
| selector | optional `string` | Value binds to grpc connection's selector field. gRPC server implementations may use this to filter flag configurations |
4040
| certPath | optional `string` | Used for grpcs sync when TLS certificate is needed. If not provided, system certificates will be used for TLS connection |
41+
| maxMsgSize | optional `int` | Used for grpcs sync to set max receive message size (in bytes) e.g. 5242880 for 5MB. If not provided, used the deafult [4MB](https://pkg.go.dev/google.golang.org#grpc#MaxCallRecvMsgSize) |
4142

4243
The `uri` field values **do not** follow the [URI patterns](#uri-patterns). The provider type is instead derived
4344
from the `provider` field. Only exception is the remote provider where `http(s)://` is expected by default. Incorrect
@@ -64,6 +65,7 @@ Startup command:
6465
{"uri":"https://secure-remote/basic-auth","provider":"http","authHeader":"Basic dXNlcjpwYXNz"},
6566
{"uri":"default/my-flag-config","provider":"kubernetes"},
6667
{"uri":"grpc-source:8080","provider":"grpc"},
68+
{"uri":"my-flag-source:8080","provider":"grpc", "maxMsgSize": 5242880},
6769
{"uri":"my-flag-source:8080","provider":"grpc", "certPath": "/certs/ca.cert", "tls": true, "providerID": "flagd-weatherapp-sidecar", "selector": "source=database,app=weatherapp"}]'
6870
```
6971

@@ -80,6 +82,9 @@ sources:
8082
provider: kubernetes
8183
- uri: my-flag-source:8080
8284
provider: grpc
85+
- uri: my-flag-source:8080
86+
provider: grpc
87+
maxMsgSize: 5242880
8388
- uri: my-flag-source:8080
8489
provider: grpc
8590
certPath: /certs/ca.cert

0 commit comments

Comments
 (0)