open-match.dev/open-match@v1.8.1/examples/scale/mmf/mmf.go (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mmf
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	"github.com/sirupsen/logrus"
    22  	"google.golang.org/grpc"
    23  
    24  	"open-match.dev/open-match/pkg/pb"
    25  
    26  	utilTesting "open-match.dev/open-match/internal/util/testing"
    27  
    28  	"open-match.dev/open-match/examples/scale/scenarios"
    29  )
    30  
    31  var (
    32  	logger = logrus.WithFields(logrus.Fields{
    33  		"app":       "openmatch",
    34  		"component": "scale.mmf",
    35  	})
    36  )
    37  
    38  // Run triggers execution of a MMF.
    39  func Run() {
    40  	activeScenario := scenarios.ActiveScenario
    41  
    42  	conn, err := grpc.Dial("open-match-query.open-match.svc.cluster.local:50503", utilTesting.NewGRPCDialOptions(logger)...)
    43  	if err != nil {
    44  		logger.Fatalf("Failed to connect to Open Match, got %v", err)
    45  	}
    46  	defer conn.Close()
    47  
    48  	server := grpc.NewServer(utilTesting.NewGRPCServerOptions(logger)...)
    49  	pb.RegisterMatchFunctionServer(server, activeScenario.MMF)
    50  	ln, err := net.Listen("tcp", fmt.Sprintf(":%d", 50502))
    51  	if err != nil {
    52  		logger.WithFields(logrus.Fields{
    53  			"error": err.Error(),
    54  			"port":  50502,
    55  		}).Fatal("net.Listen() error")
    56  	}
    57  
    58  	logger.WithFields(logrus.Fields{
    59  		"port": 50502,
    60  	}).Info("TCP net listener initialized")
    61  
    62  	logger.Info("Serving gRPC endpoint")
    63  	err = server.Serve(ln)
    64  	if err != nil {
    65  		logger.WithFields(logrus.Fields{
    66  			"error": err.Error(),
    67  		}).Fatal("gRPC serve() error")
    68  	}
    69  }