Skip to content

Commit 3ae0280

Browse files
committed
Avoid channel pointer usages.
1 parent fa7f239 commit 3ae0280

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ actual, _ = Maybe.Just(result).ToInt()
323323
// Ask channel
324324
// Result would be 30
325325
ch := Ask.New(3).AskChannel(actorRoot)
326-
actual, _ = Maybe.Just(<-*ch).ToInt()
327-
close(*ch)
326+
actual, _ = Maybe.Just(<-ch).ToInt()
327+
close(ch)
328328

329329
// Timeout cases
330330
// Result would be 0 (zero value, timeout)

actor.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ActorHandle interface {
1616
type ActorDef struct {
1717
id time.Time
1818
isClosed bool
19-
ch *chan interface{}
19+
ch chan interface{}
2020
effect func(*ActorDef, interface{})
2121

2222
context map[string]interface{}
@@ -34,12 +34,11 @@ func (actorSelf *ActorDef) GetDefault() *ActorDef {
3434

3535
// New New Actor instance
3636
func (actorSelf *ActorDef) New(effect func(*ActorDef, interface{})) *ActorDef {
37-
ch := make(chan interface{})
38-
return actorSelf.NewByOptions(effect, &ch, map[string]interface{}{})
37+
return actorSelf.NewByOptions(effect, make(chan interface{}), map[string]interface{}{})
3938
}
4039

4140
// NewByOptions New Actor by its options
42-
func (actorSelf *ActorDef) NewByOptions(effect func(*ActorDef, interface{}), ioCh *chan interface{}, context map[string]interface{}) *ActorDef {
41+
func (actorSelf *ActorDef) NewByOptions(effect func(*ActorDef, interface{}), ioCh chan interface{}, context map[string]interface{}) *ActorDef {
4342
newOne := ActorDef{
4443
id: time.Now(),
4544
ch: ioCh,
@@ -58,7 +57,7 @@ func (actorSelf *ActorDef) Send(message interface{}) {
5857
return
5958
}
6059

61-
*(actorSelf.ch) <- message
60+
actorSelf.ch <- message
6261
}
6362

6463
// Spawn Spawn a new Actor with parent(this actor)
@@ -93,7 +92,7 @@ func (actorSelf *ActorDef) GetID() time.Time {
9392
func (actorSelf *ActorDef) Close() {
9493
actorSelf.isClosed = true
9594

96-
close(*actorSelf.ch)
95+
close(actorSelf.ch)
9796
}
9897

9998
// IsClosed Check is Closed
@@ -102,7 +101,7 @@ func (actorSelf *ActorDef) IsClosed() bool {
102101
}
103102

104103
func (actorSelf *ActorDef) run() {
105-
for message := range *actorSelf.ch {
104+
for message := range actorSelf.ch {
106105
actorSelf.effect(actorSelf, message)
107106
}
108107
}
@@ -113,7 +112,7 @@ var Actor ActorDef
113112
// AskDef Ask inspired by Erlang/Akka
114113
type AskDef struct {
115114
id time.Time
116-
ch *chan interface{}
115+
ch chan interface{}
117116

118117
Message interface{}
119118
}
@@ -124,18 +123,17 @@ func (askSelf *AskDef) New(message interface{}) *AskDef {
124123
}
125124

126125
// NewByOptions New Ask by its options
127-
func (askSelf *AskDef) NewByOptions(message interface{}, ioCh *chan interface{}) *AskDef {
126+
func (askSelf *AskDef) NewByOptions(message interface{}, ioCh chan interface{}) *AskDef {
128127
return AskNewByOptionsGenerics(message, ioCh)
129128
}
130129

131130
// AskNewGenerics New Ask instance
132131
func AskNewGenerics(message interface{}) *AskDef {
133-
ch := make(chan interface{})
134-
return AskNewByOptionsGenerics(message, &ch)
132+
return AskNewByOptionsGenerics(message, make(chan interface{}))
135133
}
136134

137135
// AskNewByOptionsGenerics New Ask by its options
138-
func AskNewByOptionsGenerics(message interface{}, ioCh *chan interface{}) *AskDef {
136+
func AskNewByOptionsGenerics(message interface{}, ioCh chan interface{}) *AskDef {
139137
newOne := AskDef{
140138
id: time.Now(),
141139
ch: ioCh,
@@ -149,14 +147,14 @@ func AskNewByOptionsGenerics(message interface{}, ioCh *chan interface{}) *AskDe
149147
// AskOnce Sender Ask
150148
func (askSelf *AskDef) AskOnce(target ActorHandle, timeout *time.Duration) (interface{}, error) {
151149
ch := askSelf.AskChannel(target)
152-
defer close(*ch)
150+
defer close(ch)
153151
var result interface{}
154152
// var err error
155153
if timeout == nil {
156-
result = <-*ch
154+
result = <-ch
157155
} else {
158156
select {
159-
case result = <-*ch:
157+
case result = <-ch:
160158
case <-time.After(*timeout):
161159
return result, ErrActorAskTimeout
162160
}
@@ -166,15 +164,15 @@ func (askSelf *AskDef) AskOnce(target ActorHandle, timeout *time.Duration) (inte
166164
}
167165

168166
// AskChannel Sender Ask
169-
func (askSelf *AskDef) AskChannel(target ActorHandle) *chan interface{} {
167+
func (askSelf *AskDef) AskChannel(target ActorHandle) chan interface{} {
170168
target.Send(askSelf)
171169

172170
return askSelf.ch
173171
}
174172

175173
// Reply Receiver Reply
176174
func (askSelf *AskDef) Reply(response interface{}) {
177-
*askSelf.ch <- response
175+
askSelf.ch <- response
178176
}
179177

180178
// Ask Ask utils instance

actor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ func TestActorAsk(t *testing.T) {
124124
// Ask channel
125125
expectedInt = 30
126126
ch := AskNewGenerics(3).AskChannel(actorRoot)
127-
actual, _ = Maybe.Just(<-*ch).ToInt()
128-
close(*ch)
127+
actual, _ = Maybe.Just(<-ch).ToInt()
128+
close(ch)
129129
assert.Equal(t, expectedInt, actual)
130130

131131
// Timeout cases

cor.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,20 @@ type CorDef struct {
3838
isClosed AtomBool
3939
closedM sync.Mutex
4040

41-
opCh *chan *CorOp
42-
resultCh *chan interface{}
41+
opCh chan *CorOp
42+
resultCh chan interface{}
4343

4444
effect func()
4545
}
4646

4747
// New New a Cor instance
4848
func (corSelf *CorDef) New(effect func()) *CorDef {
49-
opCh := make(chan *CorOp, 5)
50-
resultCh := make(chan interface{}, 5)
51-
cor := &CorDef{effect: effect, opCh: &opCh, resultCh: &resultCh, isStarted: AtomBool{flag: 0}}
49+
cor := &CorDef{
50+
effect: effect,
51+
opCh: make(chan *CorOp, 5),
52+
resultCh: make(chan interface{}, 5),
53+
isStarted: AtomBool{flag: 0},
54+
}
5255
return cor
5356
}
5457

@@ -114,13 +117,13 @@ func (corSelf *CorDef) YieldRef(out interface{}) interface{} {
114117
var op *CorOp
115118
var more bool
116119
// fmt.Println(corSelf, "Wait for", "op")
117-
op, more = <-*corSelf.opCh
120+
op, more = <-corSelf.opCh
118121
// fmt.Println(corSelf, "Wait for", "op", "done")
119122

120123
if more && op != nil && op.cor != nil {
121124
cor := op.cor
122125
cor.doCloseSafe(func() {
123-
*cor.resultCh <- out
126+
cor.resultCh <- out
124127
})
125128
}
126129
result = op.val
@@ -138,7 +141,7 @@ func (corSelf *CorDef) YieldFrom(target *CorDef, in interface{}) interface{} {
138141
target.receive(corSelf, in)
139142

140143
// fmt.Println(corSelf, "Wait for", "result")
141-
result, _ = <-*corSelf.resultCh
144+
result, _ = <-corSelf.resultCh
142145
// fmt.Println(corSelf, "Wait for", "result", "done")
143146

144147
return result
@@ -148,7 +151,7 @@ func (corSelf *CorDef) receive(cor *CorDef, in interface{}) {
148151
corSelf.doCloseSafe(func() {
149152
if corSelf.opCh != nil {
150153
// fmt.Println(corSelf, "Wait for", "receive", cor, in)
151-
*(corSelf.opCh) <- &CorOp{cor: cor, val: in}
154+
corSelf.opCh <- &CorOp{cor: cor, val: in}
152155
// fmt.Println(corSelf, "Wait for", "receive", "done")
153156
}
154157
})
@@ -186,10 +189,10 @@ func (corSelf *CorDef) close() {
186189

187190
corSelf.closedM.Lock()
188191
if corSelf.resultCh != nil {
189-
close(*corSelf.resultCh)
192+
close(corSelf.resultCh)
190193
}
191194
if corSelf.opCh != nil {
192-
close(*corSelf.opCh)
195+
close(corSelf.opCh)
193196
}
194197
corSelf.closedM.Unlock()
195198
}

0 commit comments

Comments
 (0)