Skip to main content

kqueue_darwin.go - Overview

  1. Overview This file implements the IOMultiplexer interface for Darwin-based systems using kqueue. It provides functionality to subscribe to events, poll for triggered events, and close the kqueue instance.

  2. Detailed Documentation

KQueue

type KQueue struct {
// fd stores the file descriptor of the kqueue instance
fd int
// kQEvents acts as a buffer for the events returned by the Kevent syscall
kQEvents []syscall.Kevent_t
// diceEvents stores the events after they are converted to the generic Event type
// and is returned to the caller
diceEvents []Event
}

Purpose:

Represents the kqueue implementation of the IOMultiplexer interface. It holds the file descriptor, native kqueue events, and Dice events.

Fields:

  • fd: The file descriptor of the kqueue instance (int).
  • kQEvents: A slice of syscall.Kevent_t to store events returned by the Kevent syscall.
  • diceEvents: A slice of Event to store converted events.

New

func New() (*KQueue, error)

Purpose:

Creates a new KQueue instance.

Returns:

  • *KQueue: A pointer to the new KQueue instance.
  • error: An error if the kqueue creation fails or if config.Config.MaxClients is invalid.

Subscribe

func (kq *KQueue) Subscribe(event Event) error

Purpose:

Subscribes to the given event.

Parameters:

  • event: The Event to subscribe to.

Returns:

  • error: An error if the subscription fails.

Poll

func (kq *KQueue) Poll(timeout time.Duration) ([]Event, error)

Purpose:

Polls for all the subscribed events simultaneously and returns the events that were triggered. It blocks until at least one event is triggered or the timeout is reached.

Parameters:

  • timeout: The timeout duration.

Returns:

  • []Event: A slice of triggered Events.
  • error: An error if the polling fails.

Close

func (kq *KQueue) Close() error

Purpose:

Closes the KQueue instance.

Returns:

  • error: An error if closing the file descriptor fails.
  1. Code Examples N/A

  2. Getting Started Relevance YES