Apply commented improvements

- skip shutdown of gRPC server if failed to start
- minor refactoring
This commit is contained in:
Peter 2020-05-10 12:26:59 +02:00 committed by baez90
parent def844c5fe
commit 38f8ff319e
2 changed files with 29 additions and 11 deletions

View file

@ -10,17 +10,26 @@ type endpointsServer struct {
} }
func (e endpointsServer) GetEndpoints(_ context.Context, _ *GetEndpointsRequest) (*GetEndpointsResponse, error) { func (e endpointsServer) GetEndpoints(_ context.Context, _ *GetEndpointsRequest) (*GetEndpointsResponse, error) {
eps := make([]*Endpoint, 0) eps := rpcEndpointsFromEndpoints(e.endpointsManager.StartedEndpoints())
for _, ep := range e.endpointsManager.StartedEndpoints() { return &GetEndpointsResponse{
eps = append(eps, &Endpoint{ Endpoints: *eps,
}, nil
}
func rpcEndpointsFromEndpoints(eps []endpoints.Endpoint) *[]*Endpoint {
out := make([]*Endpoint, 0)
for _, ep := range eps {
out = append(out, rpcEndpointFromEndpoint(ep))
}
return &out
}
func rpcEndpointFromEndpoint(ep endpoints.Endpoint) *Endpoint {
return &Endpoint{
Id: ep.Id().String(), Id: ep.Id().String(),
Name: ep.Name(), Name: ep.Name(),
Handler: ep.Handler(), Handler: ep.Handler(),
ListenAddress: ep.Listen(), ListenAddress: ep.Listen(),
Port: int32(ep.Port()), Port: int32(ep.Port()),
})
} }
return &GetEndpointsResponse{
Endpoints: eps,
}, nil
} }

View file

@ -23,6 +23,7 @@ type inetmockAPI struct {
endpointManager endpoints.EndpointManager endpointManager endpoints.EndpointManager
registry plugins.HandlerRegistry registry plugins.HandlerRegistry
logger logging.Logger logger logging.Logger
serverRunning bool
} }
func NewINetMockAPI( func NewINetMockAPI(
@ -56,6 +57,12 @@ func (i *inetmockAPI) StartServer() (err error) {
} }
func (i *inetmockAPI) StopServer() { func (i *inetmockAPI) StopServer() {
if !i.serverRunning {
i.logger.Info(
"Skipping API server shutdown because server is not running",
)
return
}
gracefulStopChan := make(chan struct{}) gracefulStopChan := make(chan struct{})
go func() { go func() {
i.server.GracefulStop() i.server.GracefulStop()
@ -70,7 +77,9 @@ func (i *inetmockAPI) StopServer() {
} }
func (i *inetmockAPI) startServerAsync(listener net.Listener) { func (i *inetmockAPI) startServerAsync(listener net.Listener) {
i.serverRunning = true
if err := i.server.Serve(listener); err != nil { if err := i.server.Serve(listener); err != nil {
i.serverRunning = false
i.logger.Error( i.logger.Error(
"failed to start INetMock API", "failed to start INetMock API",
zap.Error(err), zap.Error(err),