Service validation

This commit is contained in:
LittleSheep 2024-10-24 23:26:24 +08:00
parent 1b5b2c42e5
commit aa67bd0b8f
3 changed files with 52 additions and 7 deletions

View File

@ -1,6 +1,9 @@
package directory package directory
import "google.golang.org/grpc" import (
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)
type ServiceInstance struct { type ServiceInstance struct {
ID string `json:"id"` ID string `json:"id"`
@ -9,21 +12,24 @@ type ServiceInstance struct {
GrpcAddr string `json:"grpc_addr"` GrpcAddr string `json:"grpc_addr"`
HttpAddr *string `json:"http_addr"` HttpAddr *string `json:"http_addr"`
grpcConn *grpc.ClientConn
retryCount int retryCount int
} }
var connectionCache = make(map[string]*grpc.ClientConn)
func (v *ServiceInstance) GetGrpcConn() (*grpc.ClientConn, error) { func (v *ServiceInstance) GetGrpcConn() (*grpc.ClientConn, error) {
if v.grpcConn != nil { if conn, ok := connectionCache[v.ID]; ok {
return v.grpcConn, nil return conn, nil
} }
var err error conn, err := ConnectService(v)
v.grpcConn, err = ConnectService(v)
if err != nil { if err != nil {
_ = RemoveServiceInstance(v.ID) _ = RemoveServiceInstance(v.ID)
log.Error().Str("id", v.ID).Err(err).Msg("Failed to connect to service, dropped...")
return nil, err return nil, err
} else {
connectionCache[v.ID] = conn
} }
return v.grpcConn, nil return conn, nil
} }

View File

@ -0,0 +1,35 @@
package directory
import "github.com/rs/zerolog/log"
func ValidateServices() {
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 {
_ = RemoveServiceInstance(service.ID)
log.Warn().Err(err).Str("id", service.ID).Str("addr", service.GrpcAddr).Msg("Unable connect to service, dropped...")
continue
}
successCount++
}
log.Info().
Int("success", successCount).
Int("failed", len(services)-successCount).
Int("total", len(services)).
Msg("Service validation completed.")
}

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/nexus/pkg/internal/auth" "git.solsynth.dev/hypernet/nexus/pkg/internal/auth"
"git.solsynth.dev/hypernet/nexus/pkg/internal/database" "git.solsynth.dev/hypernet/nexus/pkg/internal/database"
"git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
"git.solsynth.dev/hypernet/nexus/pkg/internal/http" "git.solsynth.dev/hypernet/nexus/pkg/internal/http"
"git.solsynth.dev/hypernet/nexus/pkg/internal/kv" "git.solsynth.dev/hypernet/nexus/pkg/internal/kv"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
@ -93,6 +94,9 @@ func main() {
log.Info().Msg("Internal jwt private key loaded.") log.Info().Msg("Internal jwt private key loaded.")
} }
// Post-boot actions
directory.ValidateServices()
// Server // Server
go server.NewServer().Listen() go server.NewServer().Listen()