goraft / raft

UNMAINTAINED: A Go implementation of the Raft distributed consensus protocol.
MIT License
2.43k stars 479 forks source link

Event Dispatch #141

Closed benbjohnson closed 10 years ago

benbjohnson commented 10 years ago

Overview

This pull request includes an event dispatcher implementation that the Server uses. This gives applications the ability to hook into multiple event types using the observer pattern.

The Event includes the event type, the source of the event and the current and previous value related to the event.

Event Types

The following event types are implemented:

stateChange  - Dispatched when the state changes on the server.
leaderChange - Dispatched when the leader changes on the server.
termChange   - Dispatched when the term changes on the server.
addPeer      - Dispatched when a peer is added.
removePeer   - Dispatched when a peer is removed.

Server Interface Changes

Only one function is added to the Server interface:

func AddEventListener(typ string, func(Event))

There is no RemoveEventListener() because you can't compare functions in Go. There are ways we could get around it but I'm going to wait until we actually need to remove event listeners before implementing it.

Notes

We can easily add more types of events but I thought this was a good first cut. I mostly want to get feedback on the approach so far.

/cc: @philips @xiangli-cmu

benbjohnson commented 10 years ago

I'm merging this since it's low risk. Comments and change suggestions still welcome though.

philips commented 10 years ago

I really like the possibilities this opens up! Don't have time to give it a proper review though.