forked from youtube/doorman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorman_client.go
80 lines (64 loc) · 2.21 KB
/
doorman_client.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
// Copyright 2016 Google, 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.
// Command line client for Doorman.
//
// Usage:
// doorman_client --server=localhost:9999 --resource=test_resource --wants=100 --client_id=foo
package main
import (
"flag"
"fmt"
log "github.com/golang/glog"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/youtube/doorman/go/client/doorman"
)
var (
server = flag.String("server", "", "Address of the doorman server")
resource = flag.String("resource", "", "Name of the resource to request capacity for")
wants = flag.Float64("wants", 0, "Amount of capacity to request")
clientID = flag.String("client_id", "", "Client id to use")
caFile = flag.String("ca_file", "", "The file containning the CA root cert file to connect over TLS (otherwise plain TCP will be used)")
)
func main() {
flag.Parse()
if *server == "" || *resource == "" {
log.Exit("both --server and --resource must be specified")
}
if *clientID == "" {
log.Exit("--client_id must be set")
}
var opts []grpc.DialOption
if len(*caFile) != 0 {
var creds credentials.TransportAuthenticator
var err error
creds, err = credentials.NewClientTLSFromFile(*caFile, "")
if err != nil {
log.Exitf("Failed to create TLS credentials %v", err)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
}
client, err := doorman.NewWithID(*server, *clientID, doorman.DialOpts(opts...))
if err != nil {
log.Exitf("could not create client: %v", err)
}
defer client.Close()
resource, err := client.Resource(*resource, *wants)
if err != nil {
log.Exitf("could not acquire resource: %v", err)
}
fmt.Println(<-resource.Capacity())
}