-
Notifications
You must be signed in to change notification settings - Fork 100
/
main.go
188 lines (160 loc) · 4.93 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
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
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2019 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 (
"bufio"
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"go.uber.org/yarpc"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/encoding/json"
"go.uber.org/yarpc/transport/grpc"
"go.uber.org/yarpc/transport/http"
"go.uber.org/yarpc/transport/tchannel"
)
type getRequest struct {
Key string `json:"key"`
}
type getResponse struct {
Value string `json:"value"`
}
type setRequest struct {
Key string `json:"key"`
Value string `json:"value"`
}
type setResponse struct {
}
func get(ctx context.Context, c json.Client, k string) (string, error) {
var response getResponse
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
err := c.Call(ctx, "get", &getRequest{Key: k}, &response)
return response.Value, err
}
func set(ctx context.Context, c json.Client, k string, v string) error {
var response setResponse
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
return c.Call(ctx, "set", &setRequest{Key: k, Value: v}, &response)
}
type requestLogOutboundMiddleware struct{}
func (requestLogOutboundMiddleware) Call(
ctx context.Context, request *transport.Request, out transport.UnaryOutbound) (*transport.Response, error) {
fmt.Printf("sending request %q to service %q (encoding %q)\n", request.Procedure,
request.Service, request.Encoding)
return out.Call(ctx, request)
}
func main() {
if err := do(); err != nil {
log.Fatal(err)
}
}
func do() error {
flagSet := flag.NewFlagSet("json-keyvalue", flag.ExitOnError)
outboundName := ""
flagSet.StringVar(
&outboundName,
"outbound", "", "name of the outbound to use (http/tchannel/grpc)",
)
if err := flagSet.Parse(os.Args[1:]); err != nil {
return err
}
httpTransport := http.NewTransport()
tchannelTransport, err := tchannel.NewChannelTransport(tchannel.ServiceName("keyvalue-client"))
if err != nil {
return err
}
var outbound transport.UnaryOutbound
switch strings.ToLower(outboundName) {
case "http":
outbound = httpTransport.NewSingleOutbound("http://127.0.0.1:24034")
case "tchannel":
outbound = tchannelTransport.NewSingleOutbound("127.0.0.1:28941")
case "grpc":
outbound = grpc.NewTransport().NewSingleOutbound("127.0.0.1:24038")
default:
return fmt.Errorf("invalid outbound: %q", outboundName)
}
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: "keyvalue-client",
Outbounds: yarpc.Outbounds{
"keyvalue": {
Unary: outbound,
},
},
OutboundMiddleware: yarpc.OutboundMiddleware{
Unary: yarpc.UnaryOutboundMiddleware(requestLogOutboundMiddleware{}),
},
})
if err := dispatcher.Start(); err != nil {
return fmt.Errorf("failed to start Dispatcher: %v", err)
}
defer dispatcher.Stop()
client := json.New(dispatcher.ClientConfig("keyvalue"))
scanner := bufio.NewScanner(os.Stdin)
rootCtx := context.Background()
for scanner.Scan() {
line := scanner.Text()
args := strings.Split(line, " ")
if len(args) < 1 || len(args[0]) < 3 {
continue
}
cmd := args[0]
args = args[1:]
switch cmd {
case "get":
if len(args) != 1 {
fmt.Println("usage: get key")
continue
}
key := args[0]
ctx, cancel := context.WithTimeout(rootCtx, 100*time.Millisecond)
defer cancel()
if value, err := get(ctx, client, key); err != nil {
fmt.Printf("get %q failed: %s\n", key, err)
} else {
fmt.Println(key, "=", value)
}
continue
case "set":
if len(args) != 2 {
fmt.Println("usage: set key value")
continue
}
key, value := args[0], args[1]
ctx, cancel := context.WithTimeout(rootCtx, 100*time.Millisecond)
defer cancel()
if err := set(ctx, client, key, value); err != nil {
fmt.Printf("set %q = %q failed: %v\n", key, value, err.Error())
}
continue
case "exit":
return nil
default:
return fmt.Errorf("invalid command, valid commands are: get, set, exit: %s", cmd)
}
}
return scanner.Err()
}