Debian Codegraph @

examples/auth-locker/auth-lock-client.go

raw symbols SCIP
1 /*
2 * Minio Cloud Storage, (C) 2016 Minio, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package main
18
19 import (
20 "bufio"
21 "crypto/x509"
22 "errors"
23 "fmt"
24 "io"
25 "log"
26 "net"
27 "net/http"
28 "net/rpc"
29 "sync"
30 "time"
31
32 "github.com/minio/dsync"
33 )
34
35 // defaultDialTimeout is used for non-secure connection.
36 const defaultDialTimeout = 3 * time.Second
37
38 // ReconnectRPCClient is a wrapper type for rpc.Client which provides reconnect on first failure.
39 type ReconnectRPCClient struct {
40 mutex sync.Mutex
41 netRPCClient *rpc.Client
42 serverAddr string
43 serviceEndpoint string
44 sessionID string
45 }
46
47 // newReconnectRPCClient constructs a new ReconnectRPCClient object with given serverAddr
48 // and serviceEndpoint. It does lazy connect to serverAddr on Call().
49 func newReconnectRPCClient(serverAddr, serviceEndpoint string) *ReconnectRPCClient {
50 return &ReconnectRPCClient{
51 serverAddr: serverAddr,
52 serviceEndpoint: serviceEndpoint,
53 }
54 }
55
56 // dial tries to establish a connection to serverAddr in a safe manner.
57 // If there is a valid rpc.Cliemt, it returns that else creates a new one.
58 func (rpcClient *ReconnectRPCClient) dial() (*rpc.Client, error) {
59 rpcClient.mutex.Lock()
60 defer rpcClient.mutex.Unlock()
61
62 // Nothing to do as we already have valid connection.
63 if rpcClient.netRPCClient != nil {
64 return rpcClient.netRPCClient, nil
65 }
66
67 // Dial with 3 seconds timeout.
68 conn, err := net.DialTimeout("tcp", rpcClient.serverAddr, defaultDialTimeout)
69 if err != nil {
70 // Print RPC connection errors that are worthy to display in log
71 switch err.(type) {
72 case x509.HostnameError:
73 log.Println("Unable to establish secure connection to", rpcClient.serverAddr)
74 log.Println("error:", err)
75 }
76 return nil, &net.OpError{
77 Op: "dial-http",
78 Net: rpcClient.serverAddr + " " + rpcClient.serviceEndpoint,
79 Addr: nil,
80 Err: err,
81 }
82 }
83
84 io.WriteString(conn, "CONNECT "+rpcClient.serviceEndpoint+" HTTP/1.0\n\n")
85
86 // Require successful HTTP response before switching to RPC protocol.
87 resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
88 if err == nil && resp.Status == "200 Connected to Go RPC" {
89 netRPCClient := rpc.NewClient(conn)
90 if netRPCClient == nil {
91 return nil, &net.OpError{
92 Op: "dial-http",
93 Net: rpcClient.serverAddr + " " + rpcClient.serviceEndpoint,
94 Addr: nil,
95 Err: fmt.Errorf("Unable to initialize new rpc.Client"),
96 }
97 }
98
99 rpcClient.netRPCClient = netRPCClient
100
101 return netRPCClient, nil
102 }
103
104 if err == nil {
105 err = errors.New("unexpected HTTP response: " + resp.Status)
106 }
107
108 conn.Close()
109 return nil, &net.OpError{
110 Op: "dial-http",
111 Net: rpcClient.serverAddr + " " + rpcClient.serviceEndpoint,
112 Addr: nil,
113 Err: err,
114 }
115 }
116
117 // Call makes a RPC call to the remote endpoint using the default
118 // codec, namely encoding/gob. It logs in automatically when session ID
119 // is empty.
120 func (rpcClient *ReconnectRPCClient) Call(serviceMethod string, args interface{}, reply interface{}) error {
121 // Get a new or existing rpc.Client.
122 netRPCClient, err := rpcClient.dial()
123 if err != nil {
124 return err
125 }
126
127 rpcClient.mutex.Lock()
128 if rpcClient.sessionID == "" {
129 loginArgs := NewLoginArgs("admin", "adm1npasswo7d")
130 if err = netRPCClient.Call("LockServer.Login", &loginArgs, &rpcClient.sessionID); err != nil {
131 rpcClient.mutex.Unlock()
132 if err == rpc.ErrShutdown {
133 rpcClient.Close()
134 }
135 return err
136 }
137 }
138 rpcClient.mutex.Unlock()
139
140 err = netRPCClient.Call(serviceMethod, args, reply)
141 if err == rpc.ErrShutdown {
142 netRPCClient.Close()
143 }
144 return err
145 }
146
147 func (rpcClient *ReconnectRPCClient) RLock(args dsync.LockArgs) (status bool, err error) {
148 lockArgs := NewLockArgs(args, rpcClient.sessionID)
149 err = rpcClient.Call("LockServer.RLock", &lockArgs, &status)
150 if err == rpc.ErrShutdown {
151 err = rpcClient.Call("LockServer.RLock", &lockArgs, &status)
152 }
153 return status, err
154 }
155
156 func (rpcClient *ReconnectRPCClient) Lock(args dsync.LockArgs) (status bool, err error) {
157 lockArgs := NewLockArgs(args, rpcClient.sessionID)
158 err = rpcClient.Call("LockServer.Lock", &lockArgs, &status)
159 if err == rpc.ErrShutdown {
160 err = rpcClient.Call("LockServer.Lock", &lockArgs, &status)
161 }
162 return status, err
163 }
164
165 func (rpcClient *ReconnectRPCClient) RUnlock(args dsync.LockArgs) (status bool, err error) {
166 lockArgs := NewLockArgs(args, rpcClient.sessionID)
167 err = rpcClient.Call("LockServer.RUnlock", &lockArgs, &status)
168 if err == rpc.ErrShutdown {
169 err = rpcClient.Call("LockServer.RUnlock", &lockArgs, &status)
170 }
171 return status, err
172 }
173
174 func (rpcClient *ReconnectRPCClient) Unlock(args dsync.LockArgs) (status bool, err error) {
175 lockArgs := NewLockArgs(args, rpcClient.sessionID)
176 err = rpcClient.Call("LockServer.Unlock", &lockArgs, &status)
177 if err == rpc.ErrShutdown {
178 err = rpcClient.Call("LockServer.Unlock", &lockArgs, &status)
179 }
180 return status, err
181 }
182
183 func (rpcClient *ReconnectRPCClient) ForceUnlock(args dsync.LockArgs) (status bool, err error) {
184 lockArgs := NewLockArgs(args, rpcClient.sessionID)
185 err = rpcClient.Call("LockServer.ForceUnlock", &lockArgs, &status)
186 if err == rpc.ErrShutdown {
187 err = rpcClient.Call("LockServer.ForceUnlock", &lockArgs, &status)
188 }
189 return status, err
190 }
191
192 // Close closes the underlying socket file descriptor.
193 func (rpcClient *ReconnectRPCClient) Close() (err error) {
194 rpcClient.mutex.Lock()
195 netRPCClient := rpcClient.netRPCClient
196 rpcClient.mutex.Unlock()
197
198 if netRPCClient != nil {
199 var status bool
200 netRPCClient.Call("LockServer.Logout", &rpcClient.sessionID, &status)
201
202 rpcClient.mutex.Lock()
203 rpcClient.netRPCClient = nil
204 rpcClient.mutex.Unlock()
205
206 netRPCClient.Close()
207 }
208
209 return nil
210 }
211
212 func (rpcClient *ReconnectRPCClient) ServerAddr() string {
213 return rpcClient.serverAddr
214 }
215
216 func (rpcClient *ReconnectRPCClient) ServiceEndpoint() string {
217 return rpcClient.serviceEndpoint
218 }