-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
178 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
gossiper "github.com/pieceowater-dev/lotof.lib.gossiper" | ||
) | ||
|
||
type SomeService struct { | ||
transport gossiper.Transport | ||
//client pb.SomeServiceClient // Add client as a property, generated from protobuf | ||
} | ||
|
||
func NewSomeService() *SomeService { | ||
factory := gossiper.NewTransportFactory() | ||
grpcTransport := factory.CreateTransport( | ||
gossiper.GRPC, | ||
"localhost:50051", | ||
) | ||
|
||
// Create the client only once and store it as a property | ||
//clientConstructor := pb.NewSomeServiceClient | ||
//client, err := grpcTransport.CreateClient(clientConstructor) | ||
//if err != nil { | ||
// log.Fatalf("Error creating client: %v", err) | ||
//} | ||
|
||
return &SomeService{ | ||
transport: grpcTransport, | ||
//client: client, | ||
} | ||
} | ||
|
||
//func (s *SomeService) Items() ([]any, error) { | ||
// ctx := context.Background() | ||
// | ||
// // Send the request using the client stored in the SomeService instance | ||
// response, err := s.transport.Send( | ||
// ctx, | ||
// s.client, | ||
// "GetItems", | ||
// &pb.GetItemsRequest{}, // Dynamic request for GetItems | ||
// ) | ||
// if err != nil { | ||
// log.Printf("Error sending request: %v", err) | ||
// return nil, err | ||
// } | ||
// | ||
// // Assert the response to the correct type | ||
// res, ok := response.(*pb.GetItemsResponse) | ||
// if !ok { | ||
// return nil, errors.New("invalid response type from gRPC transport") | ||
// } | ||
// | ||
// return res, nil | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package transport | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"reflect" | ||
) | ||
|
||
// GRPCTransport handles both client and server-side transport | ||
type GRPCTransport struct { | ||
address string | ||
server *grpc.Server | ||
} | ||
|
||
func NewGRPCTransport(address string) *GRPCTransport { | ||
return &GRPCTransport{address: address} | ||
} | ||
|
||
// CreateClient dynamically creates a gRPC client using the passed constructor. | ||
func (g *GRPCTransport) CreateClient(clientConstructor any) (any, error) { | ||
// Dial the gRPC connection. | ||
conn, err := grpc.Dial(g.address, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return nil, errors.New("failed to connect to gRPC server: " + err.Error()) | ||
} | ||
|
||
// Use reflection to call the constructor function dynamically | ||
constructorValue := reflect.ValueOf(clientConstructor) | ||
if constructorValue.Kind() != reflect.Func { | ||
return nil, errors.New("clientConstructor must be a function") | ||
} | ||
|
||
// Call the constructor to create the client (pass the connection as argument) | ||
clientValues := constructorValue.Call([]reflect.Value{reflect.ValueOf(conn)}) | ||
|
||
// Ensure that the client creation was successful and return the client | ||
if len(clientValues) > 0 { | ||
return clientValues[0].Interface(), nil | ||
} | ||
return nil, errors.New("failed to create client") | ||
} | ||
|
||
// Send sends a dynamic gRPC request based on method name and request type | ||
func (g *GRPCTransport) Send(ctx context.Context, client any, serviceMethod string, request any) (any, error) { | ||
// Use reflection to get the method from the client dynamically | ||
clientValue := reflect.ValueOf(client) | ||
method := clientValue.MethodByName(serviceMethod) | ||
if !method.IsValid() { | ||
return nil, errors.New("invalid service method: " + serviceMethod) | ||
} | ||
|
||
// Ensure the request is passed as a reflect.Value | ||
reqValue := reflect.ValueOf(request) | ||
if reqValue.IsValid() { | ||
// Call the method dynamically, passing the context and the request | ||
returnValues := method.Call([]reflect.Value{reflect.ValueOf(ctx), reqValue}) | ||
if len(returnValues) > 1 && returnValues[1].Interface() != nil { | ||
return nil, returnValues[1].Interface().(error) | ||
} | ||
// Return the response from the method call | ||
return returnValues[0].Interface(), nil | ||
} | ||
return nil, errors.New("invalid request type for method") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package transport | ||
|
||
import "context" | ||
|
||
type Transport interface { | ||
CreateClient(clientConstructor any) (any, error) | ||
Send(ctx context.Context, client any, serviceMethod string, request any) (any, error) | ||
} | ||
|
||
type Type string | ||
|
||
const ( | ||
GRPC Type = "grpc" | ||
) | ||
|
||
type Factory struct{} | ||
|
||
func NewFactory() *Factory { | ||
return &Factory{} | ||
} | ||
|
||
func (f *Factory) CreateTransport(transportType Type, address string) Transport { | ||
switch transportType { | ||
case GRPC: | ||
return NewGRPCTransport(address) | ||
default: | ||
return nil | ||
} | ||
} |