Пример #1
0
// Create a new dispatcher
func NewDispatcher(transport *http.Transport, cfg config.ValkyrieConfig) *Dispatcher {
	noneStats, _ := stats.LoadStatsReporter("none")
	return &Dispatcher{Transport: transport, Config: cfg, Stats: noneStats}
}
Пример #2
0
// Start the valkyrie web-server.
func main() {

	// Bind address
	bindAddress := flag.String("bind", ":443", "The address and port to bind to")

	// TLS
	useTLS := flag.Bool("tls", true, "Listen / Serve using TLS")
	tlsCert := flag.String("cert", "cert.pem", "The TLS certificate file")
	tlsKey := flag.String("key", "key.pem", "The TLS key file")

	// Rate limit
	doThrottle := flag.Bool("throttle", true, "Rate limit the server")
	reqPerSec := flag.Float64("rate", 1100.0, "Rate limit req/sec")

	// Network
	timeout := flag.Int("timeout", 30, "Back-end service timeout in seconds")
	keepAlive := flag.Bool("keepalive", true, "Whether to re-use TCP connections between requests")
	compression := flag.Bool("compress", false, "Whether a compression header can be added to requests")
	maxIdle := flag.Int("maxidle", 10, "The maximum idle connections (keep-alive) to keep per-host")

	// Cassandra config
	keyspace := flag.String("keyspace", "valkyrie", "The cassandra configuration keyspace")
	user := flag.String("user", "valkyrie", "The cassandra user")
	pass := flag.String("pass", "valkyrie", "The cassandra password")
	cp := flag.String("cp", "localhost", "The comma-separated list of cassandra contact points")

	// service discovery config
	discoType := flag.String("disco", "consul", "service discover type")
	discoAddress := flag.String("discoAddress", "localhost:8500", "address for the service discovery endpoint")

	// Stats reporter
	reportTo := flag.String("report", "console", "The stats reporter to use")

	// Parse the command line flags listed above
	flag.Parse()

	// Init cassandra credentials
	cassandraArgs := append([]string{*keyspace, *user, *pass}, strings.Split(*cp, ",")...)

	// Load config
	cfg, err := config.LoadConfig("cassandra", cassandraArgs...)
	if err != nil {
		log.Fatal(err)
	}
	defer cfg.Close()

	// Init transport and dispatcher
	timeoutDuration := time.Duration(*timeout) * time.Second
	txp := transport.NewDialTimeoutTransport(timeoutDuration, !*keepAlive, !*compression, *maxIdle)
	dispatcher := core.NewDispatcher(txp, cfg)

	// Load stats reporter
	var reporter stats.ValkyrieStats
	if *reportTo == "cassandra" {
		reporter, err = stats.LoadStatsReporter(*reportTo, cassandraArgs...)
	} else {
		reporter, err = stats.LoadStatsReporter(*reportTo)
	}
	if err != nil {
		log.Fatal(err)
	}
	defer reporter.Close()
	dispatcher.ReportTo(reporter)

	var discovery disco.ValkyrieDisco
	if *discoType == "consul" {
		discovery, err = disco.LoadServiceDiscovery(*discoType, *discoAddress)
	} else {
		discovery, err = disco.LoadServiceDiscovery(*discoType)
	}
	if err != nil {
		log.Fatal(err)
	}
	dispatcher.DiscoverFrom(discovery)

	// Init http multiplexer
	mux := http.NewServeMux()
	if *doThrottle {
		throttle := time.Tick(time.Second / time.Duration(*reqPerSec))
		mux.Handle("/", dispatcher.DispatchHandler(throttle))
	} else {
		log.Println("Warning: rate limit disabled")
		mux.Handle("/", dispatcher.DispatchHandler(nil))
	}

	// Start server
	log.Println("Starting the valkyrie web-server on", *bindAddress)
	srv := &http.Server{Handler: mux, Addr: *bindAddress}
	if *useTLS {
		err = srv.ListenAndServeTLS(*tlsCert, *tlsKey)
	} else {
		log.Println("Warning: TLS disabled")
		err = srv.ListenAndServe()
	}
	if err != nil {
		log.Fatal("Unable to start web server: ", err)
	}
}