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"
|
|
|
|
)
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// SyncErrorHandler: Handles errors when attempting to sync
|
2023-10-20 18:35:02 +02:00
|
|
|
type SyncErrorHandler interface {
|
|
|
|
Handle(meshId string, endpoint string, err error) bool
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// SyncErrorHandlerImpl Is an implementation of the SyncErrorHandler
|
2023-10-20 18:35:02 +02:00
|
|
|
type SyncErrorHandlerImpl struct {
|
2023-11-05 19:03:58 +01:00
|
|
|
meshManager mesh.MeshManager
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:37:28 +01:00
|
|
|
// self, err := s.meshManager.GetSelf(meshId)
|
2023-11-06 10:54:06 +01:00
|
|
|
|
2023-11-06 14:37:28 +01:00
|
|
|
// if err != nil {
|
|
|
|
// return false
|
|
|
|
// }
|
2023-11-06 10:54:06 +01:00
|
|
|
|
2023-11-06 14:37:28 +01:00
|
|
|
// mesh.DecrementHealth(endpoint, self.GetHostEndpoint())
|
2023-10-20 18:35:02 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SyncErrorHandlerImpl) Handle(meshId string, endpoint string, err error) bool {
|
|
|
|
errStatus, _ := status.FromError(err)
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteInfof("Handled gRPC error: %s", errStatus.Message())
|
2023-10-20 18:35:02 +02:00
|
|
|
|
|
|
|
switch errStatus.Code() {
|
|
|
|
case codes.Unavailable, codes.Unknown, codes.DeadlineExceeded, codes.Internal, codes.NotFound:
|
|
|
|
return s.incrementFailedCount(meshId, endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
func NewSyncErrorHandler(m mesh.MeshManager) SyncErrorHandler {
|
2023-10-20 18:35:02 +02:00
|
|
|
return &SyncErrorHandlerImpl{meshManager: m}
|
|
|
|
}
|