Пример #1
0
func SetRoutes(routes []core.Route) error {
	// in case of failure
	oldRoutes, err := database.GetRoutes()
	if err != nil {
		return err
	}

	// apply routes to proxymgr
	err = proxymgr.SetRoutes(routes)
	if err != nil {
		return err
	}

	if !database.CentralStore {
		// save to backend
		err = database.SetRoutes(routes)
		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
}
Пример #2
0
func (n None) SetRoutes(routes []core.Route) error {
	err := common.SetRoutes(routes)
	if err != nil {
		return err
	}
	if database.CentralStore {
		return database.SetRoutes(routes)
	}
	return nil
}
Пример #3
0
func TestSetRoutes(t *testing.T) {
	routes := []core.Route{testRoute}

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

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

	if len(routes) != 1 {
		t.Errorf("Wrong number of routes")
	}
}
Пример #4
0
// SetRoutes tells all members to replace the routes in their database with a new set.
// rolls back on failure
func (r Redis) SetRoutes(routes []core.Route) error {
	conn := pool.Get()
	defer conn.Close()

	oldRoutes, err := common.GetRoutes()
	if err != nil {
		return err
	}

	// publishJson to others
	err = r.publishJson(conn, "set-routes", routes)
	if err != nil {
		// if i failed to publishJson, request should fail
		return err
	}

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

	// ensure all members applied action
	err = r.waitForMembers(conn, actionHash)
	if err != nil {
		uActionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-routes %s", oldRoutes))))
		// 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, "set-routes", oldRoutes)
		if uerr != nil {
			err = fmt.Errorf("%v - %v", err, uerr)
		}
		return err
	}

	if database.CentralStore {
		return database.SetRoutes(routes)
	}

	return nil
}