From c6f6e4701d52b9be5044c30e785f5fa980eaa359 Mon Sep 17 00:00:00 2001 From: iamqizhao Date: Thu, 30 Apr 2015 16:30:48 -0700 Subject: [PATCH 1/2] Add a handshaker option to server --- server.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 79f17f3d..0d9a5915 100644 --- a/server.go +++ b/server.go @@ -85,6 +85,7 @@ type Server struct { } type options struct { + handshaker func(net.Conn) error codec Codec maxConcurrentStreams uint32 } @@ -92,6 +93,12 @@ type options struct { // A ServerOption sets options. type ServerOption func(*options) +func Handshaker(f func(net.Conn) error) ServerOption { + return func(o *options) { + o.handshaker = f + } +} + func CustomCodec(codec Codec) ServerOption { return func(o *options) { o.codec = codec @@ -185,7 +192,14 @@ func (s *Server) Serve(lis net.Listener) error { if err != nil { return err } - + // Perform handshaking if it is required. + if s.opts.handshaker != nil { + if err := s.opts.handshaker(c); err != nil { + log.Println("grpc: Server.Serve failed to complete handshake.") + c.Close() + continue + } + } s.mu.Lock() if s.conns == nil { s.mu.Unlock() From c3da701487d1873610b7b580b343d645b3bfb6c5 Mon Sep 17 00:00:00 2001 From: iamqizhao Date: Thu, 30 Apr 2015 16:36:21 -0700 Subject: [PATCH 2/2] gofmt -w --- server.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 0d9a5915..700e4563 100644 --- a/server.go +++ b/server.go @@ -93,19 +93,22 @@ type options struct { // A ServerOption sets options. type ServerOption func(*options) +// Handshaker returns a ServerOption that specifies a function to perform user-specified +// handshaking on the connection before it becomes usable for gRPC. func Handshaker(f func(net.Conn) error) ServerOption { return func(o *options) { o.handshaker = f } } +// CustomeCodec returns a ServerOption that sets a codec for message marshaling and unmarshaling. func CustomCodec(codec Codec) ServerOption { return func(o *options) { o.codec = codec } } -// MaxConcurrentStreams returns an Option that will apply a limit on the number +// MaxConcurrentStreams returns a ServerOption that will apply a limit on the number // of concurrent streams to each ServerTransport. func MaxConcurrentStreams(n uint32) ServerOption { return func(o *options) {