Exemplo n.º 1
0
func (h syncHandler) Rcv(m Msg, ctx RcvContext) error {
	req := m.Data().(syncReq)
	sm := msg{
		MsgData: req.Data,
		MsgFrom: m.From(),
		MsgTo:   m.To(),
	}
	sc := syncRcvContext{
		RcvContext: ctx,
		id:         req.ID,
		from:       m.From(),
	}
	err := h.handler.Rcv(sm, &sc)
	if err != nil {
		ctx.AbortTx()
		r := syncRes{
			ID:  req.ID,
			Err: bhgob.Error(err.Error()),
		}
		ctx.Reply(m, r)
		return err
	}
	if !sc.replied {
		r := syncRes{
			ID: req.ID,
		}
		ctx.Reply(m, r)
	}
	return nil
}
Exemplo n.º 2
0
package beehive

import (
	"encoding/gob"
	"sync"

	bhgob "github.com/kandoo/beehive/gob"
)

var (
	// ErrSyncStopped returned when the sync handler is stopped before receiving
	// the response.
	ErrSyncStopped = bhgob.Error("sync: stopped")
	// ErrSyncNoSuchRequest returned when we cannot find the request for that
	// response.
	ErrSyncNoSuchRequest = bhgob.Error("sync: request not found")
	// ErrSyncDuplicateResponse returned when there is a duplicate repsonse to the
	// sync request.
	ErrSyncDuplicateResponse = bhgob.Error("sync: duplicate response")
)

// syncReq represents a generic sync request.
type syncReq struct {
	ID   uint64
	Data interface{} // Data of the request. Must be registered in gob.
}

// Type returns the type of this request. It is unique for each data type.
func (r syncReq) Type() string {
	if r.Data == nil {
		return "sync-req"