-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1153 from ydb-platform/last-usage
added lastUsage locked time
- Loading branch information
Showing
5 changed files
with
160 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package conn | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/jonboulle/clockwork" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync" | ||
) | ||
|
||
type lastUsage struct { | ||
locks atomic.Int64 | ||
mu xsync.RWMutex | ||
t time.Time | ||
clock clockwork.Clock | ||
} | ||
|
||
func (l *lastUsage) Get() time.Time { | ||
if l.locks.CompareAndSwap(0, 1) { | ||
defer func() { | ||
l.locks.Add(-1) | ||
}() | ||
|
||
l.mu.RLock() | ||
defer l.mu.RUnlock() | ||
|
||
return l.t | ||
} | ||
|
||
return l.clock.Now() | ||
} | ||
|
||
func (l *lastUsage) Lock() (releaseFunc func()) { | ||
l.locks.Add(1) | ||
|
||
return sync.OnceFunc(func() { | ||
if l.locks.Add(-1) == 0 { | ||
l.mu.WithLock(func() { | ||
l.t = l.clock.Now() | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package conn | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/jonboulle/clockwork" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_lastUsage_Lock(t *testing.T) { | ||
t.Run("NowFromLocked", func(t *testing.T) { | ||
start := time.Unix(0, 0) | ||
clock := clockwork.NewFakeClockAt(start) | ||
lu := &lastUsage{ | ||
t: start, | ||
clock: clock, | ||
} | ||
t1 := lu.Get() | ||
require.Equal(t, start, t1) | ||
f := lu.Lock() | ||
clock.Advance(time.Hour) | ||
t2 := lu.Get() | ||
require.Equal(t, start.Add(time.Hour), t2) | ||
clock.Advance(time.Hour) | ||
f() | ||
t3 := lu.Get() | ||
require.Equal(t, start.Add(2*time.Hour), t3) | ||
clock.Advance(time.Hour) | ||
t4 := lu.Get() | ||
require.Equal(t, start.Add(2*time.Hour), t4) | ||
}) | ||
t.Run("UpdateAfterLastUnlock", func(t *testing.T) { | ||
start := time.Unix(0, 0) | ||
clock := clockwork.NewFakeClockAt(start) | ||
lu := &lastUsage{ | ||
t: start, | ||
clock: clock, | ||
} | ||
t1 := lu.Get() | ||
require.Equal(t, start, t1) | ||
f1 := lu.Lock() | ||
clock.Advance(time.Hour) | ||
t2 := lu.Get() | ||
require.Equal(t, start.Add(time.Hour), t2) | ||
f2 := lu.Lock() | ||
clock.Advance(time.Hour) | ||
f1() | ||
f3 := lu.Lock() | ||
clock.Advance(time.Hour) | ||
t3 := lu.Get() | ||
require.Equal(t, start.Add(3*time.Hour), t3) | ||
clock.Advance(time.Hour) | ||
t4 := lu.Get() | ||
require.Equal(t, start.Add(4*time.Hour), t4) | ||
f3() | ||
t5 := lu.Get() | ||
require.Equal(t, start.Add(4*time.Hour), t5) | ||
clock.Advance(time.Hour) | ||
t6 := lu.Get() | ||
require.Equal(t, start.Add(5*time.Hour), t6) | ||
clock.Advance(time.Hour) | ||
f2() | ||
t7 := lu.Get() | ||
require.Equal(t, start.Add(6*time.Hour), t7) | ||
clock.Advance(time.Hour) | ||
f2() | ||
t8 := lu.Get() | ||
require.Equal(t, start.Add(6*time.Hour), t8) | ||
}) | ||
t.Run("DeferRelease", func(t *testing.T) { | ||
start := time.Unix(0, 0) | ||
clock := clockwork.NewFakeClockAt(start) | ||
lu := &lastUsage{ | ||
t: start, | ||
clock: clock, | ||
} | ||
func() { | ||
t1 := lu.Get() | ||
require.Equal(t, start, t1) | ||
clock.Advance(time.Hour) | ||
t2 := lu.Get() | ||
require.Equal(t, start, t2) | ||
clock.Advance(time.Hour) | ||
defer lu.Lock()() | ||
t3 := lu.Get() | ||
require.Equal(t, start.Add(2*time.Hour), t3) | ||
clock.Advance(time.Hour) | ||
t4 := lu.Get() | ||
require.Equal(t, start.Add(3*time.Hour), t4) | ||
clock.Advance(time.Hour) | ||
}() | ||
clock.Advance(time.Hour) | ||
t5 := lu.Get() | ||
require.Equal(t, start.Add(4*time.Hour), t5) | ||
}) | ||
} |