fix:#56 added update for grpc

This commit is contained in:
Elmar Kresse
2024-10-06 20:11:03 +02:00
parent a12f9590f3
commit d8a7cc5387
10 changed files with 68 additions and 11 deletions

View File

@@ -39,8 +39,15 @@ func main() {
}
grpcClient := grpc.ConnectGRPCServer(host)
// Close the grpc connection when the main function ends
defer grpc.CloseGRPCServer(grpcClient)
// Log the grpc connection
// Test the connection to the grpc server
grpcClient.Connect()
slog.Info("GRPC connection state", "state", grpcClient.GetState())
// Initialize a new Fiber app
webdavRequestMethods := []string{"PROPFIND", "MKCOL", "COPY", "MOVE"}

View File

@@ -19,14 +19,31 @@ package grpc
import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"log/slog"
"sync"
"time"
)
var conn *grpc.ClientConn
var once sync.Once
func ConnectGRPCServer(host string) *grpc.ClientConn {
conn, err := grpc.NewClient(host+":50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
slog.Error("could not connect to grpc server", "error", err)
}
once.Do(func() {
var err error
conn, err = grpc.NewClient(
host+":50051",
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 2 * time.Minute,
Timeout: 20 * time.Second,
PermitWithoutStream: true,
}),
)
if err != nil {
slog.Error("could not connect to grpc server", "error", err)
}
})
return conn
}