-
Notifications
You must be signed in to change notification settings - Fork 166
/
router.go
158 lines (127 loc) · 3.42 KB
/
router.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
package server
import (
"context"
"errors"
"fmt"
gonet "net"
"net/url"
"time"
"github.com/golang/glog"
"github.com/livepeer/go-livepeer/clog"
"github.com/livepeer/go-livepeer/net"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
)
const getOrchestratorTimeout = 2 * time.Second
var errNoOrchestrators = errors.New("no orchestrators")
type Router struct {
uris []*url.URL
srv *grpc.Server
net.UnimplementedOrchestratorServer
}
func (r *Router) EndTranscodingSession(ctx context.Context, request *net.EndTranscodingSessionRequest) (*net.EndTranscodingSessionResponse, error) {
// shouldn't ever be called on Router
return &net.EndTranscodingSessionResponse{}, nil
}
func NewRouter(uris []*url.URL) *Router {
return &Router{uris: uris}
}
func (r *Router) Start(uri *url.URL, serviceURI *url.URL, workDir string) error {
listener, err := gonet.Listen("tcp", uri.Host)
if err != nil {
return err
}
defer listener.Close()
certFile, keyFile, err := getCert(serviceURI, workDir)
if err != nil {
return err
}
creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
if err != nil {
return err
}
s := grpc.NewServer(grpc.Creds(creds))
r.srv = s
net.RegisterOrchestratorServer(s, r)
grpc_health_v1.RegisterHealthServer(s, health.NewServer())
errCh := make(chan error)
go func() {
errCh <- s.Serve(listener)
}()
time.Sleep(1 * time.Second)
if err := checkAvailability(context.Background(), serviceURI); err != nil {
s.Stop()
return err
}
glog.Infof("Started router server at %v", uri)
return <-errCh
}
func (r *Router) Stop() {
r.srv.Stop()
}
func (r *Router) GetOrchestrator(ctx context.Context, req *net.OrchestratorRequest) (*net.OrchestratorInfo, error) {
return getOrchestratorInfo(ctx, r.uris, req)
}
func (r *Router) Ping(ctx context.Context, req *net.PingPong) (*net.PingPong, error) {
return &net.PingPong{Value: []byte{}}, nil
}
func checkAvailability(ctx context.Context, uri *url.URL) error {
client, conn, err := startOrchestratorClient(ctx, uri)
if err != nil {
return err
}
defer conn.Close()
ctx, cancel := context.WithTimeout(clog.Clone(context.Background(), ctx), GRPCConnectTimeout)
defer cancel()
_, err = client.Ping(ctx, &net.PingPong{Value: []byte{}})
if err != nil {
return err
}
return nil
}
func getOrchestratorInfo(ctx context.Context, uris []*url.URL, req *net.OrchestratorRequest) (*net.OrchestratorInfo, error) {
if len(uris) == 0 {
return nil, errNoOrchestrators
}
infoCh := make(chan *net.OrchestratorInfo)
errCh := make(chan error, len(uris))
cctx, cancel := context.WithTimeout(ctx, getOrchestratorTimeout)
defer cancel()
for _, uri := range uris {
go func(uri *url.URL) {
client, conn, err := startOrchestratorClient(ctx, uri)
if err != nil {
errCh <- fmt.Errorf("%v err=%q", uri, err)
return
}
defer conn.Close()
info, err := client.GetOrchestrator(cctx, req)
if err != nil {
errCh <- fmt.Errorf("%v err=%q", uri, err)
return
}
select {
case infoCh <- info:
default:
}
}(uri)
}
errCtr := 0
for {
select {
case info := <-infoCh:
glog.Infof("Forwarding OrchestratorInfo orch=%v", info.Transcoder)
return info, nil
case err := <-errCh:
glog.Error(err)
errCtr++
if errCtr >= len(uris) {
return nil, errNoOrchestrators
}
case <-cctx.Done():
return nil, errors.New("timed out")
}
}
}