2023-10-20 18:35:02 +02:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
2023-10-22 14:34:49 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
2023-10-20 18:35:02 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SyncErrorHandler interface {
|
|
|
|
Handle(meshId string, endpoint string, err error) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type SyncErrorHandlerImpl struct {
|
2023-10-22 14:34:49 +02:00
|
|
|
meshManager *mesh.MeshManger
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SyncErrorHandlerImpl) incrementFailedCount(meshId string, endpoint string) bool {
|
|
|
|
mesh := s.meshManager.GetMesh(meshId)
|
|
|
|
|
|
|
|
if mesh == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err := mesh.IncrementFailedCount(endpoint)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SyncErrorHandlerImpl) Handle(meshId string, endpoint string, err error) bool {
|
|
|
|
errStatus, _ := status.FromError(err)
|
|
|
|
|
|
|
|
logging.WarningLog.Printf("Handled gRPC error: %s", errStatus.Message())
|
|
|
|
|
|
|
|
switch errStatus.Code() {
|
|
|
|
case codes.Unavailable, codes.Unknown, codes.DeadlineExceeded, codes.Internal, codes.NotFound:
|
|
|
|
return s.incrementFailedCount(meshId, endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-22 14:34:49 +02:00
|
|
|
func NewSyncErrorHandler(m *mesh.MeshManger) SyncErrorHandler {
|
2023-10-20 18:35:02 +02:00
|
|
|
return &SyncErrorHandlerImpl{meshManager: m}
|
|
|
|
}
|