Skip to content

Commit

Permalink
Update gRPC to not repeatedly make new connections
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntii committed Jul 6, 2023
1 parent f8192ce commit a63b2dc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
6 changes: 6 additions & 0 deletions globals/globals.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package globals

import (
pb "github.com/PretendoNetwork/grpc-go/friends"
"github.com/PretendoNetwork/nex-go"
"github.com/PretendoNetwork/plogger-go"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

var Logger = plogger.NewLogger()
var NEXServer *nex.Server
var GRPCFriendsClientConnection *grpc.ClientConn
var GRPCFriendsClient pb.FriendsClient
var GRPCFriendsCommonMetadata metadata.MD
20 changes: 2 additions & 18 deletions grpc/send_incoming_call_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,18 @@ import (
"context"
"encoding/binary"
"log"
"os"

pb "github.com/PretendoNetwork/grpc-go/friends"
nex "github.com/PretendoNetwork/nex-go"
friends_wiiu_types "github.com/PretendoNetwork/nex-protocols-go/friends-wiiu/types"
nintendo_notifications "github.com/PretendoNetwork/nex-protocols-go/nintendo-notifications"
nintendo_notifications_types "github.com/PretendoNetwork/nex-protocols-go/nintendo-notifications/types"
"github.com/PretendoNetwork/wiiu-chat-secure/globals"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)

func SendIncomingCallNotification(caller uint32, target uint32) {
// Connect to Friends gRPC.
conn, err := grpc.Dial(os.Getenv("FRIENDS_GRPC_ADDRESS"), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("Connection to Friends gRPC failed! : %v", err)
}

defer conn.Close()
c := pb.NewFriendsClient(conn)

md := metadata.Pairs(
"X-API-Key", os.Getenv("FRIENDS_GRPC_API_KEY"),
)

ctx := metadata.NewOutgoingContext(context.Background(), md)
ctx := metadata.NewOutgoingContext(context.Background(), globals.GRPCFriendsCommonMetadata)

presence := friends_wiiu_types.NewNintendoPresenceV2()

Expand Down Expand Up @@ -63,7 +47,7 @@ func SendIncomingCallNotification(caller uint32, target uint32) {
stream := nex.NewStreamOut(globals.NEXServer)
eventObjectBytes := eventObject.Bytes(stream)

_, err = c.SendUserNotificationWiiU(ctx, &pb.SendUserNotificationWiiURequest{Pid: target, NotificationData: eventObjectBytes})
_, err := globals.GRPCFriendsClient.SendUserNotificationWiiU(ctx, &pb.SendUserNotificationWiiURequest{Pid: target, NotificationData: eventObjectBytes})
if err != nil {
log.Fatalf("Greeting Friends gRPC failed! : %v", err)
}
Expand Down
47 changes: 47 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package main

import (
"fmt"
"os"
"strconv"
"strings"

pb "github.com/PretendoNetwork/grpc-go/friends"
"github.com/PretendoNetwork/plogger-go"
"github.com/PretendoNetwork/wiiu-chat-secure/database"
"github.com/PretendoNetwork/wiiu-chat-secure/globals"
"github.com/joho/godotenv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)

var logger = plogger.NewLogger()
Expand All @@ -14,5 +24,42 @@ func init() {
logger.Warning("Error loading .env file")
}

friendsGRPCHost := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_HOST")
friendsGRPCPort := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_PORT")
friendsGRPCAPIKey := os.Getenv("PN_WIIU_CHAT_FRIENDS_GRPC_API_KEY")

if strings.TrimSpace(friendsGRPCHost) == "" {
globals.Logger.Error("PN_WIIU_CHAT_FRIENDS_GRPC_HOST environment variable not set")
os.Exit(0)
}

if strings.TrimSpace(friendsGRPCPort) == "" {
globals.Logger.Error("PN_WIIU_CHAT_FRIENDS_GRPC_PORT environment variable not set")
os.Exit(0)
}

if port, err := strconv.Atoi(friendsGRPCPort); err != nil {
globals.Logger.Errorf("PN_WIIU_CHAT_FRIENDS_GRPC_PORT is not a valid port. Expected 0-65535, got %s", friendsGRPCPort)
os.Exit(0)
} else if port < 0 || port > 65535 {
globals.Logger.Errorf("PN_WIIU_CHAT_FRIENDS_GRPC_PORT is not a valid port. Expected 0-65535, got %s", friendsGRPCPort)
os.Exit(0)
}

if strings.TrimSpace(friendsGRPCAPIKey) == "" {
globals.Logger.Warning("Insecure gRPC server detected. PN_WIIU_CHAT_FRIENDS_GRPC_API_KEY environment variable not set")
}

globals.GRPCFriendsClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", friendsGRPCHost, friendsGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
globals.Logger.Criticalf("Failed to connect to friends gRPC server: %v", err)
os.Exit(0)
}

globals.GRPCFriendsClient = pb.NewFriendsClient(globals.GRPCFriendsClientConnection)
globals.GRPCFriendsCommonMetadata = metadata.Pairs(
"X-API-Key", friendsGRPCAPIKey,
)

database.ConnectAll()
}

0 comments on commit a63b2dc

Please sign in to comment.