mirror of
https://github.com/rkd77/elinks.git
synced 2025-01-03 14:57:44 -05:00
Merge pull request #297 from smemsh/nowait-connect
remove 300ms startup delay and optimize fork_on_start
This commit is contained in:
commit
9ae8dd786b
@ -321,11 +321,6 @@ setsock_reuse_addr(int fd)
|
|||||||
/* Number of connections in listen backlog. */
|
/* Number of connections in listen backlog. */
|
||||||
#define LISTEN_BACKLOG 100
|
#define LISTEN_BACKLOG 100
|
||||||
|
|
||||||
/* Max. number of connect attempts. */
|
|
||||||
#define MAX_CONNECT_TRIES 3
|
|
||||||
/* Base delay in useconds between connect attempts. */
|
|
||||||
#define CONNECT_TRIES_DELAY 50000
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
report_af_unix_error(const char *function, int error)
|
report_af_unix_error(const char *function, int error)
|
||||||
{
|
{
|
||||||
@ -443,31 +438,24 @@ free_and_error:
|
|||||||
static int
|
static int
|
||||||
connect_to_af_unix(void)
|
connect_to_af_unix(void)
|
||||||
{
|
{
|
||||||
int attempts = 0;
|
|
||||||
int pf = get_address(&s_info_connect, ADDR_IP_CLIENT);
|
int pf = get_address(&s_info_connect, ADDR_IP_CLIENT);
|
||||||
|
|
||||||
while (pf != -1 && attempts++ < MAX_CONNECT_TRIES) {
|
if (pf != -1) {
|
||||||
int saved_errno;
|
|
||||||
|
|
||||||
s_info_connect.fd = socket(pf, SOCK_STREAM, 0);
|
s_info_connect.fd = socket(pf, SOCK_STREAM, 0);
|
||||||
if (s_info_connect.fd == -1) {
|
if (s_info_connect.fd == -1) {
|
||||||
report_af_unix_error("socket()", errno);
|
report_af_unix_error("socket()", errno);
|
||||||
break;
|
} else {
|
||||||
}
|
if (connect(s_info_connect.fd, s_info_connect.addr,
|
||||||
|
s_info_connect.size) >= 0) {
|
||||||
if (connect(s_info_connect.fd, s_info_connect.addr,
|
|
||||||
s_info_connect.size) >= 0)
|
|
||||||
return s_info_connect.fd;
|
return s_info_connect.fd;
|
||||||
|
}
|
||||||
saved_errno = errno;
|
if (errno != ECONNREFUSED && errno != ENOENT) {
|
||||||
close(s_info_connect.fd);
|
report_af_unix_error("connect()", errno);
|
||||||
|
}
|
||||||
if (saved_errno != ECONNREFUSED && saved_errno != ENOENT) {
|
else if (close(s_info_connect.fd) == -1) {
|
||||||
report_af_unix_error("connect()", errno);
|
report_af_unix_error("close(afsock)", errno);
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
elinks_usleep(CONNECT_TRIES_DELAY * attempts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mem_free_set(&s_info_connect.addr, NULL);
|
mem_free_set(&s_info_connect.addr, NULL);
|
||||||
@ -517,6 +505,9 @@ done_interlink(void)
|
|||||||
int
|
int
|
||||||
init_interlink(void)
|
init_interlink(void)
|
||||||
{
|
{
|
||||||
|
int pipefds[2];
|
||||||
|
char trigger;
|
||||||
|
ssize_t n;
|
||||||
int fd = connect_to_af_unix();
|
int fd = connect_to_af_unix();
|
||||||
|
|
||||||
if (fd != -1 || remote_session_flags) return fd;
|
if (fd != -1 || remote_session_flags) return fd;
|
||||||
@ -525,29 +516,52 @@ init_interlink(void)
|
|||||||
|
|
||||||
if (get_opt_bool("ui.sessions.fork_on_start", NULL)) {
|
if (get_opt_bool("ui.sessions.fork_on_start", NULL)) {
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid;
|
||||||
|
if (pipe(pipefds) == -1) {
|
||||||
|
report_af_unix_error("pipe()", errno);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
pid = fork();
|
||||||
|
|
||||||
if (pid == -1) return -1;
|
if (pid == -1) return -1;
|
||||||
|
|
||||||
if (pid > 0) {
|
if (pid > 0) {
|
||||||
int i;
|
int error = 0;
|
||||||
|
master_pid = pid;
|
||||||
|
|
||||||
for (i = 1; i <= (MAX_BIND_TRIES+2); ++i) {
|
/* wait for forked child to complete the bind() */
|
||||||
fd = connect_to_af_unix();
|
if ((n = read(pipefds[0], &trigger, 1)) <= 0) {
|
||||||
|
if (n == 0) {
|
||||||
if (fd != -1) {
|
errno = EPIPE; /* write end closed */
|
||||||
master_pid = pid;
|
|
||||||
return fd;
|
|
||||||
}
|
}
|
||||||
elinks_usleep(BIND_TRIES_DELAY * i);
|
report_af_unix_error("read()", errno);
|
||||||
|
error = 1;
|
||||||
}
|
}
|
||||||
return -1;
|
if (close(pipefds[0]) == -1) {
|
||||||
|
report_af_unix_error("close(pipe_r)", errno);
|
||||||
|
error = 1;
|
||||||
|
}
|
||||||
|
if (error) return -1;
|
||||||
|
|
||||||
|
return connect_to_af_unix();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* child */
|
/* child */
|
||||||
master_pid = getpid();
|
master_pid = getpid();
|
||||||
close_terminal_pipes();
|
close_terminal_pipes();
|
||||||
}
|
}
|
||||||
bind_to_af_unix();
|
bind_to_af_unix();
|
||||||
|
if (get_opt_bool("ui.sessions.fork_on_start", NULL)) {
|
||||||
|
if ((n = write(pipefds[1], &trigger, 1)) <= 0) {
|
||||||
|
if (n == 0) {
|
||||||
|
errno = EPIPE; /* read end closed */
|
||||||
|
}
|
||||||
|
report_af_unix_error("write()", errno);
|
||||||
|
}
|
||||||
|
if (close(pipefds[1]) == -1) {
|
||||||
|
report_af_unix_error("close(pipe_w)", errno);
|
||||||
|
}
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -555,5 +569,3 @@ init_interlink(void)
|
|||||||
#undef MAX_BIND_TRIES
|
#undef MAX_BIND_TRIES
|
||||||
#undef BIND_TRIES_DELAY
|
#undef BIND_TRIES_DELAY
|
||||||
#undef LISTEN_BACKLOG
|
#undef LISTEN_BACKLOG
|
||||||
#undef MAX_CONNECT_TRIES
|
|
||||||
#undef CONNECT_TRIES_DELAY
|
|
||||||
|
Loading…
Reference in New Issue
Block a user