2021-01-13 17:07:04 +00:00
|
|
|
package endpoint
|
2020-04-13 22:14:56 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-02 09:56:48 +00:00
|
|
|
"context"
|
2020-04-13 22:14:56 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-12-26 13:11:49 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/api"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/config"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/health"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/logging"
|
|
|
|
"go.uber.org/zap"
|
2020-04-13 22:14:56 +00:00
|
|
|
)
|
|
|
|
|
2020-06-15 10:04:08 +00:00
|
|
|
const (
|
|
|
|
startupTimeoutDuration = 100 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2020-04-13 22:14:56 +00:00
|
|
|
type EndpointManager interface {
|
|
|
|
RegisteredEndpoints() []Endpoint
|
|
|
|
StartedEndpoints() []Endpoint
|
2020-12-26 13:11:49 +00:00
|
|
|
CreateEndpoint(name string, multiHandlerConfig config.EndpointConfig) error
|
2020-04-13 22:14:56 +00:00
|
|
|
StartEndpoints()
|
|
|
|
ShutdownEndpoints()
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func NewEndpointManager(registry api.HandlerRegistry, logging logging.Logger, checker health.Checker, pluginContext api.PluginContext) EndpointManager {
|
2020-04-13 22:14:56 +00:00
|
|
|
return &endpointManager{
|
2020-12-26 13:11:49 +00:00
|
|
|
registry: registry,
|
|
|
|
logger: logging,
|
|
|
|
checker: checker,
|
|
|
|
pluginContext: pluginContext,
|
2020-04-13 22:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type endpointManager struct {
|
2020-12-26 13:11:49 +00:00
|
|
|
registry api.HandlerRegistry
|
2020-04-13 22:14:56 +00:00
|
|
|
logger logging.Logger
|
2020-06-15 10:04:08 +00:00
|
|
|
checker health.Checker
|
2020-12-26 13:11:49 +00:00
|
|
|
pluginContext api.PluginContext
|
2020-04-13 22:14:56 +00:00
|
|
|
registeredEndpoints []Endpoint
|
|
|
|
properlyStartedEndpoints []Endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e endpointManager) RegisteredEndpoints() []Endpoint {
|
|
|
|
return e.registeredEndpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e endpointManager) StartedEndpoints() []Endpoint {
|
|
|
|
return e.properlyStartedEndpoints
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func (e *endpointManager) CreateEndpoint(name string, endpointConfig config.EndpointConfig) error {
|
|
|
|
for _, handlerConfig := range endpointConfig.HandlerConfigs() {
|
|
|
|
if handler, ok := e.registry.HandlerForName(endpointConfig.Handler); ok {
|
2020-04-13 22:14:56 +00:00
|
|
|
e.registeredEndpoints = append(e.registeredEndpoints, &endpoint{
|
2020-05-03 08:15:24 +00:00
|
|
|
id: uuid.New(),
|
2020-04-13 22:14:56 +00:00
|
|
|
name: name,
|
|
|
|
handler: handler,
|
|
|
|
config: handlerConfig,
|
|
|
|
})
|
2020-04-25 22:22:45 +00:00
|
|
|
} else {
|
2020-12-26 13:11:49 +00:00
|
|
|
return fmt.Errorf("no matching handler registered for names %s", endpointConfig.Handler)
|
2020-04-13 22:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 22:22:45 +00:00
|
|
|
|
|
|
|
return nil
|
2020-04-13 22:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *endpointManager) StartEndpoints() {
|
|
|
|
startTime := time.Now()
|
|
|
|
for _, endpoint := range e.registeredEndpoints {
|
|
|
|
endpointLogger := e.logger.With(
|
|
|
|
zap.String("endpoint", endpoint.Name()),
|
|
|
|
)
|
|
|
|
endpointLogger.Info("Starting endpoint")
|
2020-12-26 13:11:49 +00:00
|
|
|
if ok := startEndpoint(endpoint, e.pluginContext, endpointLogger); ok {
|
2020-06-15 10:04:08 +00:00
|
|
|
_ = e.checker.RegisterCheck(
|
|
|
|
endpointComponentName(endpoint),
|
|
|
|
health.StaticResultCheckWithMessage(health.HEALTHY, "Successfully started"),
|
|
|
|
)
|
2020-04-13 22:14:56 +00:00
|
|
|
e.properlyStartedEndpoints = append(e.properlyStartedEndpoints, endpoint)
|
|
|
|
endpointLogger.Info("successfully started endpoint")
|
|
|
|
} else {
|
2020-06-15 10:04:08 +00:00
|
|
|
_ = e.checker.RegisterCheck(
|
|
|
|
endpointComponentName(endpoint),
|
|
|
|
health.StaticResultCheckWithMessage(health.UNHEALTHY, "failed to start"),
|
|
|
|
)
|
2020-04-13 22:14:56 +00:00
|
|
|
endpointLogger.Error("error occurred during endpoint startup - will be skipped for now")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endpointStartupDuration := time.Since(startTime)
|
|
|
|
e.logger.Info(
|
|
|
|
"Startup of all endpoints completed",
|
|
|
|
zap.Duration("startupTime", endpointStartupDuration),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *endpointManager) ShutdownEndpoints() {
|
2021-01-02 16:24:06 +00:00
|
|
|
waitGroup := new(sync.WaitGroup)
|
2020-04-13 22:14:56 +00:00
|
|
|
waitGroup.Add(len(e.properlyStartedEndpoints))
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
parentCtx, _ := context.WithTimeout(context.Background(), shutdownTimeout)
|
|
|
|
|
|
|
|
perHandlerTimeout := e.shutdownTimePerEndpoint()
|
|
|
|
|
2020-04-13 22:14:56 +00:00
|
|
|
for _, endpoint := range e.properlyStartedEndpoints {
|
2020-10-02 09:56:48 +00:00
|
|
|
ctx, _ := context.WithTimeout(parentCtx, perHandlerTimeout)
|
2020-04-13 22:14:56 +00:00
|
|
|
endpointLogger := e.logger.With(
|
|
|
|
zap.String("endpoint", endpoint.Name()),
|
|
|
|
)
|
|
|
|
endpointLogger.Info("Triggering shutdown of endpoint")
|
2021-01-02 16:24:06 +00:00
|
|
|
go shutdownEndpoint(ctx, endpoint, endpointLogger, waitGroup)
|
2020-04-13 22:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
waitGroup.Wait()
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func startEndpoint(ep Endpoint, ctx api.PluginContext, logger logging.Logger) (success bool) {
|
2020-06-15 10:04:08 +00:00
|
|
|
startSuccessful := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
2020-10-02 09:56:48 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logger.Fatal(
|
|
|
|
"recovered panic during startup of endpoint",
|
|
|
|
zap.Any("recovered", r),
|
|
|
|
)
|
|
|
|
startSuccessful <- false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
if err := ep.Start(ctx); err != nil {
|
2020-06-15 10:04:08 +00:00
|
|
|
logger.Error(
|
|
|
|
"failed to start endpoint",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
startSuccessful <- false
|
|
|
|
} else {
|
|
|
|
startSuccessful <- true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case success = <-startSuccessful:
|
|
|
|
case <-time.After(startupTimeoutDuration):
|
|
|
|
success = false
|
2020-04-13 22:14:56 +00:00
|
|
|
}
|
2020-06-15 10:04:08 +00:00
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
close(startSuccessful)
|
|
|
|
|
2020-04-13 22:14:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
func shutdownEndpoint(ctx context.Context, ep Endpoint, logger logging.Logger, wg *sync.WaitGroup) {
|
2020-04-13 22:14:56 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logger.Fatal(
|
|
|
|
"recovered panic during shutdown of endpoint",
|
|
|
|
zap.Any("recovered", r),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2020-10-02 09:56:48 +00:00
|
|
|
if err := ep.Shutdown(ctx); err != nil {
|
2020-04-13 22:14:56 +00:00
|
|
|
logger.Error(
|
|
|
|
"Failed to shutdown endpoint",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 10:04:08 +00:00
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
func (e *endpointManager) shutdownTimePerEndpoint() time.Duration {
|
|
|
|
return time.Duration((float64(shutdownTimeout) * 0.9) / float64(len(e.properlyStartedEndpoints)))
|
|
|
|
}
|
|
|
|
|
2020-06-15 10:04:08 +00:00
|
|
|
func endpointComponentName(ep Endpoint) string {
|
|
|
|
return fmt.Sprintf("endpoint_%s", ep.Name())
|
|
|
|
}
|