Skip to content

Commit ef203a3

Browse files
author
Al S-M
committed
update defers for convention of coming after the call they relate to
1 parent 6ef152d commit ef203a3

File tree

6 files changed

+25
-29
lines changed

6 files changed

+25
-29
lines changed

filestore.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func NewFileStore(directory string) *FileStore {
5252

5353
// Open will allow the FileStore to be used.
5454
func (store *FileStore) Open() {
55-
defer store.Unlock()
5655
store.Lock()
56+
defer store.Unlock()
5757
// if no store directory was specified in ClientOpts, by default use the
5858
// current working directory
5959
if store.directory == "" {
@@ -72,17 +72,17 @@ func (store *FileStore) Open() {
7272

7373
// Close will disallow the FileStore from being used.
7474
func (store *FileStore) Close() {
75-
defer store.Unlock()
7675
store.Lock()
76+
defer store.Unlock()
7777
store.opened = false
7878
store.t.Trace_W(STR, "store is not open")
7979
}
8080

8181
// Put will put a message into the store, associated with the provided
8282
// key value.
8383
func (store *FileStore) Put(key string, m *Message) {
84-
defer store.Unlock()
8584
store.Lock()
85+
defer store.Unlock()
8686
chkcond(store.opened)
8787
full := fullpath(store.directory, key)
8888
if exists(full) {
@@ -96,8 +96,8 @@ func (store *FileStore) Put(key string, m *Message) {
9696
// Get will retrieve a message from the store, the one associated with
9797
// the provided key value.
9898
func (store *FileStore) Get(key string) (m *Message) {
99-
defer store.RUnlock()
10099
store.RLock()
100+
defer store.RUnlock()
101101
chkcond(store.opened)
102102
filepath := fullpath(store.directory, key)
103103
if !exists(filepath) {
@@ -116,23 +116,23 @@ func (store *FileStore) Get(key string) (m *Message) {
116116
// All will provide a list of all of the keys associated with messages
117117
// currenly residing in the FileStore.
118118
func (store *FileStore) All() []string {
119-
defer store.RUnlock()
120119
store.RLock()
120+
defer store.RUnlock()
121121
return store.all()
122122
}
123123

124124
// Del will remove the persisted message associated with the provided
125125
// key from the FileStore.
126126
func (store *FileStore) Del(key string) {
127-
defer store.Unlock()
128127
store.Lock()
128+
defer store.Unlock()
129129
store.del(key)
130130
}
131131

132132
// Reset will remove all persisted messages from the FileStore.
133133
func (store *FileStore) Reset() {
134-
defer store.Unlock()
135134
store.Lock()
135+
defer store.Unlock()
136136
store.t.Trace_W(STR, "FileStore Reset")
137137
for _, key := range store.all() {
138138
store.del(key)

memstore.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type MemoryStore struct {
3333
// use until Open() has been called on it.
3434
func NewMemoryStore() *MemoryStore {
3535
store := &MemoryStore{
36-
messages: nil,
36+
messages: make(map[string]*Message),
3737
opened: false,
3838
t: nil,
3939
}
@@ -42,27 +42,26 @@ func NewMemoryStore() *MemoryStore {
4242

4343
// Open initializes a MemoryStore instance.
4444
func (store *MemoryStore) Open() {
45-
defer store.Unlock()
4645
store.Lock()
47-
store.messages = make(map[string]*Message)
46+
defer store.Unlock()
4847
store.opened = true
4948
store.t.Trace_V(STR, "memorystore initialized")
5049
}
5150

5251
// Put takes a key and a pointer to a Message and stores the
5352
// message.
5453
func (store *MemoryStore) Put(key string, message *Message) {
55-
defer store.Unlock()
5654
store.Lock()
55+
defer store.Unlock()
5756
chkcond(store.opened)
5857
store.messages[key] = message
5958
}
6059

6160
// Get takes a key and looks in the store for a matching Message
6261
// returning either the Message pointer or nil.
6362
func (store *MemoryStore) Get(key string) *Message {
64-
defer store.RUnlock()
6563
store.RLock()
64+
defer store.RUnlock()
6665
chkcond(store.opened)
6766
mid := key2mid(key)
6867
m := store.messages[key]
@@ -77,8 +76,8 @@ func (store *MemoryStore) Get(key string) *Message {
7776
// All returns a slice of strings containing all the keys currently
7877
// in the MemoryStore.
7978
func (store *MemoryStore) All() []string {
80-
defer store.RUnlock()
8179
store.RLock()
80+
defer store.RUnlock()
8281
chkcond(store.opened)
8382
keys := []string{}
8483
for k, _ := range store.messages {
@@ -90,8 +89,8 @@ func (store *MemoryStore) All() []string {
9089
// Del takes a key, searches the MemoryStore and if the key is found
9190
// deletes the Message pointer associated with it.
9291
func (store *MemoryStore) Del(key string) {
93-
defer store.Unlock()
9492
store.Lock()
93+
defer store.Unlock()
9594
mid := key2mid(key)
9695
m := store.messages[key]
9796
if m == nil {
@@ -104,24 +103,24 @@ func (store *MemoryStore) Del(key string) {
104103

105104
// Close will disallow modifications to the state of the store.
106105
func (store *MemoryStore) Close() {
107-
defer store.Unlock()
108106
store.Lock()
107+
defer store.Unlock()
109108
chkcond(store.opened)
110109
store.opened = false
111110
store.t.Trace_V(STR, "memorystore closed")
112111
}
113112

114113
// Reset eliminates all persisted message data in the store.
115114
func (store *MemoryStore) Reset() {
116-
defer store.Unlock()
117115
store.Lock()
116+
defer store.Unlock()
118117
chkcond(store.opened)
119118
store.messages = make(map[string]*Message)
120119
store.t.Trace_W(STR, "memorystore wiped")
121120
}
122121

123122
func (store *MemoryStore) SetTracer(tracer *Tracer) {
124-
defer store.Unlock()
125123
store.Lock()
124+
defer store.Unlock()
126125
store.t = tracer
127126
}

messageids.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,12 @@ func (mids *messageIds) generateMsgIds() {
5252
}
5353

5454
func (mids *messageIds) freeId(id MId) {
55-
defer mids.Unlock()
5655
mids.Lock()
56+
defer mids.Unlock()
5757
//trace_v(MID, "freeing message id: %v", id)
5858
mids.index[id] = false
5959
}
6060

6161
func (mids *messageIds) getId() MId {
62-
// defer mids.Unlock()
63-
// mids.Lock()
64-
id := <-mids.idChan // Does this need to be locked?
65-
return id
62+
return <-mids.idChan
6663
}

ping.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ type lastcontact struct {
2525
}
2626

2727
func (l *lastcontact) update() {
28-
defer l.Unlock()
2928
l.Lock()
29+
defer l.Unlock()
3030
l.lasttime = time.Now()
3131

3232
}
3333

3434
func (l *lastcontact) get() time.Time {
35-
defer l.Unlock()
3635
l.Lock()
36+
defer l.Unlock()
3737
return l.lasttime
3838
}
3939

receipts.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ func newReceiptMap() *receiptMap {
3434
}
3535

3636
func (m *receiptMap) put(mid MId, c chan Receipt) {
37-
defer m.Unlock()
3837
m.Lock()
38+
defer m.Unlock()
3939
m.contents[mid] = c
4040
}
4141

4242
func (m *receiptMap) get(mid MId) chan Receipt {
43-
defer m.RUnlock()
4443
m.RLock()
44+
defer m.RUnlock()
4545
return m.contents[mid]
4646
}
4747

4848
func (m *receiptMap) end(mid MId) {
49-
defer m.Unlock()
5049
m.Lock()
50+
defer m.Unlock()
5151
close(m.contents[mid])
5252
delete(m.contents, mid)
5353
}

router.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ func newRouter() (*router, chan bool) {
8989
// routes to see if there is already a matching Route. If there is it replaces the current
9090
// callback with the new one. If not it add a new entry to the list of Routes.
9191
func (r *router) addRoute(topic string, callback MessageHandler) {
92-
defer r.Unlock()
9392
r.Lock()
93+
defer r.Unlock()
9494
for e := r.routes.Front(); e != nil; e = e.Next() {
9595
if e.Value.(*route).match(topic) {
9696
r := e.Value.(*route)
@@ -104,8 +104,8 @@ func (r *router) addRoute(topic string, callback MessageHandler) {
104104
// deleteRoute takes a route string, looks for a matching Route in the list of Routes. If
105105
// found it removes the Route from the list.
106106
func (r *router) deleteRoute(topic string) {
107-
defer r.Unlock()
108107
r.Lock()
108+
defer r.Unlock()
109109
for e := r.routes.Front(); e != nil; e = e.Next() {
110110
if e.Value.(*route).match(topic) {
111111
r.routes.Remove(e)

0 commit comments

Comments
 (0)