@cryptotaxi247 / kubo / commits / 2678795da

fix: privatize Mux fields

Brian Tiger Chow committed Jan 12, 2015 at 19:28 UTC 2678795dacb3a813186768af5bbbcfa84f8046f3
3 files changed +31 -27
p2p/host/basic/basic_host.go
+3 -3
@@ -16,7 +16,7 @@ var log = eventlog.Logger("p2p/host/basic")
16
17 type BasicHost struct {
18 network inet.Network
19 - mux protocol.Mux
19 + mux *protocol.Mux
20 ids *identify.IDService
21 relay *relay.RelayService
22 }
@@ -25,7 +25,7 @@ type BasicHost struct {
25 func New(net inet.Network) *BasicHost {
26 h := &BasicHost{
27 network: net,
28 - mux: protocol.Mux{Handlers: protocol.StreamHandlerMap{}},
28 + mux: protocol.NewMux(),
29 }
30
31 // setup host services
@@ -65,7 +65,7 @@ func (h *BasicHost) Network() inet.Network {
65
66 // Mux returns the Mux multiplexing incoming streams to protocol handlers
67 func (h *BasicHost) Mux() *protocol.Mux {
68 - return &h.mux
68 + return h.mux
69 }
70
71 func (h *BasicHost) IDService() *identify.IDService {
p2p/protocol/mux.go
+24 -20
@@ -14,7 +14,7 @@ import (
14
15 var log = eventlog.Logger("net/mux")
16
17 -type StreamHandlerMap map[ID]inet.StreamHandler
17 +type streamHandlerMap map[ID]inet.StreamHandler
18
19 // Mux provides simple stream multixplexing.
20 // It helps you precisely when:
@@ -23,24 +23,28 @@ type StreamHandlerMap map[ID]inet.StreamHandler
23 //
24 // It contains the handlers for each protocol accepted.
25 // It dispatches handlers for streams opened by remote peers.
26 -//
27 -// WARNING: this datastructure IS NOT threadsafe.
28 -// do not modify it once the network is using it.
26 type Mux struct {
30 - Default inet.StreamHandler // handles unknown protocols.
31 - Handlers StreamHandlerMap
27 + // Default handles unknown protocols. Callers modify at your own risk.
28 + Default inet.StreamHandler
29 +
30 + lock sync.RWMutex
31 + handlers streamHandlerMap
32 +}
33
33 - sync.RWMutex
34 +func NewMux() *Mux {
35 + return &Mux{
36 + handlers: streamHandlerMap{},
37 + }
38 }
39
40 // Protocols returns the list of protocols this muxer has handlers for
41 func (m *Mux) Protocols() []ID {
38 - m.RLock()
39 - l := make([]ID, 0, len(m.Handlers))
40 - for p := range m.Handlers {
42 + m.lock.RLock()
43 + l := make([]ID, 0, len(m.handlers))
44 + for p := range m.handlers {
45 l = append(l, p)
46 }
43 - m.RUnlock()
47 + m.lock.RUnlock()
48 return l
49 }
50
@@ -54,9 +58,9 @@ func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) {
58 }
59
60 // log.Debug("readHeader got:", p)
57 - m.RLock()
58 - h, found := m.Handlers[p]
59 - m.RUnlock()
61 + m.lock.RLock()
62 + h, found := m.handlers[p]
63 + m.lock.RUnlock()
64
65 switch {
66 case !found && m.Default != nil:
@@ -70,18 +74,18 @@ func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) {
74
75 // String returns the muxer's printing representation
76 func (m *Mux) String() string {
73 - m.RLock()
74 - defer m.RUnlock()
75 - return fmt.Sprintf("<Muxer %p %d>", m, len(m.Handlers))
77 + m.lock.RLock()
78 + defer m.lock.RUnlock()
79 + return fmt.Sprintf("<Muxer %p %d>", m, len(m.handlers))
80 }
81
82 // SetHandler sets the protocol handler on the Network's Muxer.
83 // This operation is threadsafe.
84 func (m *Mux) SetHandler(p ID, h inet.StreamHandler) {
85 log.Debugf("%s setting handler for protocol: %s (%d)", m, p, len(p))
82 - m.Lock()
83 - m.Handlers[p] = h
84 - m.Unlock()
86 + m.lock.Lock()
87 + m.handlers[p] = h
88 + m.lock.Unlock()
89 }
90
91 // Handle reads the next name off the Stream, and calls a handler function
p2p/protocol/mux_test.go
+4 -4
@@ -38,12 +38,12 @@ func TestHandler(t *testing.T) {
38 }
39 }
40
41 - m := Mux{Handlers: StreamHandlerMap{}}
41 + m := NewMux()
42 m.Default = h("default")
43 - m.Handlers["/dht"] = h("bitswap")
43 + m.SetHandler("/dht", h("bitswap"))
44 // m.Handlers["/ipfs"] = h("bitswap") // default!
45 - m.Handlers["/bitswap"] = h("bitswap")
46 - m.Handlers["/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj"] = h("bitswap")
45 + m.SetHandler("/bitswap", h("bitswap"))
46 + m.SetHandler("/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj", h("bitswap"))
47
48 for k, v := range testCases {
49 var buf bytes.Buffer