@@ -94,47 +94,23 @@ public function __construct(array $servers, ClientInterface $client = null)
9494 */
9595 public function execute (string $ query , array $ parameters ): CollectionInterface
9696 {
97- $ numServers = count ($ this ->availableServers ) - 1 ;
98-
99- for ($ i = 0 ; $ i <= $ numServers ; $ i ++) {
100- // always get the first available server
101- $ server = $ this ->availableServers [0 ];
102-
103- // Move the selected server to the end of the stack
104- $ this ->availableServers [] = array_shift ($ this ->availableServers );
105-
106- try {
107- return $ this ->sendRequest ($ server , $ query , $ parameters , false );
108- } catch (ConnectException $ exception ) {
109- // Catch it before the BadResponseException but do nothing.
110- continue ;
111- } catch (BadResponseException $ exception ) {
112- $ body = (string )$ exception ->getResponse ()->getBody ();
113- $ json = json_decode ($ body , true );
114-
115- if ($ json === null && json_last_error () !== JSON_ERROR_NONE ) {
116- throw new RuntimeException (sprintf ('Server returned non-JSON response: %s ' , $ body ), 0 , $ exception );
117- }
118-
119- $ errorCode = $ json ['error ' ]['code ' ];
120- $ errorMessage = $ json ['error ' ]['message ' ];
121-
122- throw new RuntimeException ($ errorMessage , $ errorCode , $ exception );
123- }
124- }
125-
126- throw new ConnectException (
127- sprintf ('No more servers available, exception from last server: %s ' , $ exception ->getMessage ()),
128- $ exception ->getRequest (),
129- $ exception
130- );
97+ return $ this ->executeGeneric ($ query , $ parameters , false );
13198 }
13299
133100 /**
134101 * {@Inheritdoc}
135102 * @throws \GuzzleHttp\Exception\ConnectException
136103 */
137104 public function executeBulk (string $ query , array $ parameters ): BulkResponseInterface
105+ {
106+ return $ this ->executeGeneric ($ query , $ parameters , true );
107+ }
108+
109+ /**
110+ * {@Inheritdoc}
111+ * @throws \GuzzleHttp\Exception\ConnectException
112+ */
113+ private function executeGeneric (string $ query , array $ parameters , bool $ bulk_mode = false )
138114 {
139115 $ numServers = count ($ this ->availableServers ) - 1 ;
140116
@@ -146,7 +122,7 @@ public function executeBulk(string $query, array $parameters): BulkResponseInter
146122 $ this ->availableServers [] = array_shift ($ this ->availableServers );
147123
148124 try {
149- return $ this ->sendRequest ($ server , $ query , $ parameters , true );
125+ return $ this ->sendRequest ($ server , $ query , $ parameters , $ bulk_mode );
150126 } catch (ConnectException $ exception ) {
151127 // Catch it before the BadResponseException but do nothing.
152128 continue ;
0 commit comments