2023-10-20 18:35:02 +02:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/conn"
|
|
|
|
logging "github.com/tim-beatham/smegmesh/pkg/log"
|
|
|
|
"github.com/tim-beatham/smegmesh/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 {
|
2024-01-05 13:59:13 +01:00
|
|
|
Handle(mesh mesh.MeshProvider, endpoint string, err error) bool
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
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-12-29 02:29:11 +01:00
|
|
|
connManager conn.ConnectionManager
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2024-01-05 13:59:13 +01:00
|
|
|
func (s *SyncErrorHandlerImpl) handleFailed(mesh mesh.MeshProvider, nodeId string) bool {
|
2023-12-07 02:44:54 +01:00
|
|
|
mesh.Mark(nodeId)
|
2023-12-29 12:12:40 +01:00
|
|
|
node, err := mesh.GetNode(nodeId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.connManager.RemoveConnection(node.GetHostEndpoint())
|
|
|
|
}
|
2023-10-20 18:35:02 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-05 13:59:13 +01:00
|
|
|
func (s *SyncErrorHandlerImpl) handleDeadlineExceeded(mesh mesh.MeshProvider, nodeId string) bool {
|
2023-12-29 02:29:11 +01:00
|
|
|
node, err := mesh.GetNode(nodeId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
s.connManager.RemoveConnection(node.GetHostEndpoint())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-05 13:59:13 +01:00
|
|
|
func (s *SyncErrorHandlerImpl) Handle(mesh mesh.MeshProvider, 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() {
|
2023-12-29 02:29:11 +01:00
|
|
|
case codes.Unavailable, codes.Unknown, codes.Internal, codes.NotFound:
|
2024-01-05 13:59:13 +01:00
|
|
|
return s.handleFailed(mesh, nodeId)
|
2023-12-29 02:29:11 +01:00
|
|
|
case codes.DeadlineExceeded:
|
2024-01-05 13:59:13 +01:00
|
|
|
return s.handleDeadlineExceeded(mesh, nodeId)
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-29 12:12:40 +01:00
|
|
|
func NewSyncErrorHandler(m mesh.MeshManager, conn conn.ConnectionManager) SyncErrorHandler {
|
|
|
|
return &SyncErrorHandlerImpl{meshManager: m, connManager: conn}
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|