hub

package
v0.0.0-...-6d0ab7c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package hub provides a WebSocket hub for managing client connections and broadcasting GitHub webhook events to subscribed clients based on their subscription criteria.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidUsername indicates an invalid GitHub username.
	ErrInvalidUsername = errors.New("invalid username")
	// ErrInvalidURL indicates an invalid URL.
	ErrInvalidURL = errors.New("invalid URL")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	ID string
	// contains filtered or unexported fields
}

Client represents a connected WebSocket client with their subscription preferences.

func NewClient

func NewClient(id string, sub Subscription, conn *websocket.Conn, hub *Hub, userOrgs []string) *Client

NewClient creates a new client.

func (*Client) Close

func (c *Client) Close()

Close gracefully closes the client.

func (*Client) Run

func (c *Client) Run(ctx context.Context, pingInterval, writeTimeout time.Duration)

Run handles sending events to the client and periodic pings. Context should be passed from the caller for proper lifecycle management.

type Event

type Event struct {
	URL       string    `json:"url"`       // Pull request URL
	Timestamp time.Time `json:"timestamp"` // When the event occurred
	Type      string    `json:"type"`      // GitHub event type (e.g., "pull_request")
}

Event represents a GitHub webhook event that will be broadcast to clients. It contains the PR URL, timestamp, and event type from GitHub.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub manages WebSocket clients and event broadcasting. It runs in its own goroutine and handles client registration, unregistration, and event distribution.

func NewHub

func NewHub() *Hub

NewHub creates a new client hub.

func (*Hub) Broadcast

func (h *Hub) Broadcast(event Event, payload map[string]any)

Broadcast sends an event to all matching clients.

func (*Hub) Register

func (h *Hub) Register(client *Client)

Register registers a new client.

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run starts the hub's event loop. The context should be passed from main for proper lifecycle management.

func (*Hub) Stop

func (h *Hub) Stop()

Stop signals the hub to stop.

func (*Hub) Unregister

func (h *Hub) Unregister(clientID string)

Unregister unregisters a client by ID.

func (*Hub) Wait

func (h *Hub) Wait()

Wait blocks until the hub has stopped.

type Subscription

type Subscription struct {
	Organization   string   `json:"organization"`
	Username       string   `json:"-"`
	EventTypes     []string `json:"event_types,omitempty"`
	UserEventsOnly bool     `json:"user_events_only,omitempty"`
	PullRequests   []string `json:"pull_requests,omitempty"` // List of PR URLs to subscribe to
}

Subscription represents a client's subscription criteria.

func (*Subscription) Validate

func (s *Subscription) Validate() error

Validate performs security validation on subscription data.

type WebSocketHandler

type WebSocketHandler struct {
	// contains filtered or unexported fields
}

WebSocketHandler handles WebSocket connections.

func NewWebSocketHandler

func NewWebSocketHandler(h *Hub, connLimiter *security.ConnectionLimiter, allowedEvents []string) *WebSocketHandler

NewWebSocketHandler creates a new WebSocket handler.

func NewWebSocketHandlerForTest

func NewWebSocketHandlerForTest(h *Hub, connLimiter *security.ConnectionLimiter, allowedEvents []string) *WebSocketHandler

NewWebSocketHandlerForTest creates a WebSocket handler for testing that skips GitHub auth.

func (*WebSocketHandler) Handle

func (h *WebSocketHandler) Handle(ws *websocket.Conn)

Handle handles a WebSocket connection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL