-
Notifications
You must be signed in to change notification settings - Fork 84
/
main.go
119 lines (94 loc) · 3.75 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
// 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.
package main
import (
"fmt"
"time"
"golang.org/x/net/context"
"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/json"
)
var log = tchannel.SimpleLogger
// Ping is the ping request type.
type Ping struct {
Message string `json:"message"`
}
// Pong is the ping response type.
type Pong Ping
func pingHandler(ctx json.Context, ping *Ping) (*Pong, error) {
return &Pong{
Message: fmt.Sprintf("ping %v", ping),
}, nil
}
func pingOtherHandler(ctx json.Context, ping *Ping) (*Pong, error) {
return &Pong{
Message: fmt.Sprintf("pingOther %v", ping),
}, nil
}
func onError(ctx context.Context, err error) {
log.WithFields(tchannel.ErrField(err)).Fatal("onError handler triggered.")
}
func listenAndHandle(s *tchannel.Channel, hostPort string) {
log.Infof("Service %s", hostPort)
// If no error is returned, the listen was successful. Serving happens in the background.
if err := s.ListenAndServe(hostPort); err != nil {
log.WithFields(
tchannel.LogField{Key: "hostPort", Value: hostPort},
tchannel.ErrField(err),
).Fatal("Couldn't listen.")
}
}
func main() {
// Create a new TChannel for handling requests
ch, err := tchannel.NewChannel("PingService", &tchannel.ChannelOptions{Logger: tchannel.SimpleLogger})
if err != nil {
log.WithFields(tchannel.ErrField(err)).Fatal("Couldn't create new channel.")
}
// Register a handler for the ping message on the PingService
json.Register(ch, json.Handlers{
"ping": pingHandler,
}, onError)
// Listen for incoming requests
listenAndHandle(ch, "127.0.0.1:10500")
// Create a new TChannel for sending requests.
client, err := tchannel.NewChannel("ping-client", nil)
if err != nil {
log.WithFields(tchannel.ErrField(err)).Fatal("Couldn't create new client channel.")
}
// Make a call to ourselves, with a timeout of 10s
ctx, cancel := json.NewContext(time.Second * 10)
defer cancel()
peer := client.Peers().Add(ch.PeerInfo().HostPort)
var pong Pong
if err := json.CallPeer(ctx, peer, "PingService", "ping", &Ping{"Hello World"}, &pong); err != nil {
log.WithFields(tchannel.ErrField(err)).Fatal("json.Call failed.")
}
log.Infof("Received pong: %s", pong.Message)
// Create a new subchannel for the top-level channel
subCh := ch.GetSubChannel("PingServiceOther")
// Register a handler on the subchannel
json.Register(subCh, json.Handlers{
"pingOther": pingOtherHandler,
}, onError)
// Try to send a message to the Service:Method pair for the subchannel
if err := json.CallPeer(ctx, peer, "PingServiceOther", "pingOther", &Ping{"Hello Other World"}, &pong); err != nil {
log.WithFields(tchannel.ErrField(err)).Fatal("json.Call failed.")
}
log.Infof("Received pong: %s", pong.Message)
}