-
Notifications
You must be signed in to change notification settings - Fork 5
/
mailbox.go
146 lines (130 loc) · 4.2 KB
/
mailbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* MIT License
*
* Copyright (c) 2022-2024 Tochemey
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package actors
import (
"sync"
"go.uber.org/atomic"
)
// Mailbox defines the actor mailbox.
// Any implementation should be a thread-safe FIFO
type Mailbox interface {
// Push pushes a message into the mailbox. This returns an error
// when the box is full
Push(msg ReceiveContext) error
// Pop fetches a message from the mailbox
Pop() (msg ReceiveContext, err error)
// Iterator returns a channel that can be used to iterate over the mailbox
Iterator() <-chan ReceiveContext
// IsEmpty returns true when the mailbox is empty
IsEmpty() bool
// IsFull returns true when the mailbox is full
IsFull() bool
// Size returns the size of the buffer atomically
Size() uint64
// Reset resets the mailbox
Reset()
// Clone clones the current mailbox and returns a new Mailbox with reset settings
Clone() Mailbox
// Capacity returns the mailbox capacity atomically
Capacity() uint64
}
// receiveContextBuffer is the actor default inbox
type receiveContextBuffer struct {
// specifies the number of messages to stash
capacity *atomic.Uint64
buffer chan ReceiveContext
mu *sync.RWMutex
}
// newReceiveContextBuffer creates a Mailbox with a fixed capacity
func newReceiveContextBuffer(capacity uint64) Mailbox {
return &receiveContextBuffer{
capacity: atomic.NewUint64(capacity),
buffer: make(chan ReceiveContext, capacity),
mu: &sync.RWMutex{},
}
}
// enforce compilation error
var _ Mailbox = &receiveContextBuffer{}
// Push pushes a message into the mailbox. This returns an error
// when the box is full
func (x *receiveContextBuffer) Push(msg ReceiveContext) error {
x.mu.Lock()
defer x.mu.Unlock()
select {
case x.buffer <- msg:
return nil
default:
return ErrFullMailbox
}
}
// Pop fetches a message from the mailbox
func (x *receiveContextBuffer) Pop() (msg ReceiveContext, err error) {
x.mu.RLock()
defer x.mu.RUnlock()
select {
case msg := <-x.buffer:
return msg, nil
default:
return nil, ErrEmptyMailbox
}
}
// Iterator returns a channel that can be used to iterate over the mailbox
func (x *receiveContextBuffer) Iterator() <-chan ReceiveContext {
x.mu.RLock()
defer x.mu.RUnlock()
return x.buffer
}
// IsEmpty returns true when the buffer is empty
func (x *receiveContextBuffer) IsEmpty() bool {
x.mu.RLock()
defer x.mu.RUnlock()
return len(x.buffer) == 0
}
// Size returns the size of the buffer
func (x *receiveContextBuffer) Size() uint64 {
return uint64(len(x.buffer))
}
// Clone clones the current mailbox and returns a new Mailbox with reset settings
func (x *receiveContextBuffer) Clone() Mailbox {
return &receiveContextBuffer{
capacity: x.capacity,
buffer: make(chan ReceiveContext, x.capacity.Load()),
mu: &sync.RWMutex{},
}
}
// Reset resets the mailbox
func (x *receiveContextBuffer) Reset() {
x.mu.Lock()
defer x.mu.Unlock()
close(x.buffer)
x.buffer = make(chan ReceiveContext, x.capacity.Load())
}
// IsFull returns true when the mailbox is full
func (x *receiveContextBuffer) IsFull() bool {
return x.Size() >= x.capacity.Load()
}
// Capacity implements Mailbox.
func (x *receiveContextBuffer) Capacity() uint64 {
return x.capacity.Load()
}