Пример #1
0
func buildClient(t *testing.T, port int, auth bool) pb.RpcCacheClient {
	url := fmt.Sprintf("localhost:%d", port)
	if !auth {
		conn, err := grpc.Dial(url, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second))
		assert.NoError(t, err)
		return pb.NewRpcCacheClient(conn)
	}
	cert, err := tls.LoadX509KeyPair(testCert, testKey)
	assert.NoError(t, err)
	ca, err := ioutil.ReadFile(testCa)
	assert.NoError(t, err)
	config := tls.Config{
		Certificates: []tls.Certificate{cert},
		RootCAs:      x509.NewCertPool(),
	}
	assert.True(t, config.RootCAs.AppendCertsFromPEM(ca))
	conn, err := grpc.Dial(url, grpc.WithTransportCredentials(credentials.NewTLS(&config)), grpc.WithTimeout(5*time.Second))
	assert.NoError(t, err)
	return pb.NewRpcCacheClient(conn)
}
Пример #2
0
func (cache *rpcCache) connect(config *core.Configuration) {
	// Change grpc to log using our implementation
	grpclog.SetLogger(&grpcLogMabob{})
	log.Info("Connecting to RPC cache at %s", config.Cache.RpcUrl)
	opts := []grpc.DialOption{grpc.WithTimeout(cache.timeout)}
	if config.Cache.RpcPublicKey != "" || config.Cache.RpcCACert != "" || config.Cache.RpcSecure {
		auth, err := loadAuth(config.Cache.RpcCACert, config.Cache.RpcPublicKey, config.Cache.RpcPrivateKey)
		if err != nil {
			log.Warning("Failed to load RPC cache auth keys: %s", err)
			return
		}
		opts = append(opts, auth)
	} else {
		opts = append(opts, grpc.WithInsecure())
	}
	connection, err := grpc.Dial(config.Cache.RpcUrl, opts...)
	if err != nil {
		cache.Connecting = false
		log.Warning("Failed to connect to RPC cache: %s", err)
		return
	}
	// Note that we have to actually send it a message here to validate the connection;
	// Dial() only seems to return errors for superficial failures like syntactically invalid addresses,
	// it will return essentially immediately even if the server doesn't exist.
	healthclient := healthpb.NewHealthClient(connection)
	ctx, cancel := context.WithTimeout(context.Background(), cache.timeout)
	defer cancel()
	resp, err := healthclient.Check(ctx, &healthpb.HealthCheckRequest{Service: "plz-rpc-cache"})
	if err != nil {
		cache.Connecting = false
		log.Warning("Failed to contact RPC cache: %s", err)
	} else if resp.Status != healthpb.HealthCheckResponse_SERVING {
		cache.Connecting = false
		log.Warning("RPC cache says it is not serving (%d)", resp.Status)
	} else {
		cache.connection = connection
		cache.client = pb.NewRpcCacheClient(connection)
		cache.Connected = true
		cache.Connecting = false
		log.Info("RPC cache connected after %0.2fs", time.Since(cache.startTime).Seconds())
	}
}