Nexus/pkg/internal/directory/validator.go

65 lines
1.5 KiB
Go
Raw Normal View History

2024-10-24 23:26:24 +08:00
package directory
2025-03-01 13:22:38 +08:00
import (
"sync"
"github.com/rs/zerolog/log"
)
var statusOfServices = make(map[string]bool)
var statusLock sync.Mutex
func GetServiceStatus() map[string]bool {
out := make(map[string]bool)
for k, v := range statusOfServices {
out[k] = v
}
services := ListServiceInstance()
for _, service := range services {
if _, ok := out[service.Type]; !ok {
out[service.Type] = false
}
}
return out
}
2024-10-24 23:26:24 +08:00
func ValidateServices() {
2025-03-01 13:22:38 +08:00
statusLock.Lock()
defer statusLock.Unlock()
2024-10-24 23:26:24 +08:00
services := ListServiceInstance()
if len(services) == 0 {
return
}
checklist := make(map[string]bool)
successCount := 0
log.Info().Int("count", len(services)).Msg("Validating services...")
for _, service := range services {
if _, ok := checklist[service.GrpcAddr]; ok {
_ = RemoveServiceInstance(service.ID)
log.Warn().Str("id", service.ID).Str("addr", service.GrpcAddr).Msg("Duplicated service address, dropped...")
continue
}
// Directly use the connect method to skip cache
if _, err := ConnectService(service); err != nil {
2025-03-01 13:22:38 +08:00
statusOfServices[service.Type] = false
2024-10-24 23:26:24 +08:00
_ = RemoveServiceInstance(service.ID)
log.Warn().Err(err).Str("id", service.ID).Str("addr", service.GrpcAddr).Msg("Unable connect to service, dropped...")
continue
2025-03-01 13:22:38 +08:00
} else {
statusOfServices[service.Type] = true
2024-10-24 23:26:24 +08:00
}
successCount++
}
log.Info().
Int("success", successCount).
Int("failed", len(services)-successCount).
Int("total", len(services)).
Msg("Service validation completed.")
}