forked from delimitrou/DeathStarBench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
177 lines (147 loc) · 4.01 KB
/
server.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package search
import (
// "encoding/json"
"fmt"
// F"io/ioutil"
"net"
"github.com/rs/zerolog/log"
// "os"
"time"
"github.com/google/uuid"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/harlow/go-micro-services/dialer"
"github.com/harlow/go-micro-services/registry"
geo "github.com/harlow/go-micro-services/services/geo/proto"
rate "github.com/harlow/go-micro-services/services/rate/proto"
pb "github.com/harlow/go-micro-services/services/search/proto"
"github.com/harlow/go-micro-services/tls"
opentracing "github.com/opentracing/opentracing-go"
context "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
const name = "srv-search"
// Server implments the search service
type Server struct {
geoClient geo.GeoClient
rateClient rate.RateClient
Tracer opentracing.Tracer
Port int
IpAddr string
Registry *registry.Client
uuid string
}
// Run starts the server
func (s *Server) Run() error {
if s.Port == 0 {
return fmt.Errorf("server port must be set")
}
s.uuid = uuid.New().String()
opts := []grpc.ServerOption{
grpc.KeepaliveParams(keepalive.ServerParameters{
Timeout: 120 * time.Second,
}),
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
PermitWithoutStream: true,
}),
grpc.UnaryInterceptor(
otgrpc.OpenTracingServerInterceptor(s.Tracer),
),
}
if tlsopt := tls.GetServerOpt(); tlsopt != nil {
opts = append(opts, tlsopt)
}
srv := grpc.NewServer(opts...)
pb.RegisterSearchServer(srv, s)
// init grpc clients
if err := s.initGeoClient("srv-geo"); err != nil {
return err
}
if err := s.initRateClient("srv-rate"); err != nil {
return err
}
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.Port))
if err != nil {
log.Fatal().Msgf("failed to listen: %v", err)
}
// register with consul
// jsonFile, err := os.Open("config.json")
// if err != nil {
// fmt.Println(err)
// }
// defer jsonFile.Close()
// byteValue, _ := ioutil.ReadAll(jsonFile)
// var result map[string]string
// json.Unmarshal([]byte(byteValue), &result)
err = s.Registry.Register(name, s.uuid, s.IpAddr, s.Port)
if err != nil {
return fmt.Errorf("failed register: %v", err)
}
log.Info().Msg("Successfully registered in consul")
return srv.Serve(lis)
}
// Shutdown cleans up any processes
func (s *Server) Shutdown() {
s.Registry.Deregister(s.uuid)
}
func (s *Server) initGeoClient(name string) error {
conn, err := dialer.Dial(
name,
dialer.WithTracer(s.Tracer),
dialer.WithBalancer(s.Registry.Client),
)
if err != nil {
return fmt.Errorf("dialer error: %v", err)
}
s.geoClient = geo.NewGeoClient(conn)
return nil
}
func (s *Server) initRateClient(name string) error {
conn, err := dialer.Dial(
name,
dialer.WithTracer(s.Tracer),
dialer.WithBalancer(s.Registry.Client),
)
if err != nil {
return fmt.Errorf("dialer error: %v", err)
}
s.rateClient = rate.NewRateClient(conn)
return nil
}
// Nearby returns ids of nearby hotels ordered by ranking algo
func (s *Server) Nearby(ctx context.Context, req *pb.NearbyRequest) (*pb.SearchResult, error) {
// find nearby hotels
log.Trace().Msg("in Search Nearby")
log.Trace().Msgf("nearby lat = %f", req.Lat)
log.Trace().Msgf("nearby lon = %f", req.Lon)
nearby, err := s.geoClient.Nearby(ctx, &geo.Request{
Lat: req.Lat,
Lon: req.Lon,
})
if err != nil {
return nil, err
}
for _, hid := range nearby.HotelIds {
log.Trace().Msgf("get Nearby hotelId = %s", hid)
}
// find rates for hotels
rates, err := s.rateClient.GetRates(ctx, &rate.Request{
HotelIds: nearby.HotelIds,
InDate: req.InDate,
OutDate: req.OutDate,
})
if err != nil {
return nil, err
}
// TODO(hw): add simple ranking algo to order hotel ids:
// * geo distance
// * price (best discount?)
// * reviews
// build the response
res := new(pb.SearchResult)
for _, ratePlan := range rates.RatePlans {
log.Trace().Msgf("get RatePlan HotelId = %s, Code = %s", ratePlan.HotelId, ratePlan.Code)
res.HotelIds = append(res.HotelIds, ratePlan.HotelId)
}
return res, nil
}