func (client *Multiclient) getClient(id string) (*doorman.Client, error) { c, ok := client.clients[id] if !ok { var err error c, err = doorman.NewWithID(client.addr, id, client.opts...) if err != nil { return nil, err } client.clients[id] = c } return c, nil }
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) }
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()) }