Debian Codegraph @

rpc-client-impl_test.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 dsync_test
18
19 import (
20 "net/rpc"
21 "sync"
22
23 . "github.com/minio/dsync"
24 )
25
26 // ReconnectRPCClient is a wrapper type for rpc.Client which provides reconnect on first failure.
27 type ReconnectRPCClient struct {
28 mutex sync.Mutex
29 rpc *rpc.Client
30 addr string
31 endpoint string
32 }
33
34 // newClient constructs a ReconnectRPCClient object with addr and endpoint initialized.
35 // It _doesn't_ connect to the remote endpoint. See Call method to see when the
36 // connect happens.
37 func newClient(addr, endpoint string) NetLocker {
38 return &ReconnectRPCClient{
39 addr: addr,
40 endpoint: endpoint,
41 }
42 }
43
44 // Close closes the underlying socket file descriptor.
45 func (rpcClient *ReconnectRPCClient) Close() error {
46 rpcClient.mutex.Lock()
47 defer rpcClient.mutex.Unlock()
48 // If rpc client has not connected yet there is nothing to close.
49 if rpcClient.rpc == nil {
50 return nil
51 }
52 // Reset rpcClient.rpc to allow for subsequent calls to use a new
53 // (socket) connection.
54 clnt := rpcClient.rpc
55 rpcClient.rpc = nil
56 return clnt.Close()
57 }
58
59 // Call makes a RPC call to the remote endpoint using the default codec, namely encoding/gob.
60 func (rpcClient *ReconnectRPCClient) Call(serviceMethod string, args interface{}, reply interface{}) (err error) {
61 rpcClient.mutex.Lock()
62 defer rpcClient.mutex.Unlock()
63 dialCall := func() error {
64 // If the rpc.Client is nil, we attempt to (re)connect with the remote endpoint.
65 if rpcClient.rpc == nil {
66 clnt, derr := rpc.DialHTTPPath("tcp", rpcClient.addr, rpcClient.endpoint)
67 if derr != nil {
68 return derr
69 }
70 rpcClient.rpc = clnt
71 }
72 // If the RPC fails due to a network-related error, then we reset
73 // rpc.Client for a subsequent reconnect.
74 return rpcClient.rpc.Call(serviceMethod, args, reply)
75 }
76 if err = dialCall(); err == rpc.ErrShutdown {
77 rpcClient.rpc.Close()
78 rpcClient.rpc = nil
79 err = dialCall()
80 }
81 return err
82 }
83
84 func (rpcClient *ReconnectRPCClient) RLock(args LockArgs) (status bool, err error) {
85 err = rpcClient.Call("Dsync.RLock", &args, &status)
86 return status, err
87 }
88
89 func (rpcClient *ReconnectRPCClient) Lock(args LockArgs) (status bool, err error) {
90 err = rpcClient.Call("Dsync.Lock", &args, &status)
91 return status, err
92 }
93
94 func (rpcClient *ReconnectRPCClient) RUnlock(args LockArgs) (status bool, err error) {
95 err = rpcClient.Call("Dsync.RUnlock", &args, &status)
96 return status, err
97 }
98
99 func (rpcClient *ReconnectRPCClient) Unlock(args LockArgs) (status bool, err error) {
100 err = rpcClient.Call("Dsync.Unlock", &args, &status)
101 return status, err
102 }
103
104 func (rpcClient *ReconnectRPCClient) ForceUnlock(args LockArgs) (status bool, err error) {
105 err = rpcClient.Call("Dsync.ForceUnlock", &args, &status)
106 return status, err
107 }
108
109 func (rpcClient *ReconnectRPCClient) ServerAddr() string {
110 return rpcClient.addr
111 }
112
113 func (rpcClient *ReconnectRPCClient) ServiceEndpoint() string {
114 return rpcClient.endpoint
115 }