refactor: extract router configuration
the param list of NewRouter was getting a bit long
This commit is contained in:
1 parent
e89e3adc69
commit
21102d3615
3 files changed
+35
-12
No files matched your search
@@ -17,7 +17,7 @@ type mockChannelConnection struct {
|
||||
// Construct a "passive" Router, i.e. without any goroutines, except
|
||||
// for Routes and GossipSenders.
|
||||
func NewTestRouter(name PeerName) *Router {
|
||||
router := NewRouter(nil, name, "", nil, 10, 1024, nil, Port)
|
||||
router := NewRouter(RouterConfig{Name: name})
|
||||
// need to create a dummy channel otherwise tests hang on nil
|
||||
// channels when the Router invoked ConnectionMaker.Refresh
|
||||
router.ConnectionMaker.actionChan = make(chan ConnectionMakerAction, ChannelSize)
|
||||
|
||||
+22
-9
@@ -17,6 +17,19 @@ const macMaxAge = 10 * time.Minute // [1]
|
||||
// [1] should be greater than typical ARP cache expiries, i.e. > 3/2 *
|
||||
// /proc/sys/net/ipv4_neigh/*/base_reachable_time_ms on Linux
|
||||
|
||||
type LogFrameFunc func(string, []byte, *layers.Ethernet)
|
||||
|
||||
type RouterConfig struct {
|
||||
Port int
|
||||
Iface *net.Interface
|
||||
Name PeerName
|
||||
NickName string
|
||||
Password []byte
|
||||
ConnLimit int
|
||||
BufSz int
|
||||
LogFrame LogFrameFunc
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
Port int
|
||||
Iface *net.Interface
|
||||
@@ -31,7 +44,7 @@ type Router struct {
|
||||
Password []byte
|
||||
ConnLimit int
|
||||
BufSz int
|
||||
LogFrame func(string, []byte, *layers.Ethernet)
|
||||
LogFrame LogFrameFunc
|
||||
}
|
||||
|
||||
type PacketSource interface {
|
||||
@@ -47,15 +60,15 @@ type PacketSourceSink interface {
|
||||
PacketSink
|
||||
}
|
||||
|
||||
func NewRouter(iface *net.Interface, name PeerName, nickName string, password []byte, connLimit int, bufSz int, logFrame func(string, []byte, *layers.Ethernet), port int) *Router {
|
||||
func NewRouter(config RouterConfig) *Router {
|
||||
router := &Router{
|
||||
Iface: iface,
|
||||
Port: config.Port,
|
||||
Iface: config.Iface,
|
||||
GossipChannels: make(map[uint32]*GossipChannel),
|
||||
Password: password,
|
||||
ConnLimit: connLimit,
|
||||
BufSz: bufSz,
|
||||
LogFrame: logFrame,
|
||||
Port: port}
|
||||
Password: config.Password,
|
||||
ConnLimit: config.ConnLimit,
|
||||
BufSz: config.BufSz,
|
||||
LogFrame: config.LogFrame}
|
||||
onMacExpiry := func(mac net.HardwareAddr, peer *Peer) {
|
||||
log.Println("Expired MAC", mac, "at", peer.FullName())
|
||||
}
|
||||
@@ -63,7 +76,7 @@ func NewRouter(iface *net.Interface, name PeerName, nickName string, password []
|
||||
router.Macs.Delete(peer)
|
||||
log.Println("Removed unreachable peer", peer.FullName())
|
||||
}
|
||||
router.Ourself = NewLocalPeer(name, nickName, router)
|
||||
router.Ourself = NewLocalPeer(config.Name, config.NickName, router)
|
||||
router.Macs = NewMacCache(macMaxAge, onMacExpiry)
|
||||
router.Peers = NewPeers(router.Ourself.Peer, onPeerGC)
|
||||
router.Peers.FetchWithDefault(router.Ourself.Peer)
|
||||
|
||||
+12
-2
@@ -128,7 +128,17 @@ func main() {
|
||||
defer profile.Start(&p).Stop()
|
||||
}
|
||||
|
||||
router := weave.NewRouter(iface, ourName, nickName, pwSlice, connLimit, bufSz*1024*1024, logFrameFunc(debug), port)
|
||||
router := weave.NewRouter(
|
||||
weave.RouterConfig{
|
||||
Port: port,
|
||||
Iface: iface,
|
||||
Name: ourName,
|
||||
NickName: nickName,
|
||||
Password: pwSlice,
|
||||
ConnLimit: connLimit,
|
||||
BufSz: bufSz * 1024 * 1024,
|
||||
LogFrame: logFrameFunc(debug)})
|
||||
|
||||
log.Println("Our name is", router.Ourself.FullName())
|
||||
router.Start()
|
||||
initiateConnections(router, peers)
|
||||
@@ -138,7 +148,7 @@ func main() {
|
||||
handleSignals(router)
|
||||
}
|
||||
|
||||
func logFrameFunc(debug bool) func(string, []byte, *layers.Ethernet) {
|
||||
func logFrameFunc(debug bool) weave.LogFrameFunc {
|
||||
if !debug {
|
||||
return func(prefix string, frame []byte, eth *layers.Ethernet) {}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user