Exemple #1
0
func NewCandidate(repo repository.Repository, domain string) (Candidate, error) {
	id, err := kit.NewID()
	if err != nil {
		return nil, err
	}

	c := &candidate{
		id:     id,
		db:     repo.DB(),
		domain: domain,
		leader: false,
		ch:     make(chan struct{}, 1),
		m:      &sync.RWMutex{},
	}

	err = c.setupDB()
	if err != nil {
		return nil, err
	}

	scheduling.NewFixedDelayScheduler(time.Second, 3*time.Second).Schedule(c.elect)

	log.Println("start leader election candidate with id", id, "for domain", domain)

	return c, nil
}
Exemple #2
0
func NewDirectEventStore(repo repository.Repository, stream string, consumer string) (EventStore, error) {
	es := &direct{
		repo: repo,

		stream:   stream,
		consumer: consumer,

		locker: &sync.RWMutex{},
		types:  map[string]reflect.Type{},

		aggregators:    map[string]*list.List{},
		rawAggregators: map[string][]AggregatorHandler{},

		observers:    map[string]*list.List{},
		rawObservers: map[string][]ObserverHandler{},

		newEventChannelName: "es_" + stream,
		resumeCh:            make(chan struct{}, 1),
	}

	kit.SafeGo(es.listen)

	err := es.setupDB()
	if err != nil {
		return nil, err
	}

	lastID, err := fetchLastID(repo.DB(), stream, consumer)
	if err != nil {
		return nil, err
	}

	es.lastID = lastID

	return es, nil
}