1
- require 'net/tcp_client'
2
1
require 'netstring'
3
2
require 'logger'
4
3
require 'benchmark'
@@ -18,22 +17,34 @@ def initialize(uri, options = {})
18
17
timeout = @options . fetch ( :timeout , 5 )
19
18
connect_timeout = @options . fetch ( :connect_timeout , timeout )
20
19
read_timeout = @options . fetch ( :read_timeout , timeout )
21
- write_timeout = @options . fetch ( :write_timeout , nil ) # default 60
22
- connect_retry_count = @options . fetch ( :connect_retry_count , nil ) # default 10
20
+ write_timeout = @options . fetch ( :write_timeout , 60 ) # default 60
21
+ connect_retry_count = @options . fetch ( :connect_retry_count , 10 ) # default 10
22
+ @close_after_sent = @options . fetch ( :close_after_sent , false )
23
23
24
- @transport = Net ::TCPClient . new server : @uri ,
25
- connect_retry_count : connect_retry_count ,
26
- connect_timeout : connect_timeout ,
27
- read_timeout : read_timeout ,
28
- write_timeout : write_timeout ,
29
- buffered : false # recommended for RPC
30
- rescue ::SocketError
31
- raise ConnectionError , "Can't connect to #{ @uri } "
24
+ @transport = JRPC ::Transport ::SocketTcp . new server : @uri ,
25
+ connect_retry_count : connect_retry_count ,
26
+ connect_timeout : connect_timeout ,
27
+ read_timeout : read_timeout ,
28
+ write_timeout : write_timeout
29
+ begin
30
+ @transport . connect
31
+ rescue JRPC ::SocketTcp ::Error
32
+ raise ConnectionError , "Can't connect to #{ @uri } "
33
+ end
32
34
end
33
35
34
36
private
35
37
36
- def send_command ( request , options = { } )
38
+ def ensure_connected
39
+ if @transport . closed?
40
+ logger . debug { 'Connecting transport...' }
41
+ @transport . connect
42
+ logger . debug { 'Connected.' }
43
+ end
44
+ end
45
+
46
+ def send_command ( request , options = { } )
47
+ ensure_connected
37
48
read_timeout = options . fetch ( :read_timeout )
38
49
write_timeout = options . fetch ( :write_timeout )
39
50
response = nil
@@ -49,15 +60,20 @@ def send_command(request, options={})
49
60
"(#{ '%.2f' % ( t * 1000 ) } ms) Response message: #{ Utils . truncate ( response , MAX_LOGGED_MESSAGE_LENGTH ) } "
50
61
end
51
62
response
63
+ ensure
64
+ @transport . close if @close_after_sent
52
65
end
53
66
54
- def send_notification ( request , options = { } )
67
+ def send_notification ( request , options = { } )
68
+ ensure_connected
55
69
write_timeout = options . fetch ( :write_timeout )
56
70
logger . debug { "Request address: #{ uri } " }
57
71
logger . debug { "Request message: #{ Utils . truncate ( request , MAX_LOGGED_MESSAGE_LENGTH ) } " }
58
72
logger . debug { "Request write_timeout: #{ write_timeout } " }
59
73
send_request ( request , write_timeout )
60
74
logger . debug { 'No response required' }
75
+ ensure
76
+ @transport . close if @close_after_sent
61
77
end
62
78
63
79
def create_message ( method , params )
@@ -74,8 +90,7 @@ def send_request(request, timeout)
74
90
def receive_response ( timeout )
75
91
timeout ||= @transport . read_timeout
76
92
length = get_msg_length ( timeout )
77
- response = ''
78
- @transport . read ( length +1 , response , timeout )
93
+ response = @transport . read ( length + 1 , timeout )
79
94
raise ClientError . new ( 'invalid response. missed comma as terminator' ) if response [ -1 ] != ','
80
95
response . chomp ( ',' )
81
96
rescue ::SocketError
@@ -85,7 +100,7 @@ def receive_response(timeout)
85
100
def get_msg_length ( timeout )
86
101
length = ''
87
102
while true do
88
- character = @transport . read ( 1 , nil , timeout )
103
+ character = @transport . read ( 1 , timeout )
89
104
break if character == ':'
90
105
length += character
91
106
end
0 commit comments