26
26
import java .net .UnknownHostException ;
27
27
import java .time .Duration ;
28
28
import java .time .Instant ;
29
- import java .time .temporal .ChronoUnit ;
30
29
import java .util .Optional ;
31
30
32
31
/**
@@ -40,38 +39,26 @@ public class JedisLockProvider implements LockProvider {
40
39
private static final String KEY_PREFIX = "job-lock" ;
41
40
private static final String ENV_DEFAULT = "default" ;
42
41
43
- private static final long AT_MOST_HOURS_DEFAULT = 1 ;
44
-
45
42
// Redis Flags
46
43
private static final String SET_IF_NOT_EXIST = "NX" ;
44
+ private static final String SET_IF_EXIST = "XX" ;
47
45
private static final String SET_EXPIRE_TIME_IN_MS = "PX" ;
48
46
49
47
private JedisPool jedisPool ;
50
48
private String environment ;
51
- private Duration atMostHoursDefault ;
52
49
53
50
public JedisLockProvider (JedisPool jedisPool ) {
54
- this (jedisPool , ENV_DEFAULT , AT_MOST_HOURS_DEFAULT );
51
+ this (jedisPool , ENV_DEFAULT );
55
52
}
56
53
57
54
public JedisLockProvider (JedisPool jedisPool , String environment ) {
58
- this (jedisPool , environment , AT_MOST_HOURS_DEFAULT );
59
- }
60
-
61
- public JedisLockProvider (JedisPool jedisPool , String environment , long atMostHoursDefault ) {
62
55
this .jedisPool = jedisPool ;
63
56
this .environment = environment ;
64
- this .atMostHoursDefault = Duration .of (atMostHoursDefault , ChronoUnit .HOURS );
65
57
}
66
58
67
59
@ Override
68
60
public Optional <SimpleLock > lock (LockConfiguration lockConfiguration ) {
69
- Instant now = Instant .now ();
70
-
71
- // Get 'furthest out' configured expire time for lock TTL
72
- long expireTime = Math .max (
73
- Duration .between (now , lockConfiguration .getLockAtMostUntil ()).toMillis (),
74
- Duration .between (now , lockConfiguration .getLockAtLeastUntil ()).toMillis ());
61
+ long expireTime = getMsUntil (lockConfiguration .getLockAtMostUntil ());
75
62
76
63
String key = buildKey (lockConfiguration .getName (), this .environment );
77
64
@@ -80,7 +67,7 @@ public Optional<SimpleLock> lock(LockConfiguration lockConfiguration) {
80
67
buildValue (),
81
68
SET_IF_NOT_EXIST ,
82
69
SET_EXPIRE_TIME_IN_MS ,
83
- expireTime < 0 ? atMostHoursDefault . toMillis () : expireTime ); // And if not set, use default
70
+ expireTime );
84
71
85
72
if (rez != null && "OK" .equals (rez )) {
86
73
return Optional .of (new RedisLock (key , jedisPool , lockConfiguration ));
@@ -102,32 +89,44 @@ private RedisLock(String key, JedisPool jedisPool, LockConfiguration lockConfigu
102
89
103
90
@ Override
104
91
public void unlock () {
105
- Instant now = Instant .now ();
106
- Instant atLeastUntil = lockConfiguration .getLockAtLeastUntil ();
92
+ long keepLockFor = getMsUntil (lockConfiguration .getLockAtLeastUntil ());
107
93
108
- if (now .equals (atLeastUntil ) || now .isAfter (atLeastUntil )) {
94
+ // lock at least until is in the past
95
+ if (keepLockFor <= 0 ) {
109
96
try (Jedis jedis = jedisPool .getResource ()) {
110
97
jedis .del (key );
111
98
} catch (Exception e ) {
112
99
throw new LockException ("Can not remove node" , e );
113
100
}
101
+ } else {
102
+ try (Jedis jedis = jedisPool .getResource ()) {
103
+ jedis .set (key ,
104
+ buildValue (),
105
+ SET_IF_EXIST ,
106
+ SET_EXPIRE_TIME_IN_MS ,
107
+ keepLockFor );
108
+ }
114
109
}
115
110
}
116
111
}
117
112
118
- protected static String getHostname () {
113
+ private static long getMsUntil (Instant instant ) {
114
+ return Duration .between (Instant .now (), instant ).toMillis ();
115
+ }
116
+
117
+ private static String getHostname () {
119
118
try {
120
119
return InetAddress .getLocalHost ().getHostName ();
121
120
} catch (UnknownHostException e ) {
122
121
return "unknown host" ;
123
122
}
124
123
}
125
124
126
- public static String buildKey (String lockName , String env ) {
125
+ static String buildKey (String lockName , String env ) {
127
126
return String .format ("%s:%s:%s" , KEY_PREFIX , env , lockName );
128
127
}
129
128
130
- static String buildValue () {
129
+ private static String buildValue () {
131
130
return String .format ("ADDED:%s@%s" , Instant .now ().toString (),getHostname ());
132
131
}
133
132
}
0 commit comments