func (a *Actor) checkAndSend(c chan messages.Message, m messages.Message) { defer func() { if r := recover(); r != nil { utils.Log("debug", a.res+"Trying to send on closed channel.") } }() c <- m }
func (a *Actor) Run() { defer func() { utils.Log("debug", a.res+": Stopped running.") }() utils.Log("debug", a.res+": Started running") for { select { case requestWrapper := <-a.Inbox: if requestWrapper.Res == a.res { // if the resource of the message is this actor's resource messageString, _ := json.Marshal(requestWrapper.Message) utils.Log("debug", a.res+": "+string(messageString)) response := handleRequest(a, requestWrapper) a.checkAndSend(requestWrapper.Listener, response) utils.Log("debug", "") // TODO stop the actor if it belongs to an item and the item is deleted // TODO stop the actor if it belongs to an item and the item doesn't exist // TODO stop the actor if it belongs to an entity and 'get' returns an empty array (not sure though) } else { // if the resource belongs to a children actor childRes := getChildRes(requestWrapper.Res, a.res) actor, exists := a.children[childRes] if !exists { // if children doesn't exists, create a child actor for the res actor = CreateActor(childRes, a.level+1, a.Inbox) go actor.Run() a.children[childRes] = actor } // forward message to the children actor actor.Inbox <- requestWrapper } } } }