Example #1
0
func (s *LimitSuite) SetUpTest(c *C) {
	start := time.Date(2012, 3, 4, 5, 6, 7, 0, time.UTC)
	s.timeProvider = &timeutils.FreezedTime{CurrentTime: start}
	b, err := backend.NewMemoryBackend(s.timeProvider)
	c.Assert(err, IsNil)
	s.backend = b
	s.rateLimiter = &BasicRateLimiter{Backend: s.backend}
	s.failingRateLimiter = &BasicRateLimiter{Backend: &backend.FailingBackend{}}
}
func (s *ProxySuite) SetUpTest(c *C) {
	start := time.Date(2012, 3, 4, 5, 6, 7, 0, time.UTC)
	s.timeProvider = &timeutils.FreezedTime{CurrentTime: start}
	backend, err := backend.NewMemoryBackend(s.timeProvider)
	c.Assert(err, IsNil)
	s.backend = backend
	s.limiter = &ratelimit.BasicRateLimiter{Backend: s.backend}
	s.authHeaders = http.Header{"Authorization": []string{"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="}}
}
Example #3
0
func (s *Service) initProxy() (*vulcan.ReverseProxy, error) {
	var b backend.Backend
	var err error

	if s.options.backend == "cassandra" {
		cassandraConfig := &backend.CassandraConfig{
			Servers:       s.options.cassandraServers,
			Keyspace:      s.options.cassandraKeyspace,
			Consistency:   gocql.One,
			LaunchCleanup: s.options.cassandraCleanup,
			CleanupTime:   s.options.cassandraCleanupOptions.T,
		}
		b, err = backend.NewCassandraBackend(
			cassandraConfig, &timeutils.RealTime{})
		if err != nil {
			return nil, err
		}
	} else if s.options.backend == "memory" {
		b, err = backend.NewMemoryBackend(&timeutils.RealTime{})
		if err != nil {
			return nil, err
		}
	} else {
		return nil, fmt.Errorf("Unsupported backend")
	}

	var loadBalancer loadbalance.Balancer
	if s.options.loadBalancer == "roundrobin" || s.options.loadBalancer == "random" {
		loadBalancer = roundrobin.NewRoundRobin(&timeutils.RealTime{})
	} else {
		return nil, fmt.Errorf("Unsupported loadbalancing algo")
	}

	outputs := strings.Split(s.options.metricsOutput, ",")
	for _, v := range outputs {
		metrics.AddOutput(v)
	}

	if s.options.sslCertFile != "" && s.options.sslKeyFile == "" {
		return nil, fmt.Errorf("invalid configuration: -sslkey unspecified, but -sslcert was specified.")
	} else if s.options.sslCertFile == "" && s.options.sslKeyFile != "" {
		return nil, fmt.Errorf("invalid configuration: -sslcert unspecified, but -sslkey was specified.")
	}

	var discoveryService discovery.Service

	if s.options.discovery != "" {
		discoveryUrl := s.options.discovery
		if s.options.discovery == "etcd" {
			// TODO remove this compat hack?
			discoveryUrl = "etcd://" + strings.Join(s.options.etcdEndpoints, ",")
			discoveryService = discovery.NewEtcd(s.options.etcdEndpoints)
		}

		discoveryService, err = discovery.New(discoveryUrl)
		if err != nil {
			return nil, err
		}
	}

	controller := &js.JsController{
		CodeGetter:       js.NewFileGetter(s.options.codePath),
		DiscoveryService: discoveryService,
	}

	proxySettings := &vulcan.ProxySettings{
		Controller:       controller,
		ThrottlerBackend: b,
		LoadBalancer:     loadBalancer,
	}

	proxy, err := vulcan.NewReverseProxy(&s.metrics, proxySettings)
	if err != nil {
		return nil, err
	}
	controller.Client = proxy
	return proxy, nil
}