本文整理汇总了Golang中github.com/tsuru/tsuru/router.Retrieve函数的典型用法代码示例。如果您正苦于以下问题:Golang Retrieve函数的具体用法?Golang Retrieve怎么用?Golang Retrieve使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Retrieve函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RemoveRoute
func (r *fakeRouter) RemoveRoute(name string, address *url.URL) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
if !r.HasBackend(backendName) {
return router.ErrBackendNotFound
}
r.mutex.Lock()
defer r.mutex.Unlock()
if r.failuresByIp[address.String()] {
return ErrForcedFailure
}
index := -1
routes := r.backends[backendName]
for i := range routes {
if routes[i] == address.String() {
index = i
break
}
}
if index < 0 {
return router.ErrRouteNotFound
}
routes[index] = routes[len(routes)-1]
r.backends[backendName] = routes[:len(routes)-1]
return nil
}
示例2: TestSwap
func (s *ExternalSuite) TestSwap(c *check.C) {
backend1 := "b1"
backend2 := "b2"
r, err := router.Get("fake")
c.Assert(err, check.IsNil)
r.AddBackend(backend1)
addr1, _ := url.Parse("http://127.0.0.1")
r.AddRoute(backend1, addr1)
r.AddBackend(backend2)
addr2, _ := url.Parse("http://10.10.10.10")
r.AddRoute(backend2, addr2)
err = router.Swap(r, backend1, backend2)
c.Assert(err, check.IsNil)
routes1, err := r.Routes(backend1)
c.Assert(err, check.IsNil)
c.Assert(routes1, check.DeepEquals, []*url.URL{addr1})
routes2, err := r.Routes(backend2)
c.Assert(err, check.IsNil)
c.Assert(routes2, check.DeepEquals, []*url.URL{addr2})
name1, err := router.Retrieve(backend1)
c.Assert(err, check.IsNil)
c.Assert(name1, check.Equals, backend2)
name2, err := router.Retrieve(backend2)
c.Assert(err, check.IsNil)
c.Assert(name2, check.Equals, backend1)
}
示例3: TestRemoveBackendKeepsInRouter
func (s *RouterSuite) TestRemoveBackendKeepsInRouter(c *check.C) {
_, err := router.Retrieve(testBackend1)
c.Assert(err, check.Equals, router.ErrBackendNotFound)
err = s.Router.AddBackend(testBackend1)
c.Assert(err, check.IsNil)
name, err := router.Retrieve(testBackend1)
c.Assert(err, check.IsNil)
c.Assert(name, check.Equals, testBackend1)
err = s.Router.RemoveBackend(testBackend1)
c.Assert(err, check.IsNil)
name, err = router.Retrieve(testBackend1)
c.Assert(err, check.IsNil)
c.Assert(name, check.Equals, testBackend1)
}
示例4: TestSwapCnameOnly
func (s *ExternalSuite) TestSwapCnameOnly(c *check.C) {
backend1 := "bx1"
backend2 := "bx2"
r, err := router.Get("fake")
c.Assert(err, check.IsNil)
cnameRouter, ok := r.(router.CNameRouter)
c.Assert(ok, check.Equals, true)
err = r.AddBackend(backend1)
c.Assert(err, check.IsNil)
addr1, err := url.Parse("http://127.0.0.1")
c.Assert(err, check.IsNil)
r.AddRoute(backend1, addr1)
err = cnameRouter.SetCName("cname.com", backend1)
c.Assert(err, check.IsNil)
err = r.AddBackend(backend2)
c.Assert(err, check.IsNil)
addr2, err := url.Parse("http://10.10.10.10")
c.Assert(err, check.IsNil)
r.AddRoute(backend2, addr2)
err = router.Swap(r, backend1, backend2, true)
c.Assert(err, check.IsNil)
routes1, err := r.Routes(backend1)
c.Assert(err, check.IsNil)
c.Assert(routes1, check.DeepEquals, []*url.URL{addr1})
routes2, err := r.Routes(backend2)
c.Assert(err, check.IsNil)
c.Assert(routes2, check.DeepEquals, []*url.URL{addr2})
name1, err := router.Retrieve(backend1)
c.Assert(err, check.IsNil)
c.Assert(name1, check.Equals, backend1)
name2, err := router.Retrieve(backend2)
c.Assert(err, check.IsNil)
c.Assert(name2, check.Equals, backend2)
cnames, err := cnameRouter.CNames(backend1)
c.Assert(err, check.IsNil)
c.Assert(cnames, check.HasLen, 0)
expected := []*url.URL{{Host: "cname.com"}}
cnames, err = cnameRouter.CNames(backend2)
c.Assert(err, check.IsNil)
c.Assert(expected, check.DeepEquals, cnames)
err = router.Swap(r, backend1, backend2, true)
c.Assert(err, check.IsNil)
cnames, err = cnameRouter.CNames(backend1)
c.Assert(err, check.IsNil)
c.Assert(expected, check.DeepEquals, cnames)
cnames, err = cnameRouter.CNames(backend2)
c.Assert(err, check.IsNil)
c.Assert(cnames, check.HasLen, 0)
}
示例5: RemoveRoutes
func (r *fakeRouter) RemoveRoutes(name string, addresses []*url.URL) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
if !r.HasBackend(backendName) {
return router.ErrBackendNotFound
}
r.mutex.Lock()
defer r.mutex.Unlock()
for _, addr := range addresses {
if r.failuresByIp[addr.Host] {
return ErrForcedFailure
}
}
routes := r.backends[backendName]
for _, addr := range addresses {
for i := range routes {
if routes[i] == addr.Host {
routes = append(routes[:i], routes[i+1:]...)
break
}
}
}
r.backends[backendName] = routes
return nil
}
示例6: TestRemoveBackend
func (s *S) TestRemoveBackend(c *check.C) {
s.handler.RspCode = http.StatusNoContent
err := router.Store("myapp", "myapp", routerName)
c.Assert(err, check.IsNil)
data := galebData{
Name: "myapp",
BackendPoolId: s.server.URL + "/api/backend1",
RootRuleId: s.server.URL + "/api/rule1",
VirtualHostId: s.server.URL + "/api/vh1",
CNames: []galebCNameData{
{CName: "my.1.cname", VirtualHostId: s.server.URL + "/api/vh2"},
{CName: "my.2.cname", VirtualHostId: s.server.URL + "/api/vh3"},
},
}
err = data.save()
c.Assert(err, check.IsNil)
gRouter, err := createRouter("routers:galeb")
c.Assert(err, check.IsNil)
err = gRouter.RemoveBackend("myapp")
c.Assert(err, check.IsNil)
c.Assert(s.handler.Url, check.DeepEquals, []string{
"/api/vh1", "/api/vh2", "/api/vh3", "/api/rule1", "/api/backend1",
})
_, err = router.Retrieve("myapp")
c.Assert(err, check.Equals, router.ErrBackendNotFound)
_, err = getGalebData("myapp")
c.Assert(err, check.ErrorMatches, "not found")
}
示例7: RemoveRoute
func (r *fakeRouter) RemoveRoute(name, ip string) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
if !r.HasBackend(backendName) {
return ErrBackendNotFound
}
r.mutex.Lock()
defer r.mutex.Unlock()
index := -1
routes := r.backends[backendName]
for i := range routes {
if routes[i] == ip {
index = i
break
}
}
if index < 0 {
return errors.New("Route not found")
}
routes[index] = routes[len(routes)-1]
r.backends[backendName] = routes[:len(routes)-1]
return nil
}
示例8: SetCName
func (r *galebRouter) SetCName(cname, name string) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
domain, err := config.GetString(r.prefix + ":domain")
if err != nil {
return err
}
if !router.ValidCName(cname, domain) {
return router.ErrCNameNotAllowed
}
data, err := getGalebData(backendName)
if err != nil {
return err
}
client, err := r.getClient()
if err != nil {
return err
}
for _, val := range data.CNames {
if val.CName == cname {
return router.ErrCNameExists
}
}
virtualHostParams := galebClient.VirtualHostParams{
Name: cname,
RuleDefault: data.RootRuleId,
}
virtualHostId, err := client.AddVirtualHost(&virtualHostParams)
if err != nil {
return err
}
return data.addCName(cname, virtualHostId)
}
示例9: Routes
func (r *hipacheRouter) Routes(name string) ([]*url.URL, error) {
backendName, err := router.Retrieve(name)
if err != nil {
return nil, err
}
domain, err := config.GetString(r.prefix + ":domain")
if err != nil {
return nil, &router.RouterError{Op: "routes", Err: err}
}
frontend := "frontend:" + backendName + "." + domain
conn := r.connect()
defer conn.Close()
routes, err := redis.Strings(conn.Do("LRANGE", frontend, 1, -1))
if err != nil {
return nil, &router.RouterError{Op: "routes", Err: err}
}
result := make([]*url.URL, len(routes))
for i, route := range routes {
result[i], err = url.Parse(route)
if err != nil {
return nil, err
}
}
return result, nil
}
示例10: SetCName
func (r *vulcandRouter) SetCName(cname, name string) error {
usedName, err := router.Retrieve(name)
if err != nil {
return err
}
if !router.ValidCName(cname, r.domain) {
return router.ErrCNameNotAllowed
}
frontendName := r.frontendName(cname)
if found, _ := r.client.GetFrontend(engine.FrontendKey{Id: frontendName}); found != nil {
return router.ErrCNameExists
}
frontend, err := engine.NewHTTPFrontend(
frontendName,
r.backendName(usedName),
fmt.Sprintf(`Host(%q)`, cname),
engine.HTTPFrontendSettings{},
)
if err != nil {
return &router.RouterError{Err: err, Op: "set-cname"}
}
err = r.client.UpsertFrontend(*frontend, engine.NoTTL)
if err != nil {
return &router.RouterError{Err: err, Op: "set-cname"}
}
return nil
}
示例11: SetCName
func (r *hipacheRouter) SetCName(cname, name string) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
domain, err := config.GetString(r.prefix + ":domain")
if err != nil {
return &routeError{"setCName", err}
}
if !r.validCName(cname) {
err := errors.New(fmt.Sprintf("Invalid CNAME %s. You can't use tsuru's application domain.", cname))
return &routeError{"setCName", err}
}
frontend := "frontend:" + backendName + "." + domain
conn := r.connect()
defer conn.Close()
routes, err := redis.Strings(conn.Do("LRANGE", frontend, 0, -1))
if err != nil {
return &routeError{"get", err}
}
_, err = conn.Do("RPUSH", "cname:"+backendName, cname)
if err != nil {
return &routeError{"set", err}
}
frontend = "frontend:" + cname
for _, r := range routes {
_, err := conn.Do("RPUSH", frontend, r)
if err != nil {
return &routeError{"setCName", err}
}
}
return nil
}
示例12: RemoveRoute
func (r *hipacheRouter) RemoveRoute(name string, address *url.URL) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
domain, err := config.GetString(r.prefix + ":domain")
if err != nil {
return &routeError{"remove", err}
}
frontend := "frontend:" + backendName + "." + domain
if err := r.removeElement(frontend, address.String()); err != nil {
return err
}
cnames, err := r.getCNames(backendName)
if err != nil {
return &routeError{"remove", err}
}
if cnames == nil {
return nil
}
for _, cname := range cnames {
err = r.removeElement("frontend:"+cname, address.String())
if err != nil {
return err
}
}
return nil
}
示例13: AddRoute
func (r *hipacheRouter) AddRoute(name string, address *url.URL) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
domain, err := config.GetString(r.prefix + ":domain")
if err != nil {
log.Errorf("error on getting hipache domain in add route for %s - %s", backendName, address)
return &routeError{"add", err}
}
frontend := "frontend:" + backendName + "." + domain
if err := r.addRoute(frontend, address.String()); err != nil {
log.Errorf("error on add route for %s - %s", backendName, address)
return &routeError{"add", err}
}
cnames, err := r.getCNames(backendName)
if err != nil {
log.Errorf("error on get cname in add route for %s - %s", backendName, address)
return err
}
if cnames == nil {
return nil
}
for _, cname := range cnames {
err = r.addRoute("frontend:"+cname, address.String())
if err != nil {
return err
}
}
return nil
}
示例14: AddRoutes
func (r *galebRouter) AddRoutes(name string, addresses []*url.URL) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
return r.client.AddBackends(addresses, r.poolName(backendName))
}
示例15: AddRoute
func (r *galebRouter) AddRoute(name string, address *url.URL) error {
backendName, err := router.Retrieve(name)
if err != nil {
return err
}
data, err := getGalebData(backendName)
if err != nil {
return err
}
for _, r := range data.Reals {
if r.Real == address.Host {
return router.ErrRouteExists
}
}
client, err := r.getClient()
if err != nil {
return err
}
host, portStr, _ := net.SplitHostPort(address.Host)
port, _ := strconv.Atoi(portStr)
params := galebClient.BackendParams{
Ip: host,
Port: port,
BackendPool: data.BackendPoolId,
}
backendId, err := client.AddBackend(¶ms)
if err != nil {
return err
}
return data.addReal(address.Host, backendId)
}