Exemplo n.º 1
0
// SetRoute tells all members to add the route to their database.
// rolls back on failure
func (r Redis) SetRoute(route core.Route) error {
	conn := pool.Get()
	defer conn.Close()

	// publishJson to others
	err := r.publishJson(conn, "set-route", route)
	if err != nil {
		// nothing to rollback yet (nobody received)
		return err
	}

	actionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-route %s", route))))

	// ensure all members applied action
	err = r.waitForMembers(conn, actionHash)
	if err != nil {
		uActionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("delete-route %s", route))))
		// cleanup rollback cruft. clear actionHash ensures no mistakes on re-submit
		defer conn.Do("DEL", uActionHash, actionHash)
		// attempt rollback - no need to waitForMembers here
		uerr := r.publishJson(conn, "delete-route", route)
		if uerr != nil {
			err = fmt.Errorf("%v - %v", err, uerr)
		}
		return err
	}

	if database.CentralStore {
		return database.SetRoute(route)
	}

	return nil
}
Exemplo n.º 2
0
func SetRoute(route core.Route) error {
	// in case of failure
	oldRoutes, err := database.GetRoutes()
	if err != nil {
		return err
	}

	// apply to proxymgr
	err = proxymgr.SetRoute(route)
	if err != nil {
		return err
	}

	if !database.CentralStore {
		// save to backend
		err = database.SetRoute(route)
		if err != nil {
			// undo proxymgr action
			if uerr := proxymgr.SetRoutes(oldRoutes); uerr != nil {
				err = fmt.Errorf("%v - %v", err.Error(), uerr.Error())
			}
			return err
		}
	}
	return nil
}
Exemplo n.º 3
0
func TestSetRoute(t *testing.T) {
	if err := database.SetRoute(testRoute); err != nil {
		t.Errorf("Failed to SET route - %v", err)
	}

	if err := database.SetRoute(testRoute); err != nil {
		t.Errorf("Failed to SET route - %v", err)
	}

	routes, err := database.GetRoutes()
	if err != nil {
		t.Error(err)
	}

	if len(routes) != 1 {
		t.Errorf("Wrong number of routes")
	}
}
Exemplo n.º 4
0
func (n None) SetRoute(route core.Route) error {
	err := common.SetRoute(route)
	if err != nil {
		return err
	}
	if database.CentralStore {
		return database.SetRoute(route)
	}
	return nil
}