@@ -1052,6 +1052,7 @@ PyRabbitMQ_ConnectionType_init(PyRabbitMQ_Connection *self,
10521052 "frame_max" ,
10531053 "heartbeat" ,
10541054 "client_properties" ,
1055+ "connect_timeout" ,
10551056 NULL
10561057 };
10571058 char * hostname ;
@@ -1063,11 +1064,13 @@ PyRabbitMQ_ConnectionType_init(PyRabbitMQ_Connection *self,
10631064 int frame_max = 131072 ;
10641065 int heartbeat = 0 ;
10651066 int port = 5672 ;
1067+ int connect_timeout = 0 ;
10661068 PyObject * client_properties = NULL ;
10671069
1068- if (!PyArg_ParseTupleAndKeywords (args , kwargs , "|ssssiiiiO " , kwlist ,
1070+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "|ssssiiiiOi " , kwlist ,
10691071 & hostname , & userid , & password , & virtual_host , & port ,
1070- & channel_max , & frame_max , & heartbeat , & client_properties )) {
1072+ & channel_max , & frame_max , & heartbeat , & client_properties ,
1073+ & connect_timeout )) {
10711074 return -1 ;
10721075 }
10731076
@@ -1089,6 +1092,7 @@ PyRabbitMQ_ConnectionType_init(PyRabbitMQ_Connection *self,
10891092 self -> channel_max = channel_max ;
10901093 self -> frame_max = frame_max ;
10911094 self -> heartbeat = heartbeat ;
1095+ self -> connect_timeout = connect_timeout ;
10921096 self -> weakreflist = NULL ;
10931097 self -> callbacks = PyDict_New ();
10941098 if (self -> callbacks == NULL ) return -1 ;
@@ -1127,6 +1131,7 @@ PyRabbitMQ_Connection_connect(PyRabbitMQ_Connection *self)
11271131 amqp_rpc_reply_t reply ;
11281132 amqp_pool_t pool ;
11291133 amqp_table_t properties ;
1134+ struct timeval timeout = {0 , 0 };
11301135
11311136 pyobject_array_t pyobj_array = {0 };
11321137
@@ -1144,7 +1149,13 @@ PyRabbitMQ_Connection_connect(PyRabbitMQ_Connection *self)
11441149 goto error ;
11451150 }
11461151 Py_BEGIN_ALLOW_THREADS ;
1147- status = amqp_socket_open (socket , self -> hostname , self -> port );
1152+ if (self -> connect_timeout <= 0 ) {
1153+ status = amqp_socket_open (socket , self -> hostname , self -> port );
1154+ } else {
1155+ timeout .tv_sec = self -> connect_timeout ;
1156+ status = amqp_socket_open_noblock (socket , self -> hostname , self -> port , & timeout );
1157+ }
1158+
11481159 Py_END_ALLOW_THREADS ;
11491160 if (PyRabbitMQ_HandleAMQStatus (status , "Error opening socket" )) {
11501161 goto error ;
0 commit comments