Nexus/pkg/nex/conn.go

108 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-10-19 14:36:33 +00:00
package nex
import (
"context"
2024-10-20 09:23:53 +00:00
"google.golang.org/grpc/metadata"
2024-10-19 14:36:33 +00:00
"time"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
health "google.golang.org/grpc/health/grpc_health_v1"
)
2024-10-20 09:23:53 +00:00
type Conn struct {
2024-10-19 14:36:33 +00:00
Addr string
Info *proto.ServiceInfo
2024-10-20 09:23:53 +00:00
commandServer *grpc.Server
commandHandlers map[string]CommandHandler
nexusConn *grpc.ClientConn
clientConn map[string]*grpc.ClientConn
2024-10-19 14:36:33 +00:00
}
2024-10-20 09:23:53 +00:00
func NewNexusConn(addr string, info *proto.ServiceInfo) (*Conn, error) {
2024-10-19 14:36:33 +00:00
conn, err := grpc.NewClient(
addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}
2024-10-20 09:23:53 +00:00
return &Conn{
2024-10-19 14:36:33 +00:00
Addr: addr,
Info: info,
2024-10-20 09:23:53 +00:00
commandHandlers: make(map[string]CommandHandler),
nexusConn: conn,
clientConn: make(map[string]*grpc.ClientConn),
2024-10-19 14:36:33 +00:00
}, nil
}
2024-10-20 09:23:53 +00:00
func (v *Conn) RegisterService() error {
2024-10-21 14:07:36 +00:00
dir := proto.NewDirectoryServiceClient(v.nexusConn)
2024-10-20 09:23:53 +00:00
ctx := context.Background()
ctx = metadata.AppendToOutgoingContext(ctx, "client_id", v.Info.Id)
_, err := dir.AddService(ctx, v.Info)
2024-10-19 14:36:33 +00:00
return err
}
2024-10-20 09:23:53 +00:00
func (v *Conn) RunRegistering() error {
2024-10-19 14:36:33 +00:00
err := v.RegisterService()
if err != nil {
return err
}
for {
time.Sleep(5 * time.Second)
2024-10-20 09:23:53 +00:00
client := health.NewHealthClient(v.nexusConn)
2024-10-19 14:36:33 +00:00
if _, err := client.Check(context.Background(), &health.HealthCheckRequest{}); err != nil {
2024-10-20 09:23:53 +00:00
if v.RunRegistering() == nil {
2024-10-19 14:36:33 +00:00
break
}
}
}
return nil
}
2024-10-20 09:23:53 +00:00
func (v *Conn) GetNexusGrpcConn() *grpc.ClientConn {
return v.nexusConn
2024-10-19 14:36:33 +00:00
}
2024-10-20 09:23:53 +00:00
func (v *Conn) GetClientGrpcConn(t string) (*grpc.ClientConn, error) {
if val, ok := v.clientConn[t]; ok {
2024-10-19 14:36:33 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
if _, err := health.NewHealthClient(val).Check(ctx, &health.HealthCheckRequest{
Service: t,
}); err == nil {
return val, nil
} else {
2024-10-20 09:23:53 +00:00
delete(v.clientConn, t)
2024-10-19 14:36:33 +00:00
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
2024-10-21 14:07:36 +00:00
out, err := proto.NewDirectoryServiceClient(v.nexusConn).GetService(ctx, &proto.GetServiceRequest{
2024-10-19 14:36:33 +00:00
Type: &t,
})
if err != nil {
return nil, err
}
conn, err := grpc.NewClient(
out.GetData().GetGrpcAddr(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err == nil {
2024-10-20 09:23:53 +00:00
v.clientConn[t] = conn
2024-10-19 14:36:33 +00:00
}
return conn, err
}