revert handshaker changes
This commit is contained in:
32
server.go
32
server.go
@ -44,6 +44,7 @@ import (
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/transport"
|
||||
@ -85,7 +86,7 @@ type Server struct {
|
||||
}
|
||||
|
||||
type options struct {
|
||||
handshaker func(net.Conn) error
|
||||
creds []credentials.Credentials
|
||||
codec Codec
|
||||
maxConcurrentStreams uint32
|
||||
}
|
||||
@ -93,14 +94,6 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
// CustomCodec returns a ServerOption that sets a codec for message marshaling and unmarshaling.
|
||||
func CustomCodec(codec Codec) ServerOption {
|
||||
return func(o *options) {
|
||||
@ -116,6 +109,13 @@ func MaxConcurrentStreams(n uint32) ServerOption {
|
||||
}
|
||||
}
|
||||
|
||||
// Creds returns a ServerOption that sets credentials for server connections.
|
||||
func Creds(c credentials.Credentials) ServerOption {
|
||||
return func(o *options) {
|
||||
o.creds = append(o.creds, c)
|
||||
}
|
||||
}
|
||||
|
||||
// NewServer creates a gRPC server which has no service registered and has not
|
||||
// started to accept requests yet.
|
||||
func NewServer(opt ...ServerOption) *Server {
|
||||
@ -195,12 +195,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 {
|
||||
grpclog.Println("grpc: Server.Serve failed to complete handshake.")
|
||||
c.Close()
|
||||
continue
|
||||
for _, o := range s.opts.creds {
|
||||
if creds, ok := o.(credentials.TransportAuthenticator); ok {
|
||||
c, err = creds.ServerHandshake(c)
|
||||
if err != nil {
|
||||
grpclog.Println("grpc: Server.Serve failed to complete security handshake.")
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
s.mu.Lock()
|
||||
|
Reference in New Issue
Block a user