Skip to content

Commit c2565ca

Browse files
committed
Use "update" instead of "refresh" when updating connection token
1 parent 838caaf commit c2565ca

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

src/main/java/com/rabbitmq/client/amqp/impl/Credentials.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,26 @@ interface Credentials {
2121

2222
Credentials NO_OP = new NoOpCredentials();
2323

24-
Registration register(String name, AuthenticationCallback refreshCallback);
24+
Registration register(String name, AuthenticationCallback updateCallback);
2525

2626
interface Registration {
2727

2828
void connect(AuthenticationCallback callback);
2929

3030
void unregister();
31+
3132
}
3233

3334
interface AuthenticationCallback {
3435

3536
void authenticate(String username, String password);
37+
3638
}
3739

3840
class NoOpCredentials implements Credentials {
3941

4042
@Override
41-
public Registration register(String name, AuthenticationCallback refreshCallback) {
43+
public Registration register(String name, AuthenticationCallback updateCallback) {
4244
return new NoOpRegistration();
4345
}
4446
}

src/main/java/com/rabbitmq/client/amqp/impl/TokenCredentials.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ final class TokenCredentials implements Credentials {
4747
private final Lock lock = new ReentrantLock();
4848
private final Map<Long, RegistrationImpl> registrations = new ConcurrentHashMap<>();
4949
private final AtomicLong registrationSequence = new AtomicLong(0);
50-
private final AtomicBoolean schedulingRenewal = new AtomicBoolean(false);
50+
private final AtomicBoolean schedulingRefresh = new AtomicBoolean(false);
5151
private final Function<Instant, Duration> refreshDelayStrategy;
52-
private volatile ScheduledFuture<?> renewalTask;
52+
private volatile ScheduledFuture<?> refreshTask;
5353

5454
TokenCredentials(TokenRequester requester, ScheduledExecutorService scheduledExecutorService) {
5555
this.requester = requester;
@@ -83,28 +83,28 @@ private Token getToken() {
8383
}
8484

8585
@Override
86-
public Registration register(String name, AuthenticationCallback refreshCallback) {
86+
public Registration register(String name, AuthenticationCallback updateCallback) {
8787
Long id = this.registrationSequence.getAndIncrement();
8888
name = name == null ? id.toString() : name;
89-
RegistrationImpl registration = new RegistrationImpl(id, name, refreshCallback);
89+
RegistrationImpl registration = new RegistrationImpl(id, name, updateCallback);
9090
this.registrations.put(id, registration);
9191
return registration;
9292
}
9393

94-
private void refreshRegistrations(Token t) {
94+
private void updateRegistrations(Token t) {
9595
this.scheduledExecutorService.execute(
9696
() -> {
97-
LOGGER.debug("Refreshing {} registration(s)", this.registrations.size());
97+
LOGGER.debug("Updating {} registration(s)", this.registrations.size());
9898
int refreshedCount = 0;
9999
for (RegistrationImpl registration : this.registrations.values()) {
100100
if (t.equals(this.token)) {
101101
if (!registration.isClosed() && !registration.hasSameToken(t)) {
102102
// the registration does not have the new token yet
103103
try {
104-
registration.refreshCallback().authenticate("", this.token.value());
104+
registration.updateCallback().authenticate("", this.token.value());
105105
} catch (Exception e) {
106106
LOGGER.warn(
107-
"Error while refreshing token for registration '{}': {}",
107+
"Error while updating token for registration '{}': {}",
108108
registration.name(),
109109
e.getMessage());
110110
}
@@ -113,7 +113,7 @@ private void refreshRegistrations(Token t) {
113113
}
114114
}
115115
}
116-
LOGGER.debug("Refreshed {} registration(s)", refreshedCount);
116+
LOGGER.debug("Updated {} registration(s)", refreshedCount);
117117
});
118118
}
119119

@@ -122,22 +122,22 @@ private void token(Token t) {
122122
try {
123123
if (!t.equals(this.token)) {
124124
this.token = t;
125-
scheduleRenewal(t);
125+
scheduleTokenRefresh(t);
126126
}
127127
} finally {
128128
unlock();
129129
}
130130
}
131131

132-
private void scheduleRenewal(Token t) {
133-
if (this.schedulingRenewal.compareAndSet(false, true)) {
134-
if (this.renewalTask != null) {
135-
this.renewalTask.cancel(false);
132+
private void scheduleTokenRefresh(Token t) {
133+
if (this.schedulingRefresh.compareAndSet(false, true)) {
134+
if (this.refreshTask != null) {
135+
this.refreshTask.cancel(false);
136136
}
137137
Duration delay = this.refreshDelayStrategy.apply(t.expirationTime());
138138
if (!this.registrations.isEmpty()) {
139139
LOGGER.debug("Scheduling token retrieval in {}", delay);
140-
this.renewalTask =
140+
this.refreshTask =
141141
this.scheduledExecutorService.schedule(
142142
() -> {
143143
Token previousToken = this.token;
@@ -146,7 +146,7 @@ private void scheduleRenewal(Token t) {
146146
if (this.token.equals(previousToken)) {
147147
Token newToken = getToken();
148148
token(newToken);
149-
refreshRegistrations(newToken);
149+
updateRegistrations(newToken);
150150
}
151151
} finally {
152152
unlock();
@@ -155,9 +155,9 @@ private void scheduleRenewal(Token t) {
155155
delay.toMillis(),
156156
TimeUnit.MILLISECONDS);
157157
} else {
158-
this.renewalTask = null;
158+
this.refreshTask = null;
159159
}
160-
this.schedulingRenewal.set(false);
160+
this.schedulingRefresh.set(false);
161161
}
162162
}
163163

@@ -169,14 +169,14 @@ private final class RegistrationImpl implements Registration {
169169

170170
private final Long id;
171171
private final String name;
172-
private final AuthenticationCallback refreshCallback;
172+
private final AuthenticationCallback updateCallback;
173173
private volatile Token registrationToken;
174174
private final AtomicBoolean closed = new AtomicBoolean(false);
175175

176-
private RegistrationImpl(Long id, String name, AuthenticationCallback refreshCallback) {
176+
private RegistrationImpl(Long id, String name, AuthenticationCallback updateCallback) {
177177
this.id = id;
178178
this.name = name;
179-
this.refreshCallback = refreshCallback;
179+
this.updateCallback = updateCallback;
180180
}
181181

182182
@Override
@@ -185,37 +185,39 @@ public void connect(AuthenticationCallback callback) {
185185
Token tokenToUse;
186186
lock();
187187
try {
188-
if (token == null) {
188+
Token globalToken = token;
189+
if (globalToken == null) {
189190
token(getToken());
190-
} else if (expiresSoon(token)) {
191+
} else if (expiresSoon(globalToken)) {
191192
shouldRefresh = true;
192193
token(getToken());
193194
}
194-
this.registrationToken = token;
195+
if (!token.equals(this.registrationToken)) {
196+
this.registrationToken = token;
197+
}
195198
tokenToUse = this.registrationToken;
196-
if (renewalTask == null) {
197-
scheduleRenewal(tokenToUse);
199+
if (refreshTask == null) {
200+
scheduleTokenRefresh(tokenToUse);
198201
}
199202
} finally {
200203
unlock();
201204
}
202-
203205
callback.authenticate("", tokenToUse.value());
204206
if (shouldRefresh) {
205-
refreshRegistrations(tokenToUse);
207+
updateRegistrations(tokenToUse);
206208
}
207209
}
208210

209211
@Override
210212
public void unregister() {
211213
if (this.closed.compareAndSet(false, true)) {
212214
registrations.remove(this.id);
213-
ScheduledFuture<?> task = renewalTask;
215+
ScheduledFuture<?> task = refreshTask;
214216
if (registrations.isEmpty() && task != null) {
215217
lock();
216218
try {
217-
if (renewalTask != null) {
218-
renewalTask.cancel(false);
219+
if (refreshTask != null) {
220+
refreshTask.cancel(false);
219221
}
220222
} finally {
221223
unlock();
@@ -224,8 +226,8 @@ public void unregister() {
224226
}
225227
}
226228

227-
private AuthenticationCallback refreshCallback() {
228-
return this.refreshCallback;
229+
private AuthenticationCallback updateCallback() {
230+
return this.updateCallback;
229231
}
230232

231233
private String name() {

src/main/java/com/rabbitmq/client/amqp/impl/UsernamePasswordCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class UsernamePasswordCredentials implements Credentials {
3030
}
3131

3232
@Override
33-
public Registration register(String name, AuthenticationCallback refreshCallback) {
33+
public Registration register(String name, AuthenticationCallback updateCallback) {
3434
return this.registration;
3535
}
3636

0 commit comments

Comments
 (0)