Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop mutext at txnTime #145

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 2 additions & 43 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import (
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/fauna/fauna-go/internal/fingerprinting"
Expand Down Expand Up @@ -332,20 +329,12 @@ func (q *QueryIterator) HasNext() bool {
// WARNING: This should be used only when coordinating timestamps across multiple clients.
// Moving the timestamp arbitrarily forward into the future will cause transactions to stall.
func (c *Client) SetLastTxnTime(txnTime time.Time) {
c.lastTxnTime.Lock()
defer c.lastTxnTime.Unlock()

if val := txnTime.UnixMicro(); val > c.lastTxnTime.Value {
c.lastTxnTime.Value = val
}
c.lastTxnTime.sync(txnTime.UnixMicro())
}

// GetLastTxnTime gets the last txn timestamp seen by the [fauna.Client]
func (c *Client) GetLastTxnTime() int64 {
c.lastTxnTime.RLock()
defer c.lastTxnTime.RUnlock()

return c.lastTxnTime.Value
return c.lastTxnTime.get()
}

// String fulfil Stringify interface for the [fauna.Client]
Expand All @@ -357,33 +346,3 @@ func (c *Client) String() string {
func (c *Client) setHeader(key, val string) {
c.headers[key] = val
}

type txnTime struct {
sync.RWMutex

Value int64
}

func (t *txnTime) string() string {
t.RLock()
defer t.RUnlock()

if lastSeen := atomic.LoadInt64(&t.Value); lastSeen != 0 {
return strconv.FormatInt(lastSeen, 10)
}

return ""
}

func (t *txnTime) sync(newTxnTime int64) {
t.Lock()
defer t.Unlock()

for {
oldTxnTime := atomic.LoadInt64(&t.Value)
if oldTxnTime >= newTxnTime ||
atomic.CompareAndSwapInt64(&t.Value, oldTxnTime, newTxnTime) {
break
}
}
}
31 changes: 31 additions & 0 deletions txntime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fauna

import (
"strconv"
"sync/atomic"
)

type txnTime struct {
value atomic.Int64
}

func (t *txnTime) get() int64 {
return t.value.Load()
}

func (t *txnTime) sync(newTxnTime int64) {
for {
oldTxnTime := t.value.Load()
if oldTxnTime >= newTxnTime ||
t.value.CompareAndSwap(oldTxnTime, newTxnTime) {
break
}
}
}

func (t *txnTime) string() (str string) {
if lastSeen := t.value.Load(); lastSeen != 0 {
str = strconv.FormatInt(lastSeen, 10)
}
return
}
32 changes: 32 additions & 0 deletions txntime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fauna

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestTxnTime(t *testing.T) {
txnTime := txnTime{}
require.Equal(t, int64(0), txnTime.get())
require.Equal(t, "", txnTime.string())

txnTime.sync(42) // move forward
require.Equal(t, int64(42), txnTime.get())
require.Equal(t, "42", txnTime.string())

txnTime.sync(32) // don't move back
require.Equal(t, int64(42), txnTime.get())
require.Equal(t, "42", txnTime.string())
}

func BenchmarkTxnTime(b *testing.B) {
txnTime := txnTime{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
now := time.Now()
txnTime.sync(now.UnixMicro())
}
})
}
Loading