Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix sockets support #13

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Unix sockets support #13

wants to merge 16 commits into from

Conversation

Furrrlo
Copy link

@Furrrlo Furrrlo commented Dec 1, 2021

This PR adds support for using Unix sockets instead of the default TCP one, either via the junixsocket library (Java 1.7+) or the SocketChannel implementation introduced by Java 16.

It should completely mantain compatibility with the old API, it was mostly internal changes, with the only addition of 1 constructor to the Unique4j class. The only minor inconvenience is that I had to bump the Java version of the core module to 1.7, as the Java 16 compiler doesn't allow to go lower than that.

Example usage (p much the same):

// create unique instance
/* Unique4j unique = new Unique4j(APP_ID, true, new UnixSocketChannelIpcFactory()) { */
Unique4j unique = new Unique4j(APP_ID, true, new AFUNIXSocketIpcFactory()) {
    @Override
    public void receiveMessage(String message) {
        // print received message (timestamp)
        System.out.println(message);
    }

    @Override
    public String sendMessage() {
        // send timestamp as message
        Timestamp ts = new Timestamp(new Date().getTime());
        return "Another instance launch attempted: " + ts.toString();
    }
    
    /* It is not mandatory to override this method
     * By default, the stack trace is printed
     */
    @Override
    public void handleException(Exception exception) {
        // display the exception message
        System.out.println(exception.getMessage());
    }

    /* It is not mandatory to override this method
     * By default, the subsequent instance simply exits
     * This method is not invoked if AUTO_EXIT is turned off
     */
    @Override
    public void beforeExit() {
        // display exit message
        System.out.println("Exiting subsequent instance.");
    }
};

I had to move a bunch of stuff around and add a bit of scaffolding so the PR is kinda big, but I thought I would still try to contribute it back.

Otherwise, if one fails, the rest will too, as the lock file is left hanging
This will make sure that the tests don't have any dependency within one another and can potentially be run in parallel.
Allows the users to modify the underlying sockets directly.
This, for example, makes it possible to switch to a unix socket with the junixsocket library if one wants to.
Shared locks do not seem to behave as I expected (at least on Windows), as I cannot seem to write to the file after having just locked it from the same exact Thread and File descriptor.
This will allow to:
1. Properly cleanup additional resources such as the port file when the socket gets closed
2. Use SocketChannels instead of OIO, (which also introduced UNIX sockets in Java16 without the need of 3rd party libraries)
Fixes the Unique4jTest#testSubsequentAcquireLock() race condition, because as soon as the client received the response, it would close the socket, causing the server to not release the lock fast enough
This way it's easier to change it
This needs to make the compiler use Java 16 for compiling tests, while keeping Java 1.6 for the code itself
The file where we write the port changed
Makes it easier to target 2 different java versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant