Nexus/pkg/internal/directory/service.go

36 lines
750 B
Go
Raw Permalink Normal View History

2024-10-19 14:36:33 +00:00
package directory
2024-10-24 15:26:24 +00:00
import (
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)
2024-10-19 14:36:33 +00:00
type ServiceInstance struct {
ID string `json:"id"`
Type string `json:"type"`
Label string `json:"label"`
GrpcAddr string `json:"grpc_addr"`
HttpAddr *string `json:"http_addr"`
retryCount int
}
2024-10-24 15:26:24 +00:00
var connectionCache = make(map[string]*grpc.ClientConn)
2024-10-19 14:36:33 +00:00
func (v *ServiceInstance) GetGrpcConn() (*grpc.ClientConn, error) {
2024-10-24 15:26:24 +00:00
if conn, ok := connectionCache[v.ID]; ok {
return conn, nil
2024-10-19 14:36:33 +00:00
}
2024-10-24 15:26:24 +00:00
conn, err := ConnectService(v)
2024-10-19 14:36:33 +00:00
if err != nil {
_ = RemoveServiceInstance(v.ID)
2024-10-24 15:26:24 +00:00
log.Error().Str("id", v.ID).Err(err).Msg("Failed to connect to service, dropped...")
2024-10-19 14:36:33 +00:00
return nil, err
2024-10-24 15:26:24 +00:00
} else {
connectionCache[v.ID] = conn
2024-10-19 14:36:33 +00:00
}
2024-10-24 15:26:24 +00:00
return conn, nil
2024-10-19 14:36:33 +00:00
}