Using tldb Distributed Lock with Go and Java Clients
This article introduces tldb's distributed lock, explains its lock, trylock, and unlock methods, and provides step‑by‑step Go and Java client examples—including Maven configuration and code snippets—for acquiring and releasing locks across languages in a distributed environment.
Distributed locks are essential tools in distributed systems. tldb offers a simple, reliable distributed lock API that mimics local object locks, exposing three operations: lock (blocking), trylock (non‑blocking), and unlock (release).
The lock(string, int) method takes a lock name and a timeout in seconds; the server automatically releases the lock after the timeout if unlock is not called. unlock(key) releases a previously acquired lock using the returned key. trylock(string, int) attempts to acquire the lock and returns immediately with a key if successful or empty data if the lock is already held.
tldb implements the lock service in its MQ module, and client libraries are available for Java, Go, Python, and JavaScript. Below are concrete usage examples.
Go example (blocking lock)
import "github.com/donnie4w/tlmq-go/cli"
sc := cli.NewMqClient("ws://127.0.0.1:5001", "mymq=123")
sc.Connect()
key, err := sc.Lock("testlock", 3)
if err != nil {
// handle lock failure
} else {
defer sc.UnLock(key) // ensure release
// business logic here
}Go example (non‑blocking trylock)
sc := cli.NewMqClient("ws://127.0.0.1:5001", "mymq=123")
sc.Connect()
if key, ok := sc.TryLock("testlock2", 3); ok {
defer sc.UnLock(key)
// business logic
}Go example (spin with trylock)
sc := cli.NewMqClient("ws://127.0.0.1:5001", "mymq=123")
sc.Connect()
var key string
for {
if v, ok := sc.TryLock("testlock", 3); ok {
key = v
break
} else {
<-time.After(100 * time.Millisecond)
}
}
defer sc.UnLock(key)
// business logicJava example (Maven dependency)
<dependency>
<groupId>io.github.donnie4w</groupId>
<artifactId>tlmq-j</artifactId>
<version>0.0.2</version>
</dependency>Java example (blocking lock)
MqClient mc = new SimpleClient("ws://127.0.0.1:5001", "mymq=123");
mc.connect();
String key = null;
try {
key = mc.lock("testlock", 3);
// business logic
} finally {
if (key != null) {
mc.unLock(key);
}
}Java example (non‑blocking trylock)
MqClient mc = new SimpleClient("ws://127.0.0.1:5001", "mymq=123");
mc.connect();
String key = null;
try {
key = mc.tryLock("testlock", 3);
// business logic
} finally {
if (key != null) {
mc.unLock(key);
}
}Performance tests show that multiple threads contending for the same lock using either lock or a spin‑based trylock behave as expected, with the lock being handed over to waiting threads after the timeout or release.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.