Skip to content

Commit d7b6435

Browse files
committed
Add notes about synchronization and performance
1 parent d716e2b commit d7b6435

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ Current password: 164092
6363
Future password: 046148
6464
```
6565

66+
## Performance and best practices
67+
68+
One-time password generators are thread-safe and reusable. Generally, applications should treat one-time password generator instances as long-lived resources.
69+
70+
The password-generating methods in a one-time password generator are `synchronized`. In multi-threaded applications that make heavy use of a shared one-time password generator instance, synchronization may become a performance bottleneck. In that case, callers may benefit from using one instance per thread (for example, by using a [`ThreadLocal`](https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html)).
71+
6672
## License and copyright
6773

6874
java-otp is published under the [MIT License](https://opensource.org/licenses/MIT).

src/main/java/com/eatthepath/otp/HmacOneTimePasswordGenerator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
* <p>Generates HMAC-based one-time passwords (HOTP) as specified in
3333
* <a href="https://tools.ietf.org/html/rfc4226">RFC&nbsp;4226</a>.</p>
3434
*
35-
* <p>{@code HmacOneTimePasswordGenerator} instances are thread-safe and may be shared between threads.</p>
35+
* <p>{@code HmacOneTimePasswordGenerator} instances are thread-safe and may be shared between threads. Note that the
36+
* {@link #generateOneTimePassword(Key, long)} method (and its relatives) are {@code synchronized}; in multi-threaded
37+
* applications that make heavy use of a shared {@code HmacOneTimePasswordGenerator} instance, synchronization may
38+
* become a performance bottleneck. In that case, callers may benefit from using one
39+
* {@code HmacOneTimePasswordGenerator} instance per thread (for example, with a {@link ThreadLocal}).</p>
3640
*
3741
* @author <a href="https://github.com/jchambers">Jon Chambers</a>
3842
*/

src/main/java/com/eatthepath/otp/TimeBasedOneTimePasswordGenerator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
* <p>Generates time-based one-time passwords (TOTP) as specified in
3232
* <a href="https://tools.ietf.org/html/rfc6238">RFC&nbsp;6238</a>.</p>
3333
*
34-
* <p>{@code TimeBasedOneTimePasswordGenerator} instances are thread-safe and may be shared between threads.</p>
34+
* <p>{@code TimeBasedOneTimePasswordGenerator} instances are thread-safe and may be shared between threads. Note that
35+
* the {@link #generateOneTimePassword(Key, Instant)} method (and its relatives) are {@code synchronized}; in
36+
* multi-threaded applications that make heavy use of a shared {@code TimeBasedOneTimePasswordGenerator} instance,
37+
* synchronization may become a performance bottleneck. In that case, callers may benefit from using one
38+
* {@code TimeBasedOneTimePasswordGenerator} instance per thread (for example, with a {@link ThreadLocal}).</p>
3539
*
3640
* @author <a href="https://github.com/jchambers">Jon Chambers</a>
3741
*/

0 commit comments

Comments
 (0)