Skip to content

Commit 6e51a12

Browse files
authored
Merge pull request #1193 from ydb-platform/last-usage
moved internal/conn.lastUsage into internal/xsync package
2 parents 0d257a1 + 85a1d34 commit 6e51a12

File tree

4 files changed

+67
-51
lines changed

4 files changed

+67
-51
lines changed

internal/conn/conn.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
2020
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
2121
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
22+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
2223
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
2324
)
2425

@@ -55,8 +56,8 @@ type conn struct {
5556
endpoint endpoint.Endpoint // ro access
5657
closed bool
5758
state atomic.Uint32
58-
lastUsage *lastUsage
5959
childStreams *xcontext.CancelsGuard
60+
lastUsage xsync.LastUsage
6061
onClose []func(*conn)
6162
onTransportErrors []func(ctx context.Context, cc Conn, cause error)
6263
}
@@ -504,7 +505,7 @@ func newConn(e endpoint.Endpoint, config Config, opts ...option) *conn {
504505
endpoint: e,
505506
config: config,
506507
done: make(chan struct{}),
507-
lastUsage: newLastUsage(nil),
508+
lastUsage: xsync.NewLastUsage(),
508509
childStreams: xcontext.NewCancelsGuard(),
509510
onClose: []func(*conn){
510511
func(c *conn) {

internal/conn/last_usage.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

internal/xsync/last_usage_guard.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package xsync
2+
3+
import (
4+
"sync"
5+
"sync/atomic"
6+
"time"
7+
8+
"github.com/jonboulle/clockwork"
9+
)
10+
11+
type (
12+
LastUsage interface {
13+
Get() time.Time
14+
Start() (stop func())
15+
}
16+
lastUsage struct {
17+
locks atomic.Int64
18+
t atomic.Pointer[time.Time]
19+
clock clockwork.Clock
20+
}
21+
lastUsageOption func(g *lastUsage)
22+
)
23+
24+
func WithClock(clock clockwork.Clock) lastUsageOption {
25+
return func(g *lastUsage) {
26+
g.clock = clock
27+
}
28+
}
29+
30+
func NewLastUsage(opts ...lastUsageOption) *lastUsage {
31+
lastUsage := &lastUsage{
32+
clock: clockwork.NewRealClock(),
33+
}
34+
for _, opt := range opts {
35+
opt(lastUsage)
36+
}
37+
38+
now := lastUsage.clock.Now()
39+
40+
lastUsage.t.Store(&now)
41+
42+
return lastUsage
43+
}
44+
45+
func (g *lastUsage) Get() time.Time {
46+
if g.locks.Load() == 0 {
47+
return *g.t.Load()
48+
}
49+
50+
return g.clock.Now()
51+
}
52+
53+
func (g *lastUsage) Start() (stop func()) {
54+
g.locks.Add(1)
55+
56+
return sync.OnceFunc(func() {
57+
if g.locks.Add(-1) == 0 {
58+
now := g.clock.Now()
59+
g.t.Store(&now)
60+
}
61+
})
62+
}

internal/conn/last_usage_test.go renamed to internal/xsync/last_usage_guard_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package conn
1+
package xsync
22

33
import (
44
"testing"
@@ -8,7 +8,7 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11-
func Test_lastUsage_Lock(t *testing.T) {
11+
func TestLastUsageGuardLock(t *testing.T) {
1212
t.Run("NowFromLocked", func(t *testing.T) {
1313
start := time.Unix(0, 0)
1414
clock := clockwork.NewFakeClockAt(start)

0 commit comments

Comments
 (0)