@@ -109,35 +109,33 @@ var Actor ActorDef
109109
110110// AskDef Ask inspired by Erlang/Akka
111111type AskDef struct {
112- id time.Time
113- ch * chan interface {}
114- timeout * time.Duration
112+ id time.Time
113+ ch * chan interface {}
115114
116115 Message interface {}
117116}
118117
119118// New New Ask instance
120- func (askSelf * AskDef ) New (message interface {}, timeout * time. Duration ) * AskDef {
121- return AskNewGenerics (message , timeout )
119+ func (askSelf * AskDef ) New (message interface {}) * AskDef {
120+ return AskNewGenerics (message )
122121}
123122
124123// NewByOptions New Ask by its options
125- func (askSelf * AskDef ) NewByOptions (message interface {}, ioCh * chan interface {}, timeout * time. Duration ) * AskDef {
126- return AskNewByOptionsGenerics (message , ioCh , timeout )
124+ func (askSelf * AskDef ) NewByOptions (message interface {}, ioCh * chan interface {}) * AskDef {
125+ return AskNewByOptionsGenerics (message , ioCh )
127126}
128127
129128// AskNewGenerics New Ask instance
130- func AskNewGenerics (message interface {}, timeout * time. Duration ) * AskDef {
129+ func AskNewGenerics (message interface {}) * AskDef {
131130 ch := make (chan interface {})
132- return AskNewByOptionsGenerics (message , & ch , timeout )
131+ return AskNewByOptionsGenerics (message , & ch )
133132}
134133
135134// AskNewByOptionsGenerics New Ask by its options
136- func AskNewByOptionsGenerics (message interface {}, ioCh * chan interface {}, timeout * time. Duration ) * AskDef {
135+ func AskNewByOptionsGenerics (message interface {}, ioCh * chan interface {}) * AskDef {
137136 newOne := AskDef {
138- id : time .Now (),
139- ch : ioCh ,
140- timeout : timeout ,
137+ id : time .Now (),
138+ ch : ioCh ,
141139
142140 Message : message ,
143141 }
@@ -146,30 +144,27 @@ func AskNewByOptionsGenerics(message interface{}, ioCh *chan interface{}, timeou
146144}
147145
148146// AskOnce Sender Ask
149- func (askSelf * AskDef ) AskOnce (target ActorHandle ) interface {} {
150- ch , timer := askSelf .AskChannel (target )
151- result , success := <- * ch
152- if success && timer != nil {
153- timer .Stop ()
154- close (* ch )
147+ func (askSelf * AskDef ) AskOnce (target ActorHandle , timeout * time.Duration ) interface {} {
148+ ch := askSelf .AskChannel (target )
149+ var result interface {}
150+ if timeout == nil {
151+ result = <- * ch
152+ } else {
153+ select {
154+ case result = <- * ch :
155+ case <- time .After (* timeout ):
156+ }
155157 }
158+ close (* ch )
156159
157160 return result
158161}
159162
160163// AskChannel Sender Ask
161- func (askSelf * AskDef ) AskChannel (target ActorHandle ) (* chan interface {}, * time.Timer ) {
162- var timer * time.Timer
164+ func (askSelf * AskDef ) AskChannel (target ActorHandle ) * chan interface {} {
163165 target .Send (askSelf )
164- if askSelf .timeout != nil {
165- timer = time .NewTimer (* askSelf .timeout )
166- go func () {
167- <- timer .C
168- close (* askSelf .ch )
169- }()
170- }
171166
172- return askSelf .ch , timer
167+ return askSelf .ch
173168}
174169
175170// Reply Receiver Reply
0 commit comments