वर्तमान में परीक्षण चल रहा है: पूरा होने पर GitHub कोड खोला जाएगा।

Ticketing Java / Kotlin लाइब्रेरी

GitHub Maven Central

एक ही TicketBroker तीन कॉलिंग शैलियाँ एक साथ प्रदान करता है — Kotlin कोरूटीन acquire का उपयोग करते हैं, Java async acquireAsync (CompletableFuture) का, और Java ब्लॉकिंग acquireBlocking का। आप जो भी उपयोग करें, वह वही ब्रोकर और वही कनेक्शन है।

रिपॉज़िटरी

xml
kts

उदाहरण

बुनियादी उदाहरण

Application startup पर एक broker बनाकर share करें। wait=0 queue बिना एक immediate attempt है, अधिकतम 255 सेकंड; lease 1–250 सेकंड। अंतिम minimum-work budget अनिवार्य है और 0 हो सकता है। Sub-second ऊपर round होते हैं; out-of-range या normalized lease से बड़ा budget send से पहले reject होता है, clamp नहीं।

Kotlin (coroutines):

kotlin
val broker = TicketBroker.connect("127.0.0.1:5225")
broker.waitReady(Duration.ofSeconds(5))

val ticket = broker.acquire("key", Duration.ofSeconds(5), Duration.ofSeconds(30), Duration.ofSeconds(2))
val token = ticket.token
// In the same DB transaction: verify/update token high-water and perform the business write.
ticket.release()

Java (blocking):

java
TicketBroker broker = TicketBroker.connect("127.0.0.1:5225");
broker.waitReadyBlocking(Duration.ofSeconds(5));

try (Ticket ticket = broker.acquireBlocking(
        "key", Duration.ofSeconds(5), Duration.ofSeconds(30), Duration.ofSeconds(2))) {
    long token = ticket.getToken();
    // In the same DB transaction: verify/update token high-water and perform the business write.
}

Automatic close/drop bounded best-effort release है। Result चाहिए तो explicit release API उपयोग करें। Token को उसी DB transaction में protected write fence करना अनिवार्य है।

जानने योग्य व्यवहार

  • एक request byte भी send हुआ हो सकता है तो final response खोने का परिणाम Indeterminate है। समान owner से A auto-resend नहीं; caller critical section में नहीं जाता।
  • Send से पहले cancel unsent; possible-send के बाद session बंद। साथ में grant token parse हो तो bounded compensating exact-token release।
  • M, हर E, malformed/oversized/unknown response session-fatal हैं। Unresolved possible-send acquire Indeterminate।
  • B निश्चित capacity rejection है और तुरंत लौटता है। Internal retry नहीं; caller application backoff के बाद नए owner से नया acquire कर सकता है।
  • Explicit/compensating release exact token को call/enqueue से पूर्ण 5 सेकंड तक ही retry करता है। R success, N absent/not current; final response न हो तो error, assumed success नहीं।
  • Positive conservative remaining time और पर्याप्त work budget पर ही Ticket मिलता है। 250 सेकंड से अधिक work send से पहले unsupported।
  • Token DB fencing अनिवार्य: उसी transaction में token <= stored_high_water reject, high-water update और business write, फिर commit/rollback और release।

सुरक्षा विकल्प (टोकन · TLS)

हर विकल्प वैकल्पिक है। token सर्वर के client_tokens से मेल खाना चाहिए, और TLS के चार मोड हैं: बंद / सिस्टम ट्रस्ट स्टोर / एक निर्दिष्ट CA / सत्यापन छोड़ें (केवल परीक्षण के लिए)।

kotlin
val broker = TicketBroker.builder()
    .addrs("10.0.0.1:5225", "10.0.0.2:5225", "10.0.0.3:5225")
    .token("123")
    .tls(TlsMode.SystemRoots)
    // .tls(TlsMode.Ca("ca.crt"))
    // .tls(TlsMode.InsecureSkipVerify)
    .connect()

Java में, स्टैटिक फ़ैक्टरी TlsMode.systemRoots() / TlsMode.ca("ca.crt") / TlsMode.insecureSkipVerify() का उपयोग करें।

आप सर्वर-साइड टोकन, TLS, और क्लस्टर कॉन्फ़िगरेशन Ticketing सर्वर परिनियोजन पृष्ठ पर जनरेट कर सकते हैं।

Spring वर्चुअल थ्रेड्स

वर्चुअल थ्रेड्स पर Spring MVC चलाते समय Java और Kotlin दोनों में *Blocking कॉल उपयोग करें। नॉन-सस्पेंड कंट्रोलर acquire को नहीं बुला सकता, इसलिए Kotlin में भी acquireBlocking सामान्य मार्ग है। ये कोरूटीन से नहीं गुज़रते और केवल उत्तर पर park होते हैं, इसलिए कैरियर थ्रेड मुक्त रहता है।

kotlin
@RestController
class OrderController(private val broker: TicketBroker) {

    @PostMapping("/orders/{id}")
    fun place(@PathVariable id: String): String {
        broker.acquireBlocking("order-$id", Duration.ofSeconds(5), Duration.ofSeconds(30)).use {
            // क्रिटिकल सेक्शन
        }
        return "ok"
    }
}

close() (try-with-resources / use) के पीछे की बैकग्राउंड रिलीज़ डिफ़ॉल्ट रूप से वर्चुअल-थ्रेड एक्ज़ीक्यूटर पर चलती है, इसलिए वह कभी निश्चित आकार के पूल के पीछे कतार में नहीं लगती। Spring का एक्ज़ीक्यूटर उपयोग करने के लिए अपना एक्ज़ीक्यूटर पास करें।

kotlin
TicketBroker.builder()
    .addrs("127.0.0.1:5225")
    .executor(applicationTaskExecutor)
    .connect()

WebFlux या कोरूटीन कंट्रोलर के साथ suspend कॉल का उपयोग जारी रखें।