registry.go


Overview

The [registry.go](/projects/291/69101) file defines a subscription registry system designed to manage client subscriptions to various address channels in a WebSocket context. The core functionality revolves around allowing clients to subscribe or unsubscribe to messages published on specific addresses and ensuring that published messages are correctly routed to all subscribed clients.

This file provides:

The registry supports multiple clients subscribing to multiple addresses, with each client maintaining one or more subscriptions identified by subscription IDs. Messages are sent asynchronously to clients over dedicated channels.


Detailed Description

Interfaces and Types

Registrar Interface

type Registrar interface {
	Subscribe(clientID string, subscriptionID string, addrs []string, msg chan<- []byte)
	Unsubscribe(clientID string, subscriptionID string, addrs []string, msg chan<- []byte)
	Publish(addrs []string, data interface{})
}

Structs

Registry

type Registry struct {
	clients   map[string]map[string]struct{}
	addresses map[string]map[string]chan<- []byte
}

**Note:** Composite IDs uniquely identify subscriptions per client.


Constructor

NewRegistry() *Registry

Creates and initializes a new `Registry` instance.

func NewRegistry() *Registry {
	return &Registry{
		clients:   make(map[string]map[string]struct{}),
		addresses: make(map[string]map[string]chan<- []byte),
	}
}

Utility Functions

toID(clientID string, subscriptionID string) string

Combines `clientID` and `subscriptionID` into a single string identifier.

fromID(id string) (string, string)

Splits a composite ID back into `clientID` and `subscriptionID`.


Methods

Subscribe(clientID string, subscriptionID string, addrs []string, msgChan chan<- []byte)

Registers a client subscription to multiple addresses and associates a message channel for delivering messages.

msgChan := make(chan []byte)
registry.Subscribe("client123", "sub1", []string{"addr1", "addr2"}, msgChan)

Unsubscribe(clientID string, subscriptionID string, addrs []string, msgChan chan<- []byte)

Removes subscription of a client from specified addresses or all addresses if none provided.

// Unsubscribe from specific addresses
registry.Unsubscribe("client123", "sub1", []string{"addr1"}, msgChan)

// Unsubscribe from all addresses (complete removal)
registry.Unsubscribe("client123", "sub1", []string{}, msgChan)

Publish(addrs []string, data interface{})

Sends a message to all clients subscribed to any of the given addresses.

registry.Publish([]string{"addr1"}, map[string]string{"msg": "Hello subscribers"})

Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class Registrar {
        <<interface>>
        +Subscribe(clientID, subscriptionID string, addrs []string, msg chan<- []byte)
        +Unsubscribe(clientID, subscriptionID string, addrs []string, msg chan<- []byte)
        +Publish(addrs []string, data interface{})
    }

    class Registry {
        -clients map[string]map[string]struct{}
        -addresses map[string]map[string]chan<- []byte
        +Subscribe(clientID, subscriptionID string, addrs []string, msg chan<- []byte)
        +Unsubscribe(clientID, subscriptionID string, addrs []string, msg chan<- []byte)
        +Publish(addrs []string, data interface{})
    }

    Registrar <|.. Registry

Summary

The [registry.go](/projects/291/69101) file defines a robust subscription registry mechanism to manage client subscriptions and message delivery over WebSocket addresses. It uses composite IDs to identify subscriptions uniquely and maintains efficient bidirectional mappings to quickly add, remove, and notify subscribers. The asynchronous messaging via channels and JSON serialization enables flexible and scalable real-time communication.

This file is a foundational component for handling pub-sub style message distribution in WebSocket-based applications within the system.