Skip to content

Commit

Permalink
Support user-provided gRPC client interceptors.
Browse files Browse the repository at this point in the history
  • Loading branch information
YoEight committed Jul 3, 2023
1 parent 7c4ab7e commit dc708c4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [4.3.0] - 2023-07-03
### Added
- Support user-provided gRPC client interceptors. [EventStoreDB-Client-Java#233](https://github.com/EventStore/EventStoreDB-Client-Java/pull/233)

## [4.2.0] - 2023-04-27
### Fixed
- Do not start discovery process on ABORT gRPC error. [EventStoreDB-Client-Java#219](https://github.com/EventStore/EventStoreDB-Client-Java/pull/219)
Expand Down
2 changes: 1 addition & 1 deletion db-client-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tasks.withType(JavaCompile) {
}

group = 'com.eventstore'
version = '4.2.0'
version = '4.3.0'

java {
withJavadocJar()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.eventstore.dbclient;


import io.grpc.ClientInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
* Utility to create client settings programmatically.
Expand All @@ -24,6 +27,7 @@ public class ConnectionSettingsBuilder {
private long _keepAliveTimeout = Consts.DEFAULT_KEEP_ALIVE_TIMEOUT_IN_MS;
private long _keepAliveInterval = Consts.DEFAULT_KEEP_ALIVE_INTERVAL_IN_MS;
private Long _defaultDeadline = null;
private List<ClientInterceptor> _interceptors = new ArrayList<>();

ConnectionSettingsBuilder() {}

Expand All @@ -45,7 +49,8 @@ public EventStoreDBClientSettings buildConnectionSettings() {
_hosts.toArray(new Endpoint[_hosts.size()]),
_keepAliveTimeout,
_keepAliveInterval,
_defaultDeadline);
_defaultDeadline,
_interceptors);
}

/**
Expand Down Expand Up @@ -168,4 +173,13 @@ public ConnectionSettingsBuilder defaultDeadline(long value) {
this._defaultDeadline = value;
return this;
}

/**
* Register a gRPC interceptor every time a new gRPC channel is created.
* @param interceptor
*/
public ConnectionSettingsBuilder addInterceptor(ClientInterceptor interceptor) {
this._interceptors.add(interceptor);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.eventstore.dbclient;

import io.grpc.ClientInterceptor;

import java.util.List;

/**
* Gathers all the settings related to a gRPC client with an EventStoreDB database.
* <i>EventStoreDBClientSettings}</i> can only be created when parsing a connection string.
Expand Down Expand Up @@ -33,6 +37,7 @@ public class EventStoreDBClientSettings {
private final long keepAliveTimeout;
private final long keepAliveInterval;
private final Long defaultDeadline;
private final List<ClientInterceptor> interceptors;

/**
* If the dns discovery is enabled.
Expand Down Expand Up @@ -135,6 +140,14 @@ public Long getDefaultDeadline() {
return defaultDeadline;
}

/**
* Registered gRPC interceptors.
* @return list of registered gRPC client.
*/
public List<ClientInterceptor> getInterceptors() {
return interceptors;
}

EventStoreDBClientSettings(
boolean dnsDiscover,
int maxDiscoverAttempts,
Expand All @@ -148,7 +161,8 @@ public Long getDefaultDeadline() {
Endpoint[] hosts,
long keepAliveTimeout,
long keepAliveInterval,
Long defaultDeadline
Long defaultDeadline,
List<ClientInterceptor> interceptors
) {
this.dnsDiscover = dnsDiscover;
this.maxDiscoverAttempts = maxDiscoverAttempts;
Expand All @@ -163,6 +177,7 @@ public Long getDefaultDeadline() {
this.keepAliveTimeout = keepAliveTimeout;
this.keepAliveInterval = keepAliveInterval;
this.defaultDeadline = defaultDeadline;
this.interceptors = interceptors;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.eventstore.dbclient;

import io.grpc.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import testcontainers.module.ESDBTests;

import java.util.concurrent.atomic.AtomicInteger;

public class InterceptorTests extends ESDBTests {
@Test
public void testInterceptorIsCalled() {
EventStoreDBClientSettings settings = getEmptyServer().getSettings();

AtomicInteger atom = new AtomicInteger(0);

settings.getInterceptors().add(new MyInterceptor(atom));

EventStoreDBClient client = EventStoreDBClient.create(settings);
try {
client.readStream("foobar", ReadStreamOptions.get()).get();
} catch (Exception e) {
// We don't care.
}

Assertions.assertEquals(42, atom.get());
}

class MyInterceptor implements ClientInterceptor {
final AtomicInteger atom;

MyInterceptor(AtomicInteger atom) {
this.atom = atom;
}
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
atom.set(42);
return next.newCall(method, callOptions);
}
}
}

0 comments on commit dc708c4

Please sign in to comment.