55 "crypto/tls"
66 "crypto/x509"
77 "fmt"
8+ "net"
89 "os"
910 "sync"
1011
@@ -29,15 +30,16 @@ type PreStartHook interface {
2930}
3031
3132type Server struct {
32- options * GRPCServerOptions
33- authenticators []authn.Authenticator
34- authorizers []authz.Authorizer
35- services map [types.CloudEventsDataType ]server.Service
36- hooks []PreStartHook
33+ options * GRPCServerOptions
34+ authenticators []authn.Authenticator
35+ authorizers []authz.Authorizer
36+ cloudeventServices map [types.CloudEventsDataType ]server.Service
37+ grpcService []func (* grpc.Server )
38+ hooks []PreStartHook
3739}
3840
3941func NewServer (opt * GRPCServerOptions ) * Server {
40- return & Server {options : opt , services : make (map [types.CloudEventsDataType ]server.Service )}
42+ return & Server {options : opt , cloudeventServices : make (map [types.CloudEventsDataType ]server.Service )}
4143}
4244
4345func (s * Server ) WithAuthenticator (authenticator authn.Authenticator ) * Server {
@@ -51,7 +53,7 @@ func (s *Server) WithAuthorizer(authorizer authz.Authorizer) *Server {
5153}
5254
5355func (s * Server ) WithService (t types.CloudEventsDataType , service server.Service ) * Server {
54- s .services [t ] = service
56+ s .cloudeventServices [t ] = service
5557 return s
5658}
5759
@@ -60,6 +62,11 @@ func (s *Server) WithPreStartHooks(hooks ...PreStartHook) *Server {
6062 return s
6163}
6264
65+ func (s * Server ) WithGRPCService (fn func (* grpc.Server )) * Server {
66+ s .grpcService = append (s .grpcService , fn )
67+ return s
68+ }
69+
6370func (s * Server ) Run (ctx context.Context ) error {
6471 var grpcServerOptions []grpc.ServerOption
6572 grpcServerOptions = append (grpcServerOptions , grpc .MaxRecvMsgSize (s .options .MaxReceiveMessageSize ))
@@ -116,19 +123,38 @@ func (s *Server) Run(ctx context.Context) error {
116123 newAuthzStreamInterceptor (s .authorizers ... )))
117124
118125 grpcServer := grpc .NewServer (grpcServerOptions ... )
119- grpcEventServer := grpcserver .NewGRPCBroker (grpcServer )
120126
121- for t , service := range s .services {
127+ // Register the CloudEventServiceServer
128+ grpcEventServer := grpcserver .NewGRPCBroker (grpcServer )
129+ for t , service := range s .cloudeventServices {
122130 grpcEventServer .RegisterService (t , service )
123131 }
124132
133+ // Register other grpc services
134+ for _ , fn := range s .grpcService {
135+ fn (grpcServer )
136+ }
137+
125138 // start hook
126139 for _ , hook := range s .hooks {
127140 hook .Run (ctx )
128141 }
129142
130- go grpcEventServer .Start (ctx , ":" + s .options .ServerBindPort )
143+ // start grpc server
144+ lis , err := net .Listen ("tcp" , ":" + s .options .ServerBindPort )
145+ if err != nil {
146+ return fmt .Errorf ("failed to listen: %v" , err )
147+ }
148+ go func () {
149+ if err := grpcServer .Serve (lis ); err != nil {
150+ klog .Errorf ("failed to serve gRPC broker: %v" , err )
151+ }
152+ }()
153+
131154 <- ctx .Done ()
155+
156+ klog .Infof ("Shutting down gRPC server" )
157+ grpcServer .GracefulStop ()
132158 return nil
133159}
134160
0 commit comments