Skip to content

Commit b23b73b

Browse files
authored
feat(mailbox): send on unstarted will not return error (#125)
1 parent fd1f823 commit b23b73b

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

actor/mailbox.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ import (
77
"sync/atomic"
88
)
99

10-
var (
11-
// ErrMailboxNotStarted is an error returned by Mailbox.Send when sending is performed
12-
// on a mailbox that has not been started.
13-
ErrMailboxNotStarted = errors.New("Mailbox is not started")
14-
15-
// ErrMailboxStopped is an error returned by Mailbox.Send when sending is performed
16-
// on a mailbox that has been stopped.
17-
ErrMailboxStopped = errors.New("Mailbox is stopped")
18-
)
10+
// ErrMailboxStopped is an error returned by Mailbox.Send when sending is performed
11+
// on a mailbox that has been stopped.
12+
var ErrMailboxStopped = errors.New("Mailbox is stopped")
1913

2014
// Mailbox is interface for message transport mechanism between Actors.
2115
type Mailbox[T any] interface {
@@ -105,9 +99,8 @@ func NewMailboxes[T any](count int, opt ...MailboxOption) []Mailbox[T] {
10599
// behave like unbuffered channel.
106100
//
107101
// The Mailbox can send messages only after it has been started.
108-
// Attempting to send a message before starting will result in an error
109-
// ErrMailboxNotStarted. If a message is sent after the mailbox has been stopped,
110-
// ErrMailboxStopped will be returned.
102+
// Attempting to send a message before starting can block the caller. If a message is sent
103+
// after the mailbox has been stopped,ErrMailboxStopped will be returned.
111104
//
112105
// A Mailbox can be started and stopped only once. Restarting a stopped mailbox
113106
// has no effect.
@@ -169,9 +162,7 @@ func (m *mailboxChan[T]) Stop() {
169162

170163
func (m *mailboxChan[T]) Send(ctx Context, msg T) error {
171164
state := m.state.Load()
172-
if state == mbxStateNotStarted {
173-
return fmt.Errorf("Mailbox.Send failed: %w", ErrMailboxNotStarted)
174-
} else if state == mbxStateStopped {
165+
if state == mbxStateStopped {
175166
return fmt.Errorf("Mailbox.Send failed: %w", ErrMailboxStopped)
176167
}
177168

@@ -237,9 +228,7 @@ func (m *mailbox[T]) Stop() {
237228

238229
func (m *mailbox[T]) Send(ctx Context, msg T) error {
239230
state := m.state.Load()
240-
if state == mbxStateNotStarted {
241-
return fmt.Errorf("Mailbox.Send failed: %w", ErrMailboxNotStarted)
242-
} else if state == mbxStateStopped {
231+
if state == mbxStateStopped {
243232
return fmt.Errorf("Mailbox.Send failed: %w", ErrMailboxStopped)
244233
}
245234

actor/mailbox_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,6 @@ func assertMailboxStopped(t *testing.T, m Mailbox[any]) {
383383
func assertMailboxNotStarted(t *testing.T, m Mailbox[any]) {
384384
t.Helper()
385385

386-
assert.ErrorIs(t, m.Send(ContextStarted(), `👹`), ErrMailboxNotStarted,
387-
"should not be able to send as Mailbox is not started")
388-
389386
assertReceiveBlocking(t, m)
390387
}
391388

0 commit comments

Comments
 (0)