spawnWithProps 使用属性创建
(actor Actor, props *Props, parent *PID)
| 195 | |
| 196 | // spawnWithProps 使用属性创建 |
| 197 | func (s *System) spawnWithProps(actor Actor, props *Props, parent *PID) *PID { |
| 198 | s.actorsMu.Lock() |
| 199 | defer s.actorsMu.Unlock() |
| 200 | |
| 201 | // 检查名称是否已存在 |
| 202 | if _, exists := s.actors[props.Name]; exists { |
| 203 | actorLog.Debug(context.Background(), "actor already exists, returning existing PID", map[string]any{"name": props.Name}) |
| 204 | return s.actors[props.Name].pid |
| 205 | } |
| 206 | |
| 207 | // 创建 PID |
| 208 | pid := &PID{ |
| 209 | ID: props.Name, |
| 210 | system: s, |
| 211 | } |
| 212 | |
| 213 | // 创建上下文 |
| 214 | ctx, cancel := context.WithCancel(s.ctx) |
| 215 | |
| 216 | // 确定邮箱大小 |
| 217 | mailboxSize := props.MailboxSize |
| 218 | if mailboxSize <= 0 { |
| 219 | mailboxSize = s.config.DefaultActorMailboxSize |
| 220 | } |
| 221 | |
| 222 | // 创建 Actor 单元 |
| 223 | cell := &actorCell{ |
| 224 | pid: pid, |
| 225 | actor: actor, |
| 226 | mailbox: make(chan envelope, mailboxSize), |
| 227 | parent: parent, |
| 228 | children: make(map[string]*PID), |
| 229 | watchers: make(map[string]*PID), |
| 230 | watching: make(map[string]*PID), |
| 231 | state: actorStateIdle, |
| 232 | supervisor: props.SupervisorStrategy, |
| 233 | ctx: ctx, |
| 234 | cancel: cancel, |
| 235 | } |
| 236 | |
| 237 | // 注册 |
| 238 | s.actors[props.Name] = cell |
| 239 | atomic.AddInt64(&s.stats.TotalActors, 1) |
| 240 | |
| 241 | // 如果有父 Actor,注册为子 Actor |
| 242 | if parent != nil { |
| 243 | if parentCell, ok := s.actors[parent.ID]; ok { |
| 244 | parentCell.children[props.Name] = pid |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // 启动 Actor 消息循环 |
| 249 | s.wg.Add(1) |
| 250 | go s.actorLoop(cell) |
| 251 | |
| 252 | // 发送 Started 消息 |
| 253 | s.SendWithSender(pid, &Started{}, nil) |
| 254 |
no test coverage detected