Search in sources :

Example 1 with CancellationListener

use of io.grpc.Context.CancellationListener in project grpc-java by grpc.

the class ClientXdsClientV3Test method createLrsService.

@Override
protected BindableService createLrsService() {
    return new LoadReportingServiceImplBase() {

        @Override
        public StreamObserver<LoadStatsRequest> streamLoadStats(StreamObserver<LoadStatsResponse> responseObserver) {
            assertThat(lrsEnded.get()).isTrue();
            lrsEnded.set(false);
            @SuppressWarnings("unchecked") StreamObserver<LoadStatsRequest> requestObserver = mock(StreamObserver.class);
            LrsRpcCall call = new LrsRpcCallV3(requestObserver, responseObserver);
            Context.current().addListener(new CancellationListener() {

                @Override
                public void cancelled(Context context) {
                    lrsEnded.set(true);
                }
            }, MoreExecutors.directExecutor());
            loadReportCalls.offer(call);
            return requestObserver;
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Context(io.grpc.Context) UpstreamTlsContext(io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext) CertificateValidationContext(io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CertificateValidationContext) CommonTlsContext(io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CommonTlsContext) LoadStatsRequest(io.envoyproxy.envoy.service.load_stats.v3.LoadStatsRequest) CancellationListener(io.grpc.Context.CancellationListener) LoadReportingServiceImplBase(io.envoyproxy.envoy.service.load_stats.v3.LoadReportingServiceGrpc.LoadReportingServiceImplBase)

Example 2 with CancellationListener

use of io.grpc.Context.CancellationListener in project grpc-java by grpc.

the class HealthServiceImpl method watch.

@Override
public void watch(HealthCheckRequest request, final StreamObserver<HealthCheckResponse> responseObserver) {
    final String service = request.getService();
    synchronized (watchLock) {
        ServingStatus status = statusMap.get(service);
        responseObserver.onNext(getResponseForWatch(status));
        IdentityHashMap<StreamObserver<HealthCheckResponse>, Boolean> serviceWatchers = watchers.get(service);
        if (serviceWatchers == null) {
            serviceWatchers = new IdentityHashMap<>();
            watchers.put(service, serviceWatchers);
        }
        serviceWatchers.put(responseObserver, Boolean.TRUE);
    }
    Context.current().addListener(new CancellationListener() {

        @Override
        public // Called when the client has closed the stream
        void cancelled(Context context) {
            synchronized (watchLock) {
                IdentityHashMap<StreamObserver<HealthCheckResponse>, Boolean> serviceWatchers = watchers.get(service);
                if (serviceWatchers != null) {
                    serviceWatchers.remove(responseObserver);
                    if (serviceWatchers.isEmpty()) {
                        watchers.remove(service);
                    }
                }
            }
        }
    }, MoreExecutors.directExecutor());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Context(io.grpc.Context) ServingStatus(io.grpc.health.v1.HealthCheckResponse.ServingStatus) HealthCheckResponse(io.grpc.health.v1.HealthCheckResponse) CancellationListener(io.grpc.Context.CancellationListener) IdentityHashMap(java.util.IdentityHashMap)

Example 3 with CancellationListener

use of io.grpc.Context.CancellationListener in project grpc-java by grpc.

the class ClientXdsClientV2Test method createAdsService.

@Override
protected BindableService createAdsService() {
    return new AggregatedDiscoveryServiceImplBase() {

        @Override
        public StreamObserver<DiscoveryRequest> streamAggregatedResources(final StreamObserver<DiscoveryResponse> responseObserver) {
            // ensure previous call was ended
            assertThat(adsEnded.get()).isTrue();
            adsEnded.set(false);
            @SuppressWarnings("unchecked") StreamObserver<DiscoveryRequest> requestObserver = mock(StreamObserver.class);
            DiscoveryRpcCall call = new DiscoveryRpcCallV2(requestObserver, responseObserver);
            resourceDiscoveryCalls.offer(call);
            Context.current().addListener(new CancellationListener() {

                @Override
                public void cancelled(Context context) {
                    adsEnded.set(true);
                }
            }, MoreExecutors.directExecutor());
            return requestObserver;
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Context(io.grpc.Context) CommonTlsContext(io.envoyproxy.envoy.api.v2.auth.CommonTlsContext) UpstreamTlsContext(io.envoyproxy.envoy.api.v2.auth.UpstreamTlsContext) CancellationListener(io.grpc.Context.CancellationListener) DiscoveryRequest(io.envoyproxy.envoy.api.v2.DiscoveryRequest) AggregatedDiscoveryServiceImplBase(io.envoyproxy.envoy.service.discovery.v2.AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase)

Example 4 with CancellationListener

use of io.grpc.Context.CancellationListener in project grpc-java by grpc.

the class ClientXdsClientV2Test method createLrsService.

@Override
protected BindableService createLrsService() {
    return new LoadReportingServiceImplBase() {

        @Override
        public StreamObserver<LoadStatsRequest> streamLoadStats(StreamObserver<LoadStatsResponse> responseObserver) {
            assertThat(lrsEnded.get()).isTrue();
            lrsEnded.set(false);
            @SuppressWarnings("unchecked") StreamObserver<LoadStatsRequest> requestObserver = mock(StreamObserver.class);
            LrsRpcCall call = new LrsRpcCallV2(requestObserver, responseObserver);
            Context.current().addListener(new CancellationListener() {

                @Override
                public void cancelled(Context context) {
                    lrsEnded.set(true);
                }
            }, MoreExecutors.directExecutor());
            loadReportCalls.offer(call);
            return requestObserver;
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Context(io.grpc.Context) CommonTlsContext(io.envoyproxy.envoy.api.v2.auth.CommonTlsContext) UpstreamTlsContext(io.envoyproxy.envoy.api.v2.auth.UpstreamTlsContext) LoadStatsRequest(io.envoyproxy.envoy.service.load_stats.v2.LoadStatsRequest) CancellationListener(io.grpc.Context.CancellationListener) LoadReportingServiceImplBase(io.envoyproxy.envoy.service.load_stats.v2.LoadReportingServiceGrpc.LoadReportingServiceImplBase)

Example 5 with CancellationListener

use of io.grpc.Context.CancellationListener in project grpc-java by grpc.

the class ClientXdsClientV3Test method createAdsService.

@Override
protected BindableService createAdsService() {
    return new AggregatedDiscoveryServiceImplBase() {

        @Override
        public StreamObserver<DiscoveryRequest> streamAggregatedResources(final StreamObserver<DiscoveryResponse> responseObserver) {
            // ensure previous call was ended
            assertThat(adsEnded.get()).isTrue();
            adsEnded.set(false);
            @SuppressWarnings("unchecked") StreamObserver<DiscoveryRequest> requestObserver = mock(StreamObserver.class);
            DiscoveryRpcCall call = new DiscoveryRpcCallV3(requestObserver, responseObserver);
            resourceDiscoveryCalls.offer(call);
            Context.current().addListener(new CancellationListener() {

                @Override
                public void cancelled(Context context) {
                    adsEnded.set(true);
                }
            }, MoreExecutors.directExecutor());
            return requestObserver;
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Context(io.grpc.Context) UpstreamTlsContext(io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext) CertificateValidationContext(io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CertificateValidationContext) CommonTlsContext(io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CommonTlsContext) CancellationListener(io.grpc.Context.CancellationListener) DiscoveryRequest(io.envoyproxy.envoy.service.discovery.v3.DiscoveryRequest) AggregatedDiscoveryServiceImplBase(io.envoyproxy.envoy.service.discovery.v3.AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase)

Aggregations

Context (io.grpc.Context)5 CancellationListener (io.grpc.Context.CancellationListener)5 StreamObserver (io.grpc.stub.StreamObserver)5 CommonTlsContext (io.envoyproxy.envoy.api.v2.auth.CommonTlsContext)2 UpstreamTlsContext (io.envoyproxy.envoy.api.v2.auth.UpstreamTlsContext)2 CertificateValidationContext (io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CertificateValidationContext)2 CommonTlsContext (io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CommonTlsContext)2 UpstreamTlsContext (io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext)2 DiscoveryRequest (io.envoyproxy.envoy.api.v2.DiscoveryRequest)1 AggregatedDiscoveryServiceImplBase (io.envoyproxy.envoy.service.discovery.v2.AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase)1 AggregatedDiscoveryServiceImplBase (io.envoyproxy.envoy.service.discovery.v3.AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase)1 DiscoveryRequest (io.envoyproxy.envoy.service.discovery.v3.DiscoveryRequest)1 LoadReportingServiceImplBase (io.envoyproxy.envoy.service.load_stats.v2.LoadReportingServiceGrpc.LoadReportingServiceImplBase)1 LoadStatsRequest (io.envoyproxy.envoy.service.load_stats.v2.LoadStatsRequest)1 LoadReportingServiceImplBase (io.envoyproxy.envoy.service.load_stats.v3.LoadReportingServiceGrpc.LoadReportingServiceImplBase)1 LoadStatsRequest (io.envoyproxy.envoy.service.load_stats.v3.LoadStatsRequest)1 HealthCheckResponse (io.grpc.health.v1.HealthCheckResponse)1 ServingStatus (io.grpc.health.v1.HealthCheckResponse.ServingStatus)1 IdentityHashMap (java.util.IdentityHashMap)1