/
main.go
145 lines (127 loc) · 5.15 KB
/
main.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
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//go:generate thrift-gen --generateThrift --outputDir gen-go --inputFile ping.thrift --template github.com/uber/ringpop-go/ringpop.thrift-gen
package main
import (
"errors"
"flag"
log "github.com/sirupsen/logrus"
"github.com/uber-common/bark"
"github.com/uber/ringpop-go"
"github.com/uber/ringpop-go/discovery/jsonfile"
gen "github.com/uber/ringpop-go/examples/ping-thrift-gen/gen-go/ping"
"github.com/uber/ringpop-go/swim"
"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/thrift"
)
var (
hostport = flag.String("listen", "127.0.0.1:3000", "hostport to start service on")
hostfile = flag.String("hosts", "./hosts.json", "path to hosts file")
)
type worker struct {
ringpop *ringpop.Ringpop
channel *tchannel.Channel
logger *log.Logger
}
func (w *worker) RegisterPing() error {
server := thrift.NewServer(w.channel)
// wrap the PingPongService worker by a ringpop adapter for routing RPC calls
// NewRingpopPingPongServiceAdapter is in the package containing the generated code
// Its name is derived from the thrift service name as follows: `NewRingpop[Service Name]Adapter`
adapter, err := gen.NewRingpopPingPongServiceAdapter(w, w.ringpop, w.channel,
// PingPongServiceConfiguration contains the configuration for ringpop forwarding regaring this service
// The name is derived as follows `[Service Name]Configuration`
gen.PingPongServiceConfiguration{
// The ping member of the configuration refers to the Ping endpoint within the service.
// Configuring an endpoint is optional and unconfigured endpoints will work but not be
// forwarded to a different ringpop node during invocation
// The name of the configuration struct passed in here is derived as follows:
// `[Service Name][Endpoint Name]Configuration`
Ping: &gen.PingPongServicePingConfiguration{
// The configuration structs only member is the Key closure. The purpose of this closure
// is to return the key used for ringpop sharding and forwarding. Calls that are sharded
// on the same key are guaranteed to be forwarded to the same node.
// The input signature for this closure is the same as the signature for your
// implementation. The output is always the tuple (shardKey string, err error).
Key: func(ctx thrift.Context, request *gen.Ping) (shardKey string, err error) {
// The body of the Key closure can perform whatever logic is needed to come to a
// meaningful shardKey for the the request
if request == nil {
return "", errors.New("missing request in call to Ping")
}
// Here we route the ping's for the same key to the same machine
return request.Key, nil
},
},
},
)
if err != nil {
log.Fatalf("unable to wrap the worker: %q", err)
}
// now pass the ringpop adapter into the TChannel Thrift server for the PingPongService
server.Register(gen.NewTChanPingPongServiceServer(adapter))
return nil
}
func (w *worker) Ping(ctx thrift.Context, request *gen.Ping) (*gen.Pong, error) {
address, err := w.ringpop.WhoAmI()
if err != nil {
return nil, err
}
headers := ctx.Headers()
pHeader := headers["p"]
return &gen.Pong{
Message: "Hello, world!",
From_: address,
Pheader: &pHeader,
}, nil
}
func main() {
flag.Parse()
ch, err := tchannel.NewChannel("pingchannel", nil)
if err != nil {
log.Fatalf("channel did not create successfully: %v", err)
}
logger := log.StandardLogger()
rp, err := ringpop.New("ping-app",
ringpop.Channel(ch),
ringpop.Address(*hostport),
ringpop.Logger(bark.NewLoggerFromLogrus(logger)),
)
if err != nil {
log.Fatalf("Unable to create Ringpop: %v", err)
}
worker := &worker{
channel: ch,
ringpop: rp,
logger: logger,
}
if err := worker.RegisterPing(); err != nil {
log.Fatalf("could not register ping handler: %v", err)
}
if err := worker.channel.ListenAndServe(*hostport); err != nil {
log.Fatalf("could not listen on given hostport: %v", err)
}
opts := new(swim.BootstrapOptions)
opts.DiscoverProvider = jsonfile.New(*hostfile)
if _, err := worker.ringpop.Bootstrap(opts); err != nil {
log.Fatalf("ringpop bootstrap failed: %v", err)
}
select {}
}