Guide to Using tldb Distributed Locks with Go and Java
This article introduces tldb's distributed lock mechanism, explains lock, trylock, and unlock methods, provides Go and Java client examples, and also includes promotional information about ChatGPT services and a developer community for developers.
Distributed locks are a crucial tool in distributed systems, and tldb offers a simple, reliable lock service that mimics object locks in a program. The service can be accessed via various third‑party systems such as Redis, MQ, databases, or Zookeeper.
tldb Distributed Lock Usage Methods
lock : a blocking request for a lock.
trylock : attempts to acquire a lock; returns immediately with success or failure.
unlock : releases a previously acquired lock.
Lock (string, int) Method
The lock("abc", 3) method takes two parameters: the lock name (e.g., "abc" ) and the maximum holding time in seconds. If the lock is not released within the specified time, the server forcibly releases it.
UnLock (string) Method
After a lock is obtained, the server returns a key. The client must call UnLock(key) to release the lock; otherwise the server will release it after the timeout.
TryLock (string, int) Method
trylock is non‑blocking: it returns immediately with a key if the lock is acquired, or empty data if the lock is already held.
Go Example
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 error
} else {
defer sc.UnLock(key)
// business logic
}
// tryLock example (non‑blocking)
if key, ok := sc.TryLock("testlock2", 3); ok {
defer sc.UnLock(key)
// business logic
}
// spin‑wait tryLock
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
<dependency>
<groupId>io.github.donnie4w</groupId>
<artifactId>tlmq-j</artifactId>
<version>0.0.2</version>
</dependency>
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);
}
}
// tryLock example
String key = null;
try {
key = mc.tryLock("testlock", 3);
// business logic
} finally {
if (key != null) {
mc.unLock(key);
}
}Test results show multi‑threaded concurrency using both lock and tryLock with spin‑wait, illustrated by the included performance charts.
Beyond the technical tutorial, the article also promotes a ChatGPT community, offering discounted access, exclusive accounts, and various resources such as interview question collections and side‑project guides.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn 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.