Skip to content

Commit

Permalink
grpc: Add join Dial Option (#5861)
Browse files Browse the repository at this point in the history
  • Loading branch information
zasweq authored Dec 17, 2022
1 parent 70617b1 commit 54b7d03
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
36 changes: 36 additions & 0 deletions default_dial_option_server_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,39 @@ func (s) TestAddExtraServerOptions(t *testing.T) {
t.Fatalf("Unexpected len of extraServerOptions: %d != 0", len(extraServerOptions))
}
}

// TestJoinDialOption tests the join dial option. It configures a joined dial
// option with three individual dial options, and verifies that all three are
// successfully applied.
func (s) TestJoinDialOption(t *testing.T) {
const maxRecvSize = 998765
const initialWindowSize = 100
jdo := newJoinDialOption(WithTransportCredentials(insecure.NewCredentials()), WithReadBufferSize(maxRecvSize), WithInitialWindowSize(initialWindowSize))
cc, err := Dial("fake", jdo)
if err != nil {
t.Fatalf("Dialing with insecure credentials failed: %v", err)
}
defer cc.Close()
if cc.dopts.copts.ReadBufferSize != maxRecvSize {
t.Fatalf("Unexpected cc.dopts.copts.ReadBufferSize: %d != %d", cc.dopts.copts.ReadBufferSize, maxRecvSize)
}
if cc.dopts.copts.InitialWindowSize != initialWindowSize {
t.Fatalf("Unexpected cc.dopts.copts.InitialWindowSize: %d != %d", cc.dopts.copts.InitialWindowSize, initialWindowSize)
}
}

// TestJoinDialOption tests the join server option. It configures a joined
// server option with three individual server options, and verifies that all
// three are successfully applied.
func (s) TestJoinServerOption(t *testing.T) {
const maxRecvSize = 998765
const initialWindowSize = 100
jso := newJoinServerOption(Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize), InitialWindowSize(initialWindowSize))
s := NewServer(jso)
if s.opts.maxReceiveMessageSize != maxRecvSize {
t.Fatalf("Unexpected s.opts.maxReceiveMessageSize: %d != %d", s.opts.maxReceiveMessageSize, maxRecvSize)
}
if s.opts.initialWindowSize != initialWindowSize {
t.Fatalf("Unexpected s.opts.initialWindowSize: %d != %d", s.opts.initialWindowSize, initialWindowSize)
}
}
15 changes: 15 additions & 0 deletions dialoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func init() {
extraDialOptions = nil
}
internal.WithBinaryLogger = withBinaryLogger
internal.JoinDialOptions = newJoinDialOption
}

// dialOptions configure a Dial call. dialOptions are set by the DialOption
Expand Down Expand Up @@ -111,6 +112,20 @@ func newFuncDialOption(f func(*dialOptions)) *funcDialOption {
}
}

type joinDialOption struct {
opts []DialOption
}

func (jdo *joinDialOption) apply(do *dialOptions) {
for _, opt := range jdo.opts {
opt.apply(do)
}
}

func newJoinDialOption(opts ...DialOption) DialOption {
return &joinDialOption{opts: opts}
}

// WithWriteBufferSize determines how much data can be batched before doing a
// write on the wire. The corresponding memory allocation for this buffer will
// be twice the size to keep syscalls low. The default value for this buffer is
Expand Down
3 changes: 3 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ var (
// ClearGlobalDialOptions clears the array of extra DialOption. This
// method is useful in testing and benchmarking.
ClearGlobalDialOptions func()
// JoinDialOptions combines the dial options passed as arguments into a
// single dial option.
JoinDialOptions interface{} // func(...grpc.DialOption) grpc.DialOption
// JoinServerOptions combines the server options passed as arguments into a
// single server option.
JoinServerOptions interface{} // func(...grpc.ServerOption) grpc.ServerOption
Expand Down

0 comments on commit 54b7d03

Please sign in to comment.