Exemplo n.º 1
0
func WatchForDisappearancesUnder(logger lager.Logger, client consuladapter.Client, disappearanceChan chan []string, stop <-chan struct{}, prefix string) {
	logger = logger.Session("watch-for-disappearances")

	go func() {
		logger.Info("starting")
		defer logger.Info("finished")

		keys := keySet{}

		queryOpts := &api.QueryOptions{
			WaitIndex: 0,
			WaitTime:  defaultWatchBlockDuration,
		}

		for {
			newPairs, queryMeta, err := client.KV().List(prefix, queryOpts)

			if err != nil {
				logger.Error("list-failed", err)
				select {
				case <-stop:
					return
				case <-time.After(1 * time.Second):
				}
				queryOpts.WaitIndex = 0
				continue
			}

			select {
			case <-stop:
				return
			default:
			}

			queryOpts.WaitIndex = queryMeta.LastIndex

			if newPairs == nil {
				// key not found
				_, err = client.KV().Put(&api.KVPair{Key: prefix, Value: emptyBytes}, nil)
				if err != nil {
					logger.Error("put-failed", err)
					continue
				}
			}

			newKeys := newKeySet(newPairs)
			if missing := difference(keys, newKeys); len(missing) > 0 {
				select {
				case disappearanceChan <- missing:
				case <-stop:
					return
				}
			}

			keys = newKeys
		}
	}()
}
Exemplo n.º 2
0
var _ = Describe("Presence", func() {
	var (
		presenceKey   string
		presenceValue []byte

		consulClient consuladapter.Client

		presenceRunner             ifrit.Runner
		presenceProcess            ifrit.Process
		retryInterval, presenceTTL time.Duration
		logger                     lager.Logger
		clock                      *fakeclock.FakeClock
	)

	getPresenceValue := func() ([]byte, error) {
		kvPair, _, err := consulClient.KV().Get(presenceKey, nil)
		if err != nil {
			return nil, err
		}

		if kvPair == nil || kvPair.Session == "" {
			return nil, consuladapter.NewKeyNotFoundError(presenceKey)
		}

		return kvPair.Value, nil
	}

	BeforeEach(func() {
		consulClient = consulRunner.NewClient()

		presenceKey = "some-key"
Exemplo n.º 3
0
		lockValue            []byte

		consulClient consuladapter.Client

		lockRunner    ifrit.Runner
		lockProcess   ifrit.Process
		retryInterval time.Duration
		lockTTL       time.Duration
		logger        lager.Logger

		sender *fake.FakeMetricSender
		clock  *fakeclock.FakeClock
	)

	getLockValue := func() ([]byte, error) {
		kvPair, _, err := consulClient.KV().Get(lockKey, nil)
		if err != nil {
			return nil, err
		}

		if kvPair == nil || kvPair.Session == "" {
			return nil, consuladapter.NewKeyNotFoundError(lockKey)
		}

		return kvPair.Value, nil
	}

	BeforeEach(func() {
		consulClient = consulRunner.NewClient()

		lockKey = locket.LockSchemaPath("some-key")