Skip to content

Commit f00b9dd

Browse files
committedMar 10, 2018
Merge branch 'release/1.3.1'
2 parents f58e51d + 019e66d commit f00b9dd

File tree

7 files changed

+126
-1
lines changed

7 files changed

+126
-1
lines changed
 

‎CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ Change Log
44
All notable changes to this project will be documented in this file.
55
This project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [v1.3.1] - 2018-03-10
8+
9+
### Fixed
10+
11+
- Adding additional locking during message delivery to prevent race condition
12+
that could lose messages.
13+
14+
715
## [v1.3.0] - 2018-02-28
816

917
### Added
@@ -13,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1321
### Changed
1422
- Reverse message display sort order in the UI; now newest first.
1523

24+
1625
## [v1.2.0] - 2017-12-27
1726

1827
### Changed
@@ -31,6 +40,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3140
types
3241
- Fixed panic when `monitor.history` set to 0
3342

43+
3444
## [v1.2.0-rc1] - 2017-01-29
3545

3646
### Added
@@ -57,6 +67,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
5767
- Allow increased local-part length of 128 chars for Mailgun
5868
- RedHat and Ubuntu now use systemd instead of legacy init systems
5969

70+
6071
## [v1.1.0] - 2016-09-03
6172

6273
### Added
@@ -65,6 +76,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
6576
### Fixed
6677
- Log and continue when unable to delete oldest message during cap enforcement
6778

79+
6880
## [v1.1.0-rc2] - 2016-03-06
6981

7082
### Added
@@ -75,6 +87,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
7587
- Shutdown hang in retention scanner
7688
- Display empty subject as `(No Subject)`
7789

90+
7891
## [v1.1.0-rc1] - 2016-03-04
7992

8093
### Added
@@ -89,6 +102,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
89102
- RESTful API moved to `/api/v1` base URI
90103
- More graceful shutdown on Ctrl-C or when errors encountered
91104

105+
92106
## [v1.0] - 2014-04-14
93107

94108
### Added
@@ -98,6 +112,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
98112
specific message.
99113

100114
[Unreleased]: https://github.com/jhillyerd/inbucket/compare/master...develop
115+
[v1.3.1]: https://github.com/jhillyerd/inbucket/compare/v1.3.0...v1.3.1
101116
[v1.3.0]: https://github.com/jhillyerd/inbucket/compare/v1.2.0...v1.3.0
102117
[v1.2.0]: https://github.com/jhillyerd/inbucket/compare/1.2.0-rc2...1.2.0
103118
[v1.2.0-rc2]: https://github.com/jhillyerd/inbucket/compare/1.2.0-rc1...1.2.0-rc2

‎datastore/datastore.go

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"io"
77
"net/mail"
8+
"sync"
89
"time"
910

1011
"github.com/jhillyerd/enmime"
@@ -22,6 +23,8 @@ var (
2223
type DataStore interface {
2324
MailboxFor(emailAddress string) (Mailbox, error)
2425
AllMailboxes() ([]Mailbox, error)
26+
// LockFor is a temporary hack to fix #77 until Datastore revamp
27+
LockFor(emailAddress string) (*sync.RWMutex, error)
2528
}
2629

2730
// Mailbox is an interface to get and manipulate messages in a DataStore

‎datastore/lock.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package datastore
2+
3+
import (
4+
"strconv"
5+
"sync"
6+
)
7+
8+
type HashLock [4096]sync.RWMutex
9+
10+
func (h *HashLock) Get(hash string) *sync.RWMutex {
11+
if len(hash) < 3 {
12+
return nil
13+
}
14+
i, err := strconv.ParseInt(hash[0:3], 16, 0)
15+
if err != nil {
16+
return nil
17+
}
18+
return &h[i]
19+
}

‎datastore/lock_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package datastore_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/jhillyerd/inbucket/datastore"
7+
)
8+
9+
func TestHashLock(t *testing.T) {
10+
hl := &datastore.HashLock{}
11+
12+
// Invalid hashes
13+
testCases := []struct {
14+
name, input string
15+
}{
16+
{"empty", ""},
17+
{"short", "a0"},
18+
{"badhex", "zzzzzzzzzzzzzzzzzzzzzzz"},
19+
}
20+
for _, tc := range testCases {
21+
t.Run(tc.input, func(t *testing.T) {
22+
l := hl.Get(tc.input)
23+
if l != nil {
24+
t.Errorf("Expected nil lock for %s %q, got %v", tc.name, tc.input, l)
25+
}
26+
})
27+
}
28+
29+
// Valid hashes
30+
testStrings := []string{
31+
"deadbeef",
32+
"00000000",
33+
"ffffffff",
34+
}
35+
for _, ts := range testStrings {
36+
t.Run(ts, func(t *testing.T) {
37+
l := hl.Get(ts)
38+
if l == nil {
39+
t.Errorf("Expeced non-nil lock for hex string %q", ts)
40+
}
41+
})
42+
}
43+
44+
a := hl.Get("deadbeef")
45+
b := hl.Get("deadbeef")
46+
if a != b {
47+
t.Errorf("Expected identical locks for identical hashes, got: %p != %p", a, b)
48+
}
49+
50+
a = hl.Get("deadbeef")
51+
b = hl.Get("d3adb33f")
52+
if a == b {
53+
t.Errorf("Expected different locks for different hashes, got: %p == %p", a, b)
54+
}
55+
56+
a = hl.Get("deadbeef")
57+
b = hl.Get("deadb33f")
58+
if a != b {
59+
t.Errorf("Expected identical locks for identical leading hashes, got: %p != %p", a, b)
60+
}
61+
}

‎datastore/testing.go

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package datastore
33
import (
44
"io"
55
"net/mail"
6+
"sync"
67
"time"
78

89
"github.com/jhillyerd/enmime"
@@ -26,6 +27,10 @@ func (m *MockDataStore) AllMailboxes() ([]Mailbox, error) {
2627
return args.Get(0).([]Mailbox), args.Error(1)
2728
}
2829

30+
func (m *MockDataStore) LockFor(name string) (*sync.RWMutex, error) {
31+
return &sync.RWMutex{}, nil
32+
}
33+
2934
// MockMailbox is a shared mock for unit testing
3035
type MockMailbox struct {
3136
mock.Mock

‎filestore/fstore.go

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func countGenerator(c chan int) {
5151
// FileDataStore implements DataStore aand is the root of the mail storage
5252
// hiearchy. It provides access to Mailbox objects
5353
type FileDataStore struct {
54+
hashLock datastore.HashLock
5455
path string
5556
mailPath string
5657
messageCap int
@@ -139,6 +140,15 @@ func (ds *FileDataStore) AllMailboxes() ([]datastore.Mailbox, error) {
139140
return mailboxes, nil
140141
}
141142

143+
func (ds *FileDataStore) LockFor(emailAddress string) (*sync.RWMutex, error) {
144+
name, err := stringutil.ParseMailboxName(emailAddress)
145+
if err != nil {
146+
return nil, err
147+
}
148+
hash := stringutil.HashMailboxName(name)
149+
return ds.hashLock.Get(hash), nil
150+
}
151+
142152
// FileMailbox implements Mailbox, manages the mail for a specific user and
143153
// correlates to a particular directory on disk.
144154
type FileMailbox struct {

‎smtpd/handler.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,19 @@ func (ss *Session) dataHandler() {
402402
if ss.server.storeMessages {
403403
// Create a message for each valid recipient
404404
for _, r := range recipients {
405-
if ok := ss.deliverMessage(r, msgBuf); ok {
405+
// TODO temporary hack to fix #77 until datastore revamp
406+
mu, err := ss.server.dataStore.LockFor(r.localPart)
407+
if err != nil {
408+
ss.logError("Failed to get lock for %q: %s", r.localPart, err)
409+
// Delivery failure
410+
ss.send(fmt.Sprintf("451 Failed to store message for %v", r.localPart))
411+
ss.reset()
412+
return
413+
}
414+
mu.Lock()
415+
ok := ss.deliverMessage(r, msgBuf)
416+
mu.Unlock()
417+
if ok {
406418
expReceivedTotal.Add(1)
407419
} else {
408420
// Delivery failure

0 commit comments

Comments
 (0)