Use type alias for readability
@@ -8,14 +8,16 @@ import (
"github.com/cloudfoundry/sonde-go/events"
)
+type shardID string
+
type Router struct {
lock sync.RWMutex
- subscriptions map[plumbing.Filter]map[string][]DataSetter
+ subscriptions map[plumbing.Filter]map[shardID][]DataSetter
}
func NewRouter() *Router {
return &Router{
- subscriptions: make(map[plumbing.Filter]map[string][]DataSetter),
+ subscriptions: make(map[plumbing.Filter]map[shardID][]DataSetter),
}
}
@@ -42,18 +44,18 @@ func (r *Router) SendTo(appID string, envelope *events.Envelope) {
AppID: appID,
}
- for shardID, setters := range r.subscriptions[filter] {
- r.writeToShard(shardID, setters, data)
+ for id, setters := range r.subscriptions[filter] {
+ r.writeToShard(id, setters, data)
}
var noFilter plumbing.Filter
- for shardID, setters := range r.subscriptions[noFilter] {
- r.writeToShard(shardID, setters, data)
+ for id, setters := range r.subscriptions[noFilter] {
+ r.writeToShard(id, setters, data)
}
}
-func (r *Router) writeToShard(shardID string, setters []DataSetter, data []byte) {
- if shardID == "" {
+func (r *Router) writeToShard(id shardID, setters []DataSetter, data []byte) {
+ if id == "" {
for _, setter := range setters {
setter.Set(data)
}
@@ -71,11 +73,11 @@ func (r *Router) registerSetter(req *plumbing.SubscriptionRequest, dataSetter Da
m, ok := r.subscriptions[filter]
if !ok {
- m = make(map[string][]DataSetter)
+ m = make(map[shardID][]DataSetter)
r.subscriptions[filter] = m
}
- m[req.ShardID] = append(m[req.ShardID], dataSetter)
+ m[shardID(req.ShardID)] = append(m[shardID(req.ShardID)], dataSetter)
}
func (r *Router) buildCleanup(req *plumbing.SubscriptionRequest, dataSetter DataSetter) func() {
@@ -89,18 +91,18 @@ func (r *Router) buildCleanup(req *plumbing.SubscriptionRequest, dataSetter Data
}
var setters []DataSetter
- for _, s := range r.subscriptions[filter][req.ShardID] {
+ for _, s := range r.subscriptions[filter][shardID(req.ShardID)] {
if s != dataSetter {
setters = append(setters, s)
}
}
if len(setters) > 0 {
- r.subscriptions[filter][req.ShardID] = setters
+ r.subscriptions[filter][shardID(req.ShardID)] = setters
return
}
- delete(r.subscriptions[filter], req.ShardID)
+ delete(r.subscriptions[filter], shardID(req.ShardID))
if len(r.subscriptions[filter]) == 0 {
delete(r.subscriptions, filter)
