Esempio n. 1
0
// DispatchResponse enqueues an incoming response into the response channel for
// the corresponding request.
//
// header: Message header for the response.
//
// data: User data in the response message.
//
// Returns nil on success.
func (this *Messenger) DispatchResponse(header *msgpb.Header,
	data []byte) error {

	lock, errLock := this.ctlr.Lock("this.requestMap")
	if errLock != nil {
		return errLock
	}
	defer lock.Unlock()

	response := header.GetResponse()
	requestID := response.GetRequestId()
	responseCh, found := this.requestMap[requestID]
	if !found {
		this.Errorf("could not find request %d to dispatch response %s",
			requestID, header)
		return errs.ErrNotExist
	}

	// Close the lock early to release the resources.
	lock.Unlock()

	entry := &Entry{header: header, data: data}

	select {
	case responseCh <- entry:
		return nil
	default:
		this.Errorf("could not queue response %s to request %d", header,
			requestID)
		return errs.ErrOverflow
	}
}