<commit_before>/*
This application handles all the startup and connection scaffolding for
running a gRPC server serving the APIService as defined in
${OM_ROOT}/internal/pb/matchfunction.pb.go

All the actual important bits are in the API Server source code: apisrv/apisrv.go

Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package harness

import (
	"fmt"

	"github.com/GoogleCloudPlatform/open-match/config"
	"github.com/GoogleCloudPlatform/open-match/internal/harness/matchfunction/golang/apisrv"
	"github.com/GoogleCloudPlatform/open-match/internal/logging"
	"github.com/GoogleCloudPlatform/open-match/internal/metrics"
	"github.com/GoogleCloudPlatform/open-match/internal/pb"
	"github.com/GoogleCloudPlatform/open-match/internal/serving"
	"github.com/GoogleCloudPlatform/open-match/internal/signal"
	"github.com/GoogleCloudPlatform/open-match/internal/util/netlistener"
	"go.opencensus.io/plugin/ocgrpc"
	"go.opencensus.io/stats/view"
	"google.golang.org/grpc"

	log "github.com/sirupsen/logrus"
)

// HarnessParams is a collection of parameters used to create a MatchFunction server.
type HarnessParams struct {
	FunctionName          string
	ServicePortConfigName string
	ProxyPortConfigName   string
	Func                  apisrv.MatchFunction
}

// ServeMatchFunction is a hook for the main() method in the main executable.
func ServeMatchFunction(params *HarnessParams) {
	mfServer, err := newMatchFunctionServer(params)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err.Error(),
		}).Info("Cannot construct the match function server.")
		return
	}

	// Instantiate the gRPC server with the bindings we've made.
	logger := mfServer.Logger
	grpcLh, err := netlistener.NewFromPortNumber(mfServer.Config.GetInt(params.ServicePortConfigName))
	proxyLh, err := netlistener.NewFromPortNumber(mfServer.Config.GetInt(params.ProxyPortConfigName))
	if err != nil {
		logger.WithFields(log.Fields{"error": err.Error()}).Fatal("Failed to create a TCP listener for the GRPC server")
		return
	}

	grpcServer := serving.NewGrpcServer(grpcLh, proxyLh, logger)
	grpcServer.AddService(func(server *grpc.Server) {
		pb.RegisterMatchFunctionServer(server, mfServer)
	})

	defer func() {
		err := grpcServer.Stop()
		if err != nil {
			logger.WithFields(log.Fields{"error": err.Error()}).Infof("Server shutdown error, %s.", err)
		}
	}()

	// Start serving traffic.
	err = grpcServer.Start()
	if err != nil {
		logger.WithFields(log.Fields{"error": err.Error()}).Fatal("Failed to start server")
	}

	// Exit when we see a signal
	wait, _ := signal.New()
	wait()
	logger.Info("Shutting down server")
}

// newMatchFunctionServer creates a MatchFunctionServer based on the harness parameters.<commit_msg>Fix grpc harness startup panic due to http proxy not being set up (#244)<commit_after>/*
This application handles all the startup and connection scaffolding for
running a gRPC server serving the APIService as defined in
${OM_ROOT}/internal/pb/matchfunction.pb.go

All the actual important bits are in the API Server source code: apisrv/apisrv.go

Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package harness

import (
	"fmt"

	"github.com/GoogleCloudPlatform/open-match/config"
	"github.com/GoogleCloudPlatform/open-match/internal/harness/matchfunction/golang/apisrv"
	"github.com/GoogleCloudPlatform/open-match/internal/logging"
	"github.com/GoogleCloudPlatform/open-match/internal/metrics"
	"github.com/GoogleCloudPlatform/open-match/internal/pb"
	"github.com/GoogleCloudPlatform/open-match/internal/serving"
	"github.com/GoogleCloudPlatform/open-match/internal/signal"
	"github.com/GoogleCloudPlatform/open-match/internal/util/netlistener"
	"go.opencensus.io/plugin/ocgrpc"
	"go.opencensus.io/stats/view"
	"google.golang.org/grpc"

	log "github.com/sirupsen/logrus"
)

// HarnessParams is a collection of parameters used to create a MatchFunction server.
type HarnessParams struct {
	FunctionName          string
	ServicePortConfigName string
	ProxyPortConfigName   string
	Func                  apisrv.MatchFunction
}

// ServeMatchFunction is a hook for the main() method in the main executable.
func ServeMatchFunction(params *HarnessParams) {
	mfServer, err := newMatchFunctionServer(params)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err.Error(),
		}).Info("Cannot construct the match function server.")
		return
	}

	// Instantiate the gRPC server with the bindings we've made.
	logger := mfServer.Logger
	grpcLh, err := netlistener.NewFromPortNumber(mfServer.Config.GetInt(params.ServicePortConfigName))
	proxyLh, err := netlistener.NewFromPortNumber(mfServer.Config.GetInt(params.ProxyPortConfigName))
	if err != nil {
		logger.WithFields(log.Fields{"error": err.Error()}).Fatal("Failed to create a TCP listener for the GRPC server")
		return
	}

	grpcServer := serving.NewGrpcServer(grpcLh, proxyLh, logger)
	grpcServer.AddService(func(server *grpc.Server) {
		pb.RegisterMatchFunctionServer(server, mfServer)
	})
	grpcServer.AddProxy(pb.RegisterMatchFunctionHandlerFromEndpoint)

	defer func() {
		err := grpcServer.Stop()
		if err != nil {
			logger.WithFields(log.Fields{"error": err.Error()}).Infof("Server shutdown error, %s.", err)
		}
	}()

	// Start serving traffic.
	err = grpcServer.Start()
	if err != nil {
		logger.WithFields(log.Fields{"error": err.Error()}).Fatal("Failed to start server")
	}

	// Exit when we see a signal
	wait, _ := signal.New()
	wait()
	logger.Info("Shutting down server")
}

// newMatchFunctionServer creates a MatchFunctionServer based on the harness parameters.
{'fraction_non_alphanumeric': 0.1021530004580852, 'fraction_numerical': 0.0029012062910367996, 'mean_word_length': 4.705574912891986, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5318185', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1930, 'n_words': 563}