harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-0205harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-0205PSEUDOCODE
// Global in-memory state registry: Map // topic -> Topic Topic: subscriptions: Map // subscription_name -> Subscription Subscription: queue: Queue Message: data: Bytes attributes: Dict<String, Any> message_id: String MessageIdCounter: Integer
function generate_message_id() -> String MessageIdCounter := MessageIdCounter + 1 return "msg-" + to_string(MessageIdCounter)
class PublisherClient constructor(project, cred) // no-op; stores credentials if needed method create_topic(topic: String) -> None if topic not in registry registry[topic] := Topic(subscriptions := {})
method get_topic(topic: String) -> None if topic not in registry raise TopicNotFoundError // else: return (topic exists)
method publish(topic: String, data: Bytes, callback: Function = null) -> None if topic not in registry raise TopicNotFoundError msg_id := generate_message_id() message := { "data": data, "attributes": {}, "message_id": msg_id } for each sub in registry[topic].subscriptions.values() sub.queue.enqueue(copy(message)) if callback is not null callback(msg_id)
// Note: copy(message) ensures separate object per subscription
class SubscribeClient constructor(project, cred) // no-op; stores credentials if needed
method create_subscription(topic: String, subscription_name: String) -> None if topic not in registry raise TopicNotFoundError topic_obj := registry[topic] if subscription_name not in topic_obj.subscriptions topic_obj.subscriptions[subscription_name] := Subscription(queue := NewQueue())
class SubscriptionService constructor(subscription: SubscribeClient) self.subscription := subscription self._thread := null self._stop_event := NewEvent()
method run(callback: Function) -> None if self._thread is not null and self._thread.is_alive() return self._stop_event.clear() self._thread := Thread(target := self._worker, args := (callback,)) self._thread.start()
method shutdown() -> None if self._thread is not null self._stop_event.set() self._thread.join() self._thread := null
method _worker(callback: Function) -> None while not self._stop_event.is_set() // Find first available subscription queue for this client queues := [] for each topic in registry.keys() topic_obj := registry[topic] for each sub in topic_obj.subscriptions.values() // match only subscriptions owned by this client if needed; tests use single subscription queues.append(sub.queue) delivered := false for each q in queues if not q.empty() message := q.get_nowait() callback(message) delivered := true break if not delivered sleep(10 milliseconds) // avoid busy-wait
// Exceptions / error types class TopicNotFoundError extends Exception
// Thread-safe queue interface (pseudo) class Queue[T] method enqueue(item: T) -> None method dequeue() -> T // blocks or non-blocking as defined method dequeue_nowait() -> T // non-blocking method empty() -> Bool
// Helpers (pseudo) function NewQueue() -> Queue[Message] function NewEvent() -> Event Event: method set() -> None method clear() -> None method is_set() -> Bool method sleep(duration) -> None