/
service.go
50 lines (43 loc) · 1.5 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package service
import (
"github.com/Octops/agones-discover-openmatch/internal/runtime"
"github.com/Octops/agones-discover-openmatch/pkg/matchfunction/functions"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"open-match.dev/open-match/pkg/matchfunction"
"open-match.dev/open-match/pkg/pb"
)
type MatchFunctionService struct {
logger *logrus.Entry
queryServiceClient pb.QueryServiceClient
makeMatchesFunc functions.MakeMatchesFunc
}
func NewMatchFunctionService(queryServiceClient pb.QueryServiceClient, makeMatchesFunc functions.MakeMatchesFunc) pb.MatchFunctionServer {
return &MatchFunctionService{
logger: runtime.Logger().WithField("source", "match_function"),
queryServiceClient: queryServiceClient,
makeMatchesFunc: makeMatchesFunc,
}
}
func (s *MatchFunctionService) Run(req *pb.RunRequest, stream pb.MatchFunction_RunServer) error {
poolTickets, err := matchfunction.QueryPools(stream.Context(), s.queryServiceClient, req.GetProfile().GetPools())
if err != nil {
err = errors.Wrap(err, "failed to query pools")
s.logger.Error(err)
return err
}
proposals, err := s.makeMatchesFunc(req.GetProfile(), poolTickets)
if err != nil {
err = errors.Wrap(err, "failed to make matches")
s.logger.Error(err)
return err
}
for _, proposal := range proposals {
if err := stream.Send(&pb.RunResponse{Proposal: proposal}); err != nil {
err := errors.Wrap(err, "failed to stream proposals to Open Match")
s.logger.Error(err)
return err
}
}
return nil
}