From 0545c4d15d4c680a01e6207c45a08480740fade9 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Wed, 17 Dec 2014 00:24:59 -0800 Subject: [PATCH] refactor: *Entry -> Entry in many places, entries are assigned from one slice to another and in different goroutines. In one place, entries were modified (in the queue). To avoid shared mutable state, probably best to handle entries by value. License: MIT Signed-off-by: Brian Tiger Chow --- exchange/bitswap/message/message.go | 12 ++++++------ exchange/bitswap/wantlist/wantlist.go | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/exchange/bitswap/message/message.go b/exchange/bitswap/message/message.go index 245fc35fb..7f7f1d08e 100644 --- a/exchange/bitswap/message/message.go +++ b/exchange/bitswap/message/message.go @@ -19,7 +19,7 @@ import ( type BitSwapMessage interface { // Wantlist returns a slice of unique keys that represent data wanted by // the sender. - Wantlist() []*Entry + Wantlist() []Entry // Blocks returns a slice of unique blocks Blocks() []*blocks.Block @@ -48,7 +48,7 @@ type Exportable interface { type impl struct { full bool - wantlist map[u.Key]*Entry + wantlist map[u.Key]Entry blocks map[u.Key]*blocks.Block // map to detect duplicates } @@ -59,7 +59,7 @@ func New() BitSwapMessage { func newMsg() *impl { return &impl{ blocks: make(map[u.Key]*blocks.Block), - wantlist: make(map[u.Key]*Entry), + wantlist: make(map[u.Key]Entry), full: true, } } @@ -90,8 +90,8 @@ func (m *impl) Full() bool { return m.full } -func (m *impl) Wantlist() []*Entry { - var out []*Entry +func (m *impl) Wantlist() []Entry { + var out []Entry for _, e := range m.wantlist { out = append(out, e) } @@ -120,7 +120,7 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) { e.Priority = priority e.Cancel = cancel } else { - m.wantlist[k] = &Entry{ + m.wantlist[k] = Entry{ Entry: wantlist.Entry{ Key: k, Priority: priority, diff --git a/exchange/bitswap/wantlist/wantlist.go b/exchange/bitswap/wantlist/wantlist.go index 1bf662102..aa58ee155 100644 --- a/exchange/bitswap/wantlist/wantlist.go +++ b/exchange/bitswap/wantlist/wantlist.go @@ -13,7 +13,7 @@ type ThreadSafe struct { // not threadsafe type Wantlist struct { - set map[u.Key]*Entry + set map[u.Key]Entry } type Entry struct { @@ -23,7 +23,7 @@ type Entry struct { Priority int } -type entrySlice []*Entry +type entrySlice []Entry func (es entrySlice) Len() int { return len(es) } func (es entrySlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] } @@ -37,7 +37,7 @@ func NewThreadSafe() *ThreadSafe { func New() *Wantlist { return &Wantlist{ - set: make(map[u.Key]*Entry), + set: make(map[u.Key]Entry), } } @@ -62,13 +62,13 @@ func (w *ThreadSafe) Contains(k u.Key) bool { return w.Wantlist.Contains(k) } -func (w *ThreadSafe) Entries() []*Entry { +func (w *ThreadSafe) Entries() []Entry { w.lk.RLock() defer w.lk.RUnlock() return w.Wantlist.Entries() } -func (w *ThreadSafe) SortedEntries() []*Entry { +func (w *ThreadSafe) SortedEntries() []Entry { w.lk.RLock() defer w.lk.RUnlock() return w.Wantlist.SortedEntries() @@ -78,7 +78,7 @@ func (w *Wantlist) Add(k u.Key, priority int) { if _, ok := w.set[k]; ok { return } - w.set[k] = &Entry{ + w.set[k] = Entry{ Key: k, Priority: priority, } @@ -93,7 +93,7 @@ func (w *Wantlist) Contains(k u.Key) bool { return ok } -func (w *Wantlist) Entries() []*Entry { +func (w *Wantlist) Entries() []Entry { var es entrySlice for _, e := range w.set { es = append(es, e) @@ -101,7 +101,7 @@ func (w *Wantlist) Entries() []*Entry { return es } -func (w *Wantlist) SortedEntries() []*Entry { +func (w *Wantlist) SortedEntries() []Entry { var es entrySlice for _, e := range w.set { es = append(es, e)