@@ -1052,6 +1052,7 @@ PyRabbitMQ_ConnectionType_init(PyRabbitMQ_Connection *self,
1052
1052
"frame_max" ,
1053
1053
"heartbeat" ,
1054
1054
"client_properties" ,
1055
+ "connect_timeout" ,
1055
1056
NULL
1056
1057
};
1057
1058
char * hostname ;
@@ -1063,11 +1064,13 @@ PyRabbitMQ_ConnectionType_init(PyRabbitMQ_Connection *self,
1063
1064
int frame_max = 131072 ;
1064
1065
int heartbeat = 0 ;
1065
1066
int port = 5672 ;
1067
+ int connect_timeout = 0 ;
1066
1068
PyObject * client_properties = NULL ;
1067
1069
1068
- if (!PyArg_ParseTupleAndKeywords (args , kwargs , "|ssssiiiiO " , kwlist ,
1070
+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "|ssssiiiiOi " , kwlist ,
1069
1071
& 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 )) {
1071
1074
return -1 ;
1072
1075
}
1073
1076
@@ -1089,6 +1092,7 @@ PyRabbitMQ_ConnectionType_init(PyRabbitMQ_Connection *self,
1089
1092
self -> channel_max = channel_max ;
1090
1093
self -> frame_max = frame_max ;
1091
1094
self -> heartbeat = heartbeat ;
1095
+ self -> connect_timeout = connect_timeout ;
1092
1096
self -> weakreflist = NULL ;
1093
1097
self -> callbacks = PyDict_New ();
1094
1098
if (self -> callbacks == NULL ) return -1 ;
@@ -1127,6 +1131,7 @@ PyRabbitMQ_Connection_connect(PyRabbitMQ_Connection *self)
1127
1131
amqp_rpc_reply_t reply ;
1128
1132
amqp_pool_t pool ;
1129
1133
amqp_table_t properties ;
1134
+ struct timeval timeout = {0 , 0 };
1130
1135
1131
1136
pyobject_array_t pyobj_array = {0 };
1132
1137
@@ -1144,7 +1149,13 @@ PyRabbitMQ_Connection_connect(PyRabbitMQ_Connection *self)
1144
1149
goto error ;
1145
1150
}
1146
1151
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
+
1148
1159
Py_END_ALLOW_THREADS ;
1149
1160
if (PyRabbitMQ_HandleAMQStatus (status , "Error opening socket" )) {
1150
1161
goto error ;
0 commit comments