Ejemplo n.º 1
0
func (e *etcd) SaveRoute(route models.Route) error {
	key := generateHttpRouteKey(route)

	retries := 0

	for retries <= maxRetries {
		response, err := e.keysAPI.Get(context.Background(), key, readOpts())

		// Update
		if response != nil && err == nil {
			var existingRoute models.Route
			err = json.Unmarshal([]byte(response.Node.Value), &existingRoute)
			if err != nil {
				return err
			}

			route.ModificationTag = existingRoute.ModificationTag
			route.ModificationTag.Increment()

			routeJSON, _ := json.Marshal(route)
			_, err = e.keysAPI.Set(context.Background(), key, string(routeJSON), updateOptsWithTTL(route.TTL, response.Node.ModifiedIndex))
			if err == nil {
				break
			}
		} else if cerr, ok := err.(client.Error); ok && cerr.Code == client.ErrorCodeKeyNotFound { //create
			// Delete came in between a read and an update
			if retries > 0 {
				return ErrorConflict
			}

			var tag models.ModificationTag
			tag, err = models.NewModificationTag()
			if err != nil {
				return err
			}
			route.ModificationTag = tag
			routeJSON, _ := json.Marshal(route)
			_, err = e.keysAPI.Set(ctx(), key, string(routeJSON), createOpts(route.TTL))
			if err == nil {
				break
			}
		}

		// only retry on a compare and swap error
		if cerr, ok := err.(client.Error); ok && cerr.Code == client.ErrorCodeTestFailed {
			retries++
		} else {
			return err
		}
	}

	if retries > maxRetries {
		return ErrorConflict
	}
	return nil
}
Ejemplo n.º 2
0
		BeforeEach(func() {
			etcd, err = db.NewETCD([]string{"im-not-really-running"})
			Expect(err).NotTo(HaveOccurred())
		})

		It("returns an error", func() {
			Expect(etcd.Connect()).To(HaveOccurred())
		})
	})

	Describe("etcd", func() {
		var (
			etcd             db.DB
			fakeEtcd         db.DB
			fakeKeysAPI      *fakes.FakeKeysAPI
			err              error
			route            models.Route
			tcpRouteMapping1 models.TcpRouteMapping
		)

		BeforeEach(func() {
			etcd, err = db.NewETCD(etcdRunner.NodeURLS())
			Expect(err).NotTo(HaveOccurred())
			route = models.Route{
				Route:           "post_here",
				Port:            7000,
				IP:              "1.2.3.4",
				TTL:             50,
				LogGuid:         "my-guid",
				RouteServiceUrl: "https://rs.com",
			}