shared-pool.ts raw

   1  /**
   2   * Shared SimplePool for all relay connections in the app.
   3   *
   4   * Every service previously created its own SimplePool (client.service,
   5   * graph-query.service, EventBrowserTab, RecoveryTab), and each pool opens a
   6   * separate WebSocket per relay. With multiple pools alive at once, one client
   7   * could hold many connections to the same relay (main pool + graph pool +
   8   * tab pools + cache relay), tripping per-IP connection caps on personal
   9   * relays. A single shared pool guarantees exactly one connection per relay
  10   * across the whole app.
  11   *
  12   * Note: `trackRelays` is enabled so `seenOn` metadata works for the main
  13   * client service.
  14   */
  15  import { SimplePool } from 'nostr-tools'
  16  
  17  const sharedPool = new SimplePool()
  18  ;(sharedPool as SimplePool & { trackRelays?: boolean }).trackRelays = true
  19  
  20  export function getSharedPool(): SimplePool {
  21    return sharedPool
  22  }
  23  
  24  /** Close all connections held by the shared pool. */
  25  export function closeSharedPool(relays: string[]): void {
  26    sharedPool.close(relays)
  27  }