Skip to content

Ability to subscribe to a publisher #209

@arangamani

Description

@arangamani

I have been experimenting the load balancer/service discovery components for my project and I like the idea of the load balancer keeping track of the latest endpoints. However, I would like the ability to subscribe to a publisher and get a callback when there is a change in the service. I want to control the concurrency at which I talk to the service based on the number of available endpoints. If the service endpoints change, I would like to be able to grow or shrink my pool of worker goroutines. I would like to propose something like this:

type Publisher struct {
    sync.RWMutex
    // ...
    subscribers []chan<- bool
}

/// Subscribe adds the channel to its list of subscribers. The subscribers
// will be notified when there is a change in the service.
func (p *Publisher) Subscribe(ch chan<- bool) {
    p.Lock()
    defer p.Unlock()
    p.subscribers = append(p.subscribers, ch)
}

// notify notifies all subscribers.
func (p *Publisher) notify() {
    if len(p.subscribers) == 0 {
        return
    }
    p.RLock()
    defer p.RUnlock()
    for _, ch := range p.subscribers {
        ch <- true
    }
}

where the notify would be called whenever there is a change in the service.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions