Skip to content

Publish and subscribe system optimizations #13

@eloylp

Description

@eloylp

Context

This library provides basic and experimental pub/sub features. In order to send the messages among the different subscribers per topic, the code maintains a map of maps, with a global lock:

type pubSubEngine struct {
	csMap map[string]map[*conn.Slot]message.Sender
	L     *sync.RWMutex
}
//...

The first level map, holds the topics name as key, having as value the second level map, which holds as a key the pointer address of the connection slot, and the related sender as a value.

In reality, senders contains the underlying connection slot, but having the connection slot pointer address as key its very handy, as its ideal for quick removal of connections on each topic. We could not use the messsage.Sender pointer as key, as this address could change over time.

Problem

We must take into account, that the publish method is a hot path of the program. We could expect high concurrency there:

  • The current global lock will lock all the structure each time a message its published, for any of the topics.
  • The current "map inside map" strategy was very convenient for the POC, as we can eliminate entries very easily each time a disconnection from a client happens. But, we are iterating a map, which means we need to search across the entire key space of the hashmap.

Goals

  • Evaluate the if this optimisations makes sense, by taking into account the status of the project and uses. Taking into account the publish method is going to be called much more times than the connection removal, probably we should make improvements at the cost of complexity.
  • Solve the problem of the global lock.
  • Solve the problem of the iteration over a map.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions