Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type clientOptions struct {
maxEdgeTraversal int
cacheSizeMB int
maxRecvMsgSize int
grpcDialOptions []grpc.DialOption
namespace string
logger logr.Logger
validator StructValidator
Expand Down Expand Up @@ -189,6 +190,18 @@ func WithMaxRecvMsgSize(size int) ClientOpt {
}
}

// WithGRPCDialOption appends a custom grpc.DialOption applied when opening a
// remote (dgraph://) connection. It is the general escape hatch for gRPC dial
// settings the dedicated options do not cover — TLS transport credentials,
// interceptors, keepalive parameters, and so on. May be supplied multiple
// times; the options are applied in the order given, after any option implied
// by WithMaxRecvMsgSize. Ignored for embedded (file://) URIs.
func WithGRPCDialOption(opt grpc.DialOption) ClientOpt {
return func(o *clientOptions) {
o.grpcDialOptions = append(o.grpcDialOptions, opt)
}
}

// WithValidator sets a validator instance for struct validation.
// The validator will be used to validate structs before insert, upsert, and update operations.
// If no validator is provided, validation will be skipped.
Expand Down Expand Up @@ -279,16 +292,26 @@ func NewClient(uri string, opts ...ClientOpt) (Client, error) {
client.logger.V(2).Info("Opening new Dgraph connection", "uri", uri)
return dgo.Open(uri)
}
// Assemble any custom gRPC dial options. maxRecvMsgSize is folded
// into the same mechanism as WithGRPCDialOption so the two compose.
var dialOpts []grpc.DialOption
if options.maxRecvMsgSize > 0 {
dialOpts = append(dialOpts,
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(options.maxRecvMsgSize)))
}
dialOpts = append(dialOpts, options.grpcDialOptions...)
if len(dialOpts) > 0 {
endpoint, dgoOpts, err := parseDgraphURI(uri)
if err != nil {
return nil, err
}
dgoOpts = append(dgoOpts, dgo.WithGrpcOption(
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(options.maxRecvMsgSize))))
for _, opt := range dialOpts {
dgoOpts = append(dgoOpts, dgo.WithGrpcOption(opt))
}
factory = func() (*dgo.Dgraph, error) {
client.logger.V(2).Info("Opening new Dgraph connection",
"uri", uri, "maxRecvMsgSize", options.maxRecvMsgSize)
"uri", uri, "maxRecvMsgSize", options.maxRecvMsgSize,
"grpcDialOptions", len(options.grpcDialOptions))
return dgo.NewClient(endpoint, dgoOpts...)
}
}
Expand Down Expand Up @@ -430,9 +453,9 @@ func (c client) key() string {
if c.options.embeddingProvider != nil {
embeddingKey = fmt.Sprintf("%p", c.options.embeddingProvider)
}
return fmt.Sprintf("%s:%t:%d:%d:%d:%d:%s:%s:%s", c.uri, c.options.autoSchema, c.options.poolSize,
return fmt.Sprintf("%s:%t:%d:%d:%d:%d:%s:%s:%s:%d", c.uri, c.options.autoSchema, c.options.poolSize,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Cache key disambiguation for custom gRPC dial options is lossy (count-only), allowing collisions for different options and inconsistent behavior with documented embedded-mode ignore semantics.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At client.go, line 456:

<comment>Cache key disambiguation for custom gRPC dial options is lossy (count-only), allowing collisions for different options and inconsistent behavior with documented embedded-mode ignore semantics.</comment>

<file context>
@@ -430,9 +453,9 @@ func (c client) key() string {
 		embeddingKey = fmt.Sprintf("%p", c.options.embeddingProvider)
 	}
-	return fmt.Sprintf("%s:%t:%d:%d:%d:%d:%s:%s:%s", c.uri, c.options.autoSchema, c.options.poolSize,
+	return fmt.Sprintf("%s:%t:%d:%d:%d:%d:%s:%s:%s:%d", c.uri, c.options.autoSchema, c.options.poolSize,
 		c.options.maxEdgeTraversal, c.options.cacheSizeMB, c.options.maxRecvMsgSize,
-		c.options.namespace, validatorKey, embeddingKey)
</file context>

c.options.maxEdgeTraversal, c.options.cacheSizeMB, c.options.maxRecvMsgSize,
c.options.namespace, validatorKey, embeddingKey)
c.options.namespace, validatorKey, embeddingKey, len(c.options.grpcDialOptions))
}

// embeddingProvider implements the embeddingClient interface, exposing the
Expand Down
30 changes: 30 additions & 0 deletions dial_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/

package modusgraph

import (
"testing"

"google.golang.org/grpc"
)

func TestWithGRPCDialOptionAppends(t *testing.T) {
var o clientOptions
WithGRPCDialOption(grpc.WithUserAgent("a"))(&o)
WithGRPCDialOption(grpc.WithUserAgent("b"))(&o)
if got := len(o.grpcDialOptions); got != 2 {
t.Fatalf("expected 2 dial options, got %d", got)
}
}

func TestKeyDistinguishesGRPCDialOptions(t *testing.T) {
base := client{uri: "dgraph://localhost:9080"}
withOpt := client{uri: "dgraph://localhost:9080"}
WithGRPCDialOption(grpc.WithUserAgent("x"))(&withOpt.options)
if base.key() == withOpt.key() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: TestKeyDistinguishesGRPCDialOptions only validates 0 vs 1 dial option count, not different values, masking a cache-key collision bug.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At dial_options_test.go, line 27:

<comment>TestKeyDistinguishesGRPCDialOptions only validates 0 vs 1 dial option count, not different values, masking a cache-key collision bug.</comment>

<file context>
@@ -0,0 +1,30 @@
+	base := client{uri: "dgraph://localhost:9080"}
+	withOpt := client{uri: "dgraph://localhost:9080"}
+	WithGRPCDialOption(grpc.WithUserAgent("x"))(&withOpt.options)
+	if base.key() == withOpt.key() {
+		t.Fatal("client.key() must differ when grpcDialOptions differ, else clients dedup incorrectly")
+	}
</file context>

t.Fatal("client.key() must differ when grpcDialOptions differ, else clients dedup incorrectly")
}
}
Loading