-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
server.go
100 lines (90 loc) · 3.1 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
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// 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
//
// http://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 driver
import (
"context"
"net"
otgrpc "github.com/opentracing-contrib/go-grpc"
"github.com/opentracing/opentracing-go"
"go.uber.org/zap"
"google.golang.org/grpc"
"github.com/jaegertracing/jaeger/examples/hotrod/pkg/log"
"github.com/jaegertracing/jaeger/examples/hotrod/pkg/tracing"
"github.com/jaegertracing/jaeger/pkg/metrics"
)
// Server implements jaeger-demo-frontend service
type Server struct {
hostPort string
tracer opentracing.Tracer
logger log.Factory
redis *Redis
server *grpc.Server
}
var _ DriverServiceServer = (*Server)(nil)
// NewServer creates a new driver.Server
func NewServer(hostPort string, otelExporter string, metricsFactory metrics.Factory, logger log.Factory) *Server {
tracer := tracing.Init("driver", otelExporter, metricsFactory, logger)
server := grpc.NewServer(
grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)),
grpc.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),
)
return &Server{
hostPort: hostPort,
tracer: tracer,
logger: logger,
server: server,
redis: newRedis(otelExporter, metricsFactory, logger),
}
}
// Run starts the Driver server
func (s *Server) Run() error {
lis, err := net.Listen("tcp", s.hostPort)
if err != nil {
s.logger.Bg().Fatal("Unable to create http listener", zap.Error(err))
}
RegisterDriverServiceServer(s.server, s)
err = s.server.Serve(lis)
if err != nil {
s.logger.Bg().Fatal("Unable to start gRPC server", zap.Error(err))
}
return err
}
// FindNearest implements gRPC driver interface
func (s *Server) FindNearest(ctx context.Context, location *DriverLocationRequest) (*DriverLocationResponse, error) {
s.logger.For(ctx).Info("Searching for nearby drivers", zap.String("location", location.Location))
driverIDs := s.redis.FindDriverIDs(ctx, location.Location)
retMe := make([]*DriverLocation, len(driverIDs))
for i, driverID := range driverIDs {
var drv Driver
var err error
for i := 0; i < 3; i++ {
drv, err = s.redis.GetDriver(ctx, driverID)
if err == nil {
break
}
s.logger.For(ctx).Error("Retrying GetDriver after error", zap.Int("retry_no", i+1), zap.Error(err))
}
if err != nil {
s.logger.For(ctx).Error("Failed to get driver after 3 attempts", zap.Error(err))
return nil, err
}
retMe[i] = &DriverLocation{
DriverID: drv.DriverID,
Location: drv.Location,
}
}
s.logger.For(ctx).Info("Search successful", zap.Int("num_drivers", len(retMe)))
return &DriverLocationResponse{Locations: retMe}, nil
}