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
|
|
|
}
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
func (s *SyncErrorHandlerImpl) handleFailed(meshId string, nodeId string) bool {
|
2023-10-20 18:35:02 +02:00
|
|
|
mesh := s.meshManager.GetMesh(meshId)
|
2023-12-07 02:44:54 +01:00
|
|
|
mesh.Mark(nodeId)
|
2023-10-20 18:35:02 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
func (s *SyncErrorHandlerImpl) Handle(meshId string, nodeId string, err error) bool {
|
2023-10-20 18:35:02 +02:00
|
|
|
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:
|
2023-12-07 02:44:54 +01:00
|
|
|
return s.handleFailed(meshId, nodeId)
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
}
|