Skip to content

Instantly share code, notes, and snippets.

@cheeming
Created August 5, 2012 11:19
Show Gist options
  • Save cheeming/3263946 to your computer and use it in GitHub Desktop.
Save cheeming/3263946 to your computer and use it in GitHub Desktop.
Patch to fix nginx gridfs module to allow connection to secondary/slave mongodb instances. This patch will apply cleanly on revision 76225528b3903fc9eafcb8163c7ebfdf34237e8b for https://github.com/mdirolf/nginx-gridfs.
diff --git a/ngx_http_gridfs_module.c b/ngx_http_gridfs_module.c
index 77ef430..859dff1 100644
--- a/ngx_http_gridfs_module.c
+++ b/ngx_http_gridfs_module.c
@@ -490,7 +490,7 @@ static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_grid
if ( gridfs_loc_conf->mongods->nelts == 1 ) {
ngx_cpystrn( host, mongods[0].host.data, mongods[0].host.len + 1 );
- status = mongo_connect( &mongo_conn->conn, (const char*)host, mongods[0].port );
+ status = mongo_connect( &mongo_conn->conn, (const char*)host, mongods[0].port, MONGO_SECONDARY_OK );
} else if ( gridfs_loc_conf->mongods->nelts >= 2 && gridfs_loc_conf->mongods->nelts < 9 ) {
/* Initiate replica set connection. */
@@ -501,7 +501,7 @@ static ngx_int_t ngx_http_mongo_add_connection(ngx_cycle_t* cycle, ngx_http_grid
ngx_cpystrn( host, mongods[i].host.data, mongods[i].host.len + 1 );
mongo_replset_add_seed( &mongo_conn->conn, (const char *)host, mongods[i].port );
}
- status = mongo_replset_connect( &mongo_conn->conn );
+ status = mongo_replset_connect( &mongo_conn->conn, MONGO_SECONDARY_OK );
} else {
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
"Mongo Nginx Exception: Too many strings provided in 'mongo' directive.");
@@ -573,7 +573,7 @@ static ngx_int_t ngx_http_mongo_reconnect(ngx_log_t *log, ngx_http_mongo_connect
if (&mongo_conn->conn.connected) {
mongo_disconnect(&mongo_conn->conn);
ngx_msleep(MONGO_RECONNECT_WAITTIME);
- status = mongo_reconnect(&mongo_conn->conn);
+ status = mongo_reconnect(&mongo_conn->conn, MONGO_SECONDARY_OK);
} else {
status = MONGO_CONN_FAIL;
}
@cheeming
Copy link
Author

cheeming commented Aug 5, 2012

@cheeming
Copy link
Author

cheeming commented Aug 5, 2012

Disclaimer: This patch is definitely not perfect. Hardly done major testing but it works for my setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment