Comments (4)
I would rather wrap all that in a helper function that handles all of this.
e.g. if your code fails to initialize, set some state of the actor to failed.
then add a message request+response where you can query that state.
func NewSomeActor() {
props := actor.PropsFromProducer(......)
pid := system.Root.Spawn(props)
res := system.Root.RequestFuture(pid, &SomeRequest {}).Result
if res == .... {
//actor started successfully
}
else {
//actor failed to initialize
}
}
Or if possible, you could do it even before proto.actor is involved at all.
You can set state in the actor upon producing it.
func() actor.Actor { return &helloActor{
.....set state here
} }
you could create a function that resolves all deps and returns a Props if successful, or err if failed.
That way, you could catch this error before the actor is spawned.
from protoactor-go.
我宁愿将所有这些包装在一个处理所有这些的帮助程序函数中。例如,如果您的代码无法初始化,请将 actor 的某些状态设置为 failed。然后添加消息 request+response,您可以在其中查询该状态。
func NewSomeActor() { props := actor.PropsFromProducer(......) pid := system.Root.Spawn(props) res := system.Root.RequestFuture(pid, &SomeRequest {}).Result if res == .... { //actor started successfully } else { //actor failed to initialize } }
或者,如果可能的话,你甚至可以在 proto.actor 参与之前就这样做。您可以在生成 actor 时在 actor 中设置状态。
func() actor.Actor { return &helloActor{ .....set state here } }
您可以创建一个函数来解析所有 deps,如果成功,则返回 Props,如果失败,则返回 err。这样,您就可以在生成 actor 之前捕获此错误。
I have considered and tried the first method, but I am unable to determine how long the timeout for synchronous requests should be set. If the time is too long, it may affect the overall performance.
from protoactor-go.
新一期参
For the second approach, I might need to do some things using actor.Context during initialization, so it may not work as well.
from protoactor-go.
Currently, I am not sure if there will be any issues with the way I am doing it. The code is as follows:
func makeHook(factory BehaviorFactory, args ...any) actor.SpawnFunc {
return func(actorSystem *actor.ActorSystem, id string, props *actor.Props, parentContext actor.SpawnerContext) (pid *actor.PID, err error) {
hook := &hook{Behavior: factory(args...), Actor: makeActor(), factory: factory, args: args}
wait := make(chan error)
defer close(wait)
fun := func(ctx actor.Context) {
hook.update(ctx)
hook.Errorf("--------------------------------> %s", ctx.Parent())
hook.Do(func() {
if err := hook.Init(hook); err != nil {
hook.Debugf("behavior init failed: %s", err)
hook.Exit()
wait <- err
return
}
hook.Debugf("behavior init ok.")
wait <- nil
})
hook.Receive(ctx)
}
props = actor.PropsFromFunc(fun, actor.WithSupervisor(actor.DefaultSupervisorStrategy()))
if pid, err = actor.DefaultSpawner(actorSystem, id, props, parentContext); err != nil {
return nil, err
}
err = <-wait
return
}
}
func Spawn(alias Alias, factory BehaviorFactory, args ...any) (pid Pid, err error) {
props := (&actor.Props{}).Configure(actor.WithSpawnFunc(makeHook(factory, args...)))
if pid, err = root.ActorSystem.Root.SpawnNamed(props, string(alias)); err != nil {
return
}
root.Children = append(root.Children, pid)
return
}
from protoactor-go.
Related Issues (20)
- ensureTopicKindRegistered is not a good idea HOT 1
- subscriber may block PubSubMemberDeliveryActor
- Remove sprintf logging HOT 1
- Batch message sent twice? HOT 2
- Logging is a breaking change HOT 1
- MemberStrategy is not work!!!
- Go coroutine thread safe HOT 2
- Is every actor message Receiver run in they own goroutine ? HOT 1
- How can a member within a cluster be restarted? HOT 2
- Data Race
- Possible memory leak HOT 4
- Error when sending large message to remote actors HOT 1
- Context propagation HOT 1
- The release tag "0.4.0" does not work
- About using your code, and copyright
- protoc-gen-gograinv2 cannot recognize google.protobuf.Empty
- How to do grain reentrance HOT 2
- Improve code generation HOT 1
- Remove GrainResponse HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protoactor-go.