Ejemplo n.º 1
0
func main() {
	flag.Parse()
	log.Infof("Simulating %v clients.", *count)
	for i := 0; i < *count; i++ {
		id := uuid.New()
		log.Infof("client %v with id %v", i, id)

		client, err := doorman.NewWithID(*addr, id, doorman.DialOpts(grpc.WithInsecure()))
		if err != nil {
			log.Exit(err)
		}
		defer client.Close()

		res, err := client.Resource(*resource, *initialCapacity)
		if err != nil {
			log.Exit(err)
		}

		go manipulateCapacity(res, *initialCapacity, id)

		conn, err := grpc.Dial(*target, grpc.WithInsecure())
		if err != nil {
			log.Exitf("did not connect: %v", err)
		}
		defer conn.Close()

		c := pb.NewGreeterClient(conn)
		rl := ratelimiter.NewQPS(res)

		for i := 0; i < *workers; i++ {
			go func() {
				ctx := context.Background()
				for {
					if err := rl.Wait(ctx); err != nil {
						log.Exitf("rl.Wait: %v", err)
					}

					ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
					if _, err := c.SayHello(ctx, &pb.HelloRequest{Name: *resource}); err != nil {
						log.Error(err)
					}
					cancel()
				}
			}()
		}
	}
	http.Handle("/metrics", prometheus.Handler())
	http.ListenAndServe(fmt.Sprintf(":%v", *port), nil)

}
Ejemplo n.º 2
0
func main() {
	flag.Parse()

	if *server == "" || *resource == "" {
		log.Exit("both --server and --resource must be specified")
	}

	if *clientID == "" {
		log.Exit("--client_id must be set")
	}

	var opts []grpc.DialOption
	if len(*caFile) != 0 {
		var creds credentials.TransportAuthenticator
		var err error
		creds, err = credentials.NewClientTLSFromFile(*caFile, "")
		if err != nil {
			log.Exitf("Failed to create TLS credentials %v", err)
		}

		opts = append(opts, grpc.WithTransportCredentials(creds))
	} else {
		opts = append(opts, grpc.WithInsecure())
	}

	client, err := doorman.NewWithID(*server, *clientID, doorman.DialOpts(opts...))

	if err != nil {
		log.Exitf("could not create client: %v", err)
	}

	defer client.Close()
	resource, err := client.Resource(*resource, *wants)

	if err != nil {
		log.Exitf("could not acquire resource: %v", err)
	}

	fmt.Println(<-resource.Capacity())
}
Ejemplo n.º 3
0
func main() {
	flag.Parse()

	if *server == "" {
		fmt.Fprintf(os.Stderr, "--server cannot be empty.\n")
		os.Exit(1)
	}

	if *port != 0 {
		http.Handle("/metrics", prometheus.Handler())
		go http.ListenAndServe(fmt.Sprintf(":%v", *port), nil)
	}

	var opts []grpc.DialOption
	if len(*caFile) != 0 {
		var creds credentials.TransportAuthenticator
		var err error
		creds, err = credentials.NewClientTLSFromFile(*caFile, "")
		if err != nil {
			log.Exitf("Failed to create TLS credentials %v\n", err)
		}

		opts = append(opts, grpc.WithTransportCredentials(creds))
	} else {
		opts = append(opts, grpc.WithInsecure())
	}

	c := &Multiclient{
		addr:      *server,
		opts:      []doorman.Option{doorman.DialOpts(opts...)},
		clients:   make(map[string]*doorman.Client),
		resources: make(map[key]doorman.Resource),

		capacities: make(map[key]float64),
	}

	defer c.Close()

	line, err := readline.NewEx(&readline.Config{
		Prompt:          "> ",
		HistoryFile:     "/tmp/doorman_shell.tmp",
		InterruptPrompt: "\nInterrupt, Press Ctrl+D to exit",
	})
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
		os.Exit(1)
	}
	for {
		data, err := line.Readline()
		if err == io.EOF {

			break
		}

		if err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		}

		command, err := shlex.Split(data)
		if err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		}

		err = c.Eval(command)
		if err == io.EOF {
			break
		}
		if err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
		}
	}
}