-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
spawn.go
54 lines (44 loc) · 1.57 KB
/
spawn.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
package actor
import (
"errors"
)
// ErrNameExists is the error used when an existing name is used for spawning an actor.
var ErrNameExists = errors.New("spawn: name exists")
type SpawnFunc func(id string, props *Props, parent *PID) (*PID, error)
// DefaultSpawner conforms to Spawner and is used to spawn a local actor
var DefaultSpawner SpawnFunc = spawn
// Spawn starts a new actor based on props and named with a unique id
func Spawn(props *Props) *PID {
pid, _ := SpawnNamed(props, ProcessRegistry.NextId())
return pid
}
// SpawnPrefix starts a new actor based on props and named using a prefix followed by a unique id
func SpawnPrefix(props *Props, prefix string) (*PID, error) {
return SpawnNamed(props, prefix+ProcessRegistry.NextId())
}
// SpawnNamed starts a new actor based on props and named using the specified name
//
// If name exists, error will be ErrNameExists
func SpawnNamed(props *Props, name string) (*PID, error) {
var parent *PID
if props.guardianStrategy != nil {
parent = guardians.getGuardianPid(props.guardianStrategy)
}
return props.spawn(name, parent)
}
func spawn(id string, props *Props, parent *PID) (*PID, error) {
lp := &localProcess{}
pid, absent := ProcessRegistry.Add(lp, id)
if !absent {
return pid, ErrNameExists
}
cell := newLocalContext(props.actorProducer, props.getSupervisor(), props.inboundMiddleware, props.outboundMiddleware, parent)
mb := props.produceMailbox(cell, props.getDispatcher())
lp.mailbox = mb
var ref Process = lp
pid.p = &ref
cell.self = pid
mb.Start()
mb.PostSystemMessage(startedMessage)
return pid, nil
}