/* Returns 1 if a failover server candidate is available.
   Close connection and return 0 if none was found.
*/
int failover_server(int conn)
{
	int server = conns[conn].server;
	DEBUG(2, "failover_server(%d)", conn);
	if (server_alg & ALG_STUBBORN) {
		DEBUG(2, "Won't failover because we are stubborn");
		close_conn(conn);
		return 0;
	}
	if (server == abuse_server) {
		DEBUG(2, "Won't failover from abuse server");
		close_conn(conn);
		return 0;
	}
	if (server == emerg_server) {
		DEBUG(2, "Already using emergency server, won't fail over");
		close_conn(conn);
		return 0;
	}
	if (conns[conn].upfd != -1) {
		close(conns[conn].upfd);
		conns[conn].upfd = -1;
	}
	do {
		server = (server+1) % nservers;
		DEBUG(2, "Intend to try server %d", server);
		if (try_server(server, conn)) return 1;
	} while (server != conns[conn].initial);
	DEBUG(1, "using emergency server, remember to reset flag");
	emergency = 1;
	if (try_server(emerg_server, conn)) return 1;
	close_conn(conn);
	return 0;
}
BaconMessageConnection *
bacon_message_connection_new (const char *prefix)
{
	BaconMessageConnection *conn;

	g_return_val_if_fail (prefix != NULL, NULL);

	conn = g_new0 (BaconMessageConnection, 1);
	conn->path = socket_filename (prefix);

	if (test_is_socket (conn->path) == FALSE)
	{
		if (!try_server (conn))
		{
			bacon_message_connection_free (conn);
			return NULL;
		}

		conn->is_server = TRUE;
		return conn;
	}

	if (try_client (conn) == FALSE)
	{
		unlink (conn->path);
		try_server (conn);
		if (conn->fd == -1)
		{
			bacon_message_connection_free (conn);
			return NULL;
		}

		conn->is_server = TRUE;
		return conn;
	}

	conn->is_server = FALSE;
	return conn;
}
示例#3
0
文件: server.c 项目: AlexanderS/pen
/* Returns 1 if a failover server candidate is available.
   Close connection and return 0 if none was found.
*/
int failover_server(int conn)
{
	int server = conns[conn].server;
	DEBUG(2, "failover_server(%d): server = %d", conn, server);
	if (server_alg & ALG_STUBBORN) {
		DEBUG(2, "Won't failover because we are stubborn");
		close_conn(conn);
		return 0;
	}
	if (server == ABUSE_SERVER) {
		DEBUG(2, "Won't failover from abuse server (%d)", abuse_server);
		close_conn(conn);
		return 0;
	}
	if (server == EMERGENCY_SERVER) {
		DEBUG(2, "Already using emergency server (%d), won't fail over", emerg_server);
		close_conn(conn);
		return 0;
	}
	if (conns[conn].upfd != -1) {
		close(conns[conn].upfd);
		conns[conn].upfd = -1;
	}
	/* there needs to be at least two regular servers in order to fail over to something else */
	/* and if we couldn't find a candidate for initial_server, we're not going to find one now */
	if (nservers > 1 && server != NO_SERVER) {
		DEBUG(2, "Trying to find failover server. server = %d, initial = %d, nservers = %d", server, conns[conn].initial, nservers);
		do {
			server = (server+1) % nservers;
			DEBUG(2, "Intend to try server %d", server);
			if (try_server(server, conn)) return 1;
		} while (server != conns[conn].initial);
	}
	DEBUG(1, "using emergency server, remember to reset flag");
	emergency = 1;
	if (try_server(emerg_server, conn)) return 1;
	close_conn(conn);
	return 0;
}