MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / cdb2_socket_pool_get_ll

Function cdb2_socket_pool_get_ll

cdb2api/cdb2api.c:1964–2071  ·  view source on GitHub ↗

cdb2_socket_pool_get_ll: low-level

Source from the content-addressed store, hash-verified

1962
1963// cdb2_socket_pool_get_ll: low-level
1964static int cdb2_socket_pool_get_ll(const char *typestr, int dbnum, int *port)
1965{
1966 int sockpool_fd = -1;
1967 int enabled = 0;
1968 int sp_generation = -1;
1969 int fd = -1;
1970
1971 pthread_mutex_lock(&cdb2_sockpool_mutex);
1972 if (sockpool_enabled == 0) {
1973 time_t current_time = time(NULL);
1974 /* Check every 10 seconds. */
1975 if ((current_time - sockpool_fail_time) > 10) {
1976 sockpool_enabled = 1;
1977 }
1978 }
1979 enabled = sockpool_enabled;
1980 if (enabled == 1) {
1981 sp_generation = sockpool_generation;
1982 sockpool_fd = sockpool_get_from_pool();
1983 }
1984 pthread_mutex_unlock(&cdb2_sockpool_mutex);
1985
1986 if (enabled != 1) {
1987 return -1;
1988 }
1989
1990 if (sockpool_fd == -1) {
1991 sockpool_fd = open_sockpool_ll();
1992 if (sockpool_fd == -1) {
1993 pthread_mutex_lock(&cdb2_sockpool_mutex);
1994 sockpool_enabled = 0;
1995 sockpool_fail_time = time(NULL);
1996 pthread_mutex_unlock(&cdb2_sockpool_mutex);
1997 return -1;
1998 }
1999 }
2000
2001 struct sockpool_msg_vers0 msg = {0};
2002 if (strlen(typestr) >= sizeof(msg.typestr)) {
2003 int closeit = 0;
2004 pthread_mutex_lock(&cdb2_sockpool_mutex);
2005 if (sp_generation == sockpool_generation) {
2006 if ((sockpool_place_fd_in_pool(sockpool_fd)) != 0) {
2007 closeit = 1;
2008 }
2009 } else {
2010 sockpool_remove_fd(sockpool_fd);
2011 closeit = 1;
2012 }
2013 pthread_mutex_unlock(&cdb2_sockpool_mutex);
2014 if (closeit)
2015 close(sockpool_fd);
2016 return -1;
2017 }
2018 /* Please may I have a file descriptor */
2019 msg.request = SOCKPOOL_REQUEST;
2020 msg.dbnum = dbnum;
2021 strncpy(msg.typestr, typestr, sizeof(msg.typestr) - 1);

Callers 1

cdb2_socket_pool_getFunction · 0.85

Calls 6

sockpool_get_from_poolFunction · 0.85
sockpool_remove_fdFunction · 0.85
open_sockpool_llFunction · 0.70
send_fdFunction · 0.70
recv_fdFunction · 0.70

Tested by

no test coverage detected