4 module MogileFS::Network
5 # given an array of URIs, verify that at least one of them is accessible
6 # with the expected HTTP code within the timeout period (in seconds).
7 def verify_uris(uris = [], expect = '200', timeout = 2.00)
12 # first, we asynchronously connect to all of them
14 sock = Socket.mogilefs_new_nonblock(uri.host, uri.port) rescue next
18 # wait for at least one of them to finish connecting and send
19 # HTTP requests to the connected ones
22 r = IO.select(nil, uri_socks.keys, nil, timeout > 0 ? timeout : 0)
23 timeout -= (Time.now - t0)
24 break unless r && r[1]
27 sock.syswrite "HEAD #{uri_socks[sock].request_uri} HTTP/1.0\r\n\r\n"
33 end until sockets[0] || timeout < 0
35 # Await a response from the sockets we had written to, we only need one
36 # valid response, but we'll take more if they return simultaneously
40 r = IO.select(sockets, nil, nil, timeout > 0 ? timeout : 0)
41 timeout -= (Time.now - t0)
42 break unless r && r[0]
44 buf = sock.recv_nonblock(128, Socket::MSG_PEEK) rescue next
45 if buf && /\AHTTP\/[\d\.]+ #{expect} / =~ buf
46 ok_uris << uri_socks.delete(sock)
51 end until ok_uris[0] || timeout < 0
55 uri_socks.keys.each { |sock| sock.close rescue nil }
58 end # module MogileFS::Network