|
| 1 | +# singleinstance |
| 2 | + |
| 3 | +## Introduction |
| 4 | +Lightweight Kotlin/JVM library that prevents multiple concurrent instances of a Kotlin application. |
| 5 | +Launch arguments from subsequent instances are communicated back to the first instance via a UNIX domain socket. |
| 6 | + |
| 7 | +A Kotlin equivalent to [unique4j](https://github.com/prat-man/unique4j) |
| 8 | + |
| 9 | +## Installation |
| 10 | +Add the [jitpack](https://jitpack.io/) repository to your build file: |
| 11 | +```kotlin |
| 12 | +repositories { |
| 13 | + maven { url 'https://jitpack.io' } |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +Then add as a module dependency: |
| 18 | +```kotlin |
| 19 | +dependencies { |
| 20 | + implementation("com.github.iamcalledrob:singleinstance:1.0.0") |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | +Instantiate `SingleInstance` with a file path and call `args()`. |
| 26 | +1. If this is the first instance, `args` will listen indefinitely for subsequent instances to connect to it. |
| 27 | +2. If there is an existing instance, `args` will connect and send its arguments, then exit. |
| 28 | + |
| 29 | +```kotlin |
| 30 | +fun main(args: Array<String>) { |
| 31 | + SingleInstance("/path/to/a/unique/file.sock").args(args) { receivedArgs -> |
| 32 | + // Handle received args from another instance |
| 33 | + println("Subsequent instance args: ${receivedArgs.joinToString()}") |
| 34 | + } |
| 35 | + |
| 36 | + // Handle original args |
| 37 | + println("Original launch args: ${args.joinToString()}") |
| 38 | + |
| 39 | + // The rest of the application logic |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +There is also a helper function for generating a suitable sock file path from an identifier: |
| 44 | +```kotlin |
| 45 | +socketPath(identifier = "org.foo.widget") |
| 46 | +// -> /tmp/org.foo.widget.sock |
| 47 | +``` |
| 48 | + |
| 49 | +## How it works |
| 50 | +1. A system-wide file lock is used to determine which instance is "first" to launch |
| 51 | +2. The first instance then listens on a domain socket for arguments from other instances |
| 52 | +3. Other instances are unable to acquire the file lock, and instead connect to the first instance via the socket. |
| 53 | + Arguments are written to the socket, then the process exits. |
| 54 | +4. When the original process exits, the file lock is released. |
| 55 | + |
| 56 | +## Why you might consider this over [unique4j](https://github.com/prat-man/unique4j) |
| 57 | +1. Uses [UNIX Domain Sockets](https://inside.java/2021/02/03/jep380-unix-domain-sockets-channels/), which avoids |
| 58 | + triggering the Windows firewall. unique4j listens on a local TCP socket, which will pop a firewall dialog. |
| 59 | +2. Minimal design, leaves the exact implementation up to you. |
| 60 | +3. Filesystem permissions can be used to control which processes are able to send arguments, if desired. |
| 61 | + |
| 62 | +## Considerations |
| 63 | +Domain sockets were added to Windows version 1809, so this library is unsuitable for legacy versions of Windows. |
0 commit comments