notifier.go 4.05 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
4
5
6
7
8
// Package notifier provides a simple notification dispatcher
// meant to be embedded in larger structres who wish to allow
// clients to sign up for event notifications.
package notifier

import (
	"sync"

Jeromy's avatar
Jeromy committed
9
10
	process "QmSir6qPL1tjuxd8LkR8VZq6v625ExAUVs2eCLeqQuaPGU/goprocess"
	ratelimit "QmSir6qPL1tjuxd8LkR8VZq6v625ExAUVs2eCLeqQuaPGU/goprocess/ratelimit"
Jeromy's avatar
Jeromy committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
)

// Notifiee is a generic interface. Clients implement
// their own Notifiee interfaces to ensure type-safety
// of notifications:
//
//  type RocketNotifiee interface{
//    Countdown(r Rocket, countdown time.Duration)
//    LiftedOff(Rocket)
//    ReachedOrbit(Rocket)
//    Detached(Rocket, Capsule)
//    Landed(Rocket)
//  }
//
type Notifiee interface{}

// Notifier is a notification dispatcher. It's meant
// to be composed, and its zero-value is ready to be used.
//
//  type Rocket struct {
//    notifier notifier.Notifier
//  }
//
type Notifier struct {
	mu   sync.RWMutex // guards notifiees
	nots map[Notifiee]struct{}
	lim  *ratelimit.RateLimiter
}

// RateLimited returns a rate limited Notifier. only limit goroutines
// will be spawned. If limit is zero, no rate limiting happens. This
// is the same as `Notifier{}`.
func RateLimited(limit int) Notifier {
	n := Notifier{}
	if limit > 0 {
		n.lim = ratelimit.NewRateLimiter(process.Background(), limit)
	}
	return n
}

// Notify signs up Notifiee e for notifications. This function
// is meant to be called behind your own type-safe function(s):
//
//   // generic function for pattern-following
//   func (r *Rocket) Notify(n Notifiee) {
//     r.notifier.Notify(n)
//   }
//
//   // or as part of other functions
//   func (r *Rocket) Onboard(a Astronaut) {
//     r.astronauts = append(r.austronauts, a)
//     r.notifier.Notify(a)
//   }
//
func (n *Notifier) Notify(e Notifiee) {
	n.mu.Lock()
	if n.nots == nil { // so that zero-value is ready to be used.
		n.nots = make(map[Notifiee]struct{})
	}
	n.nots[e] = struct{}{}
	n.mu.Unlock()
}

// StopNotify stops notifying Notifiee e. This function
// is meant to be called behind your own type-safe function(s):
//
//   // generic function for pattern-following
//   func (r *Rocket) StopNotify(n Notifiee) {
//     r.notifier.StopNotify(n)
//   }
//
//   // or as part of other functions
//   func (r *Rocket) Detach(c Capsule) {
//     r.notifier.StopNotify(c)
//     r.capsule = nil
//   }
//
func (n *Notifier) StopNotify(e Notifiee) {
	n.mu.Lock()
	if n.nots != nil { // so that zero-value is ready to be used.
		delete(n.nots, e)
	}
	n.mu.Unlock()
}

// NotifyAll messages the notifier's notifiees with a given notification.
// This is done by calling the given function with each notifiee. It is
// meant to be called with your own type-safe notification functions:
//
//  func (r *Rocket) Launch() {
//    r.notifyAll(func(n Notifiee) {
//      n.Launched(r)
//    })
//  }
//
//  // make it private so only you can use it. This function is necessary
//  // to make sure you only up-cast in one place. You control who you added
//  // to be a notifiee. If Go adds generics, maybe we can get rid of this
//  // method but for now it is like wrapping a type-less container with
//  // a type safe interface.
//  func (r *Rocket) notifyAll(notify func(Notifiee)) {
//    r.notifier.NotifyAll(func(n notifier.Notifiee) {
//      notify(n.(Notifiee))
//    })
//  }
//
// Note well: each notification is launched in its own goroutine, so they
// can be processed concurrently, and so that whatever the notification does
// it _never_ blocks out the client. This is so that consumers _cannot_ add
// hooks into your object that block you accidentally.
func (n *Notifier) NotifyAll(notify func(Notifiee)) {
	n.mu.Lock()
	defer n.mu.Unlock()

	if n.nots == nil { // so that zero-value is ready to be used.
		return
	}

	// no rate limiting.
	if n.lim == nil {
		for notifiee := range n.nots {
			go notify(notifiee)
		}
		return
	}

	// with rate limiting.
	n.lim.Go(func(worker process.Process) {
		for notifiee := range n.nots {
			notifiee := notifiee // rebind for loop data races
			n.lim.LimitedGo(func(worker process.Process) {
				notify(notifiee)
			})
		}
	})
}