-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: gossip into channels instead of callbacks (#1159)
## Which problem is this PR solving? - Gossip uses callbacks. A callback approach adds extra code we don't need if we use channels. This is a refactoring to change it to channels. Each commit is standalone. ## Short description of the changes - [x] Split the gossip interface file from the inmem implementation - [x] Add a SubscribeChan function that lives beside the Subscribe function - [x] Add test code for chan version - [x] Convert stress relief to use the channel model - [x] Remove callback code - [x] Rename remaining functions to simpler names again My desire is to merge this before #1158 and then fix that one (which does a bit more gossiping) to use channels.
- Loading branch information
Showing
5 changed files
with
231 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package gossip | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/honeycombio/refinery/logger" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// InMemoryGossip is a Gossiper that uses an in-memory channel | ||
type InMemoryGossip struct { | ||
Logger logger.Logger `inject:""` | ||
gossipCh chan []byte | ||
subscriptions map[string][]chan []byte | ||
|
||
done chan struct{} | ||
mut sync.RWMutex | ||
eg *errgroup.Group | ||
} | ||
|
||
var _ Gossiper = &InMemoryGossip{} | ||
|
||
func (g *InMemoryGossip) Publish(channel string, value []byte) error { | ||
msg := message{ | ||
key: channel, | ||
data: value, | ||
} | ||
|
||
select { | ||
case <-g.done: | ||
return errors.New("gossip has been stopped") | ||
case g.gossipCh <- msg.ToBytes(): | ||
default: | ||
g.Logger.Warn().WithFields(map[string]interface{}{ | ||
"channel": channel, | ||
"msg": string(value), | ||
}).Logf("Unable to publish message") | ||
} | ||
return nil | ||
} | ||
|
||
func (g *InMemoryGossip) Subscribe(channel string, depth int) chan []byte { | ||
select { | ||
case <-g.done: | ||
return nil | ||
default: | ||
} | ||
|
||
ch := make(chan []byte, depth) | ||
g.mut.Lock() | ||
g.subscriptions[channel] = append(g.subscriptions[channel], ch) | ||
g.mut.Unlock() | ||
|
||
return ch | ||
} | ||
|
||
func (g *InMemoryGossip) Start() error { | ||
g.gossipCh = make(chan []byte, 10) | ||
g.eg = &errgroup.Group{} | ||
g.subscriptions = make(map[string][]chan []byte) | ||
g.done = make(chan struct{}) | ||
|
||
g.eg.Go(func() error { | ||
for { | ||
select { | ||
case <-g.done: | ||
return nil | ||
case value := <-g.gossipCh: | ||
msg := newMessageFromBytes(value) | ||
g.mut.RLock() | ||
for _, ch := range g.subscriptions[msg.key] { | ||
select { | ||
case ch <- msg.data: | ||
default: | ||
g.Logger.Warn().WithFields(map[string]interface{}{ | ||
"channel": msg.key, | ||
"msg": string(msg.data), | ||
}).Logf("Unable to forward message") | ||
} | ||
} | ||
g.mut.RUnlock() | ||
} | ||
} | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func (g *InMemoryGossip) Stop() error { | ||
close(g.done) | ||
close(g.gossipCh) | ||
return g.eg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.