Skip to content

Commit

Permalink
Optimize lease get state for proxy (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuangTung97 authored Oct 9, 2023
1 parent 1080d96 commit 74273eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
24 changes: 24 additions & 0 deletions proxy/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package proxy

import (
"sync"
)

// ===============================
// pool of lease get state
// ===============================

var leaseGetStatePool = sync.Pool{
New: func() any {
return &leaseGetState{}
},
}

func putLeaseGetState(s *leaseGetState) {
*s = leaseGetState{}
leaseGetStatePool.Put(s)
}

func getLeaseGetState() *leaseGetState {
return leaseGetStatePool.Get().(*leaseGetState)
}
20 changes: 20 additions & 0 deletions proxy/pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package proxy

import (
"testing"

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

func TestLeaseGetStatePool(t *testing.T) {
t.Run("normal", func(t *testing.T) {
s := getLeaseGetState()
assert.Equal(t, &leaseGetState{}, s)

s.serverID = 123

putLeaseGetState(s)
s = getLeaseGetState()
assert.Equal(t, &leaseGetState{}, s)
})
}
10 changes: 8 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ func (s *leaseGetState) nextFunc() {
func (s *leaseGetState) Result() (memproxy.LeaseGetResponse, error) {
s.pipe.sess.Execute()
s.pipe.selector.Reset()
return s.resp, s.err

resp, err := s.resp, s.err

putLeaseGetState(s)

return resp, err
}

// LeaseGet ...
Expand All @@ -251,7 +256,8 @@ func (p *Pipeline) LeaseGet(
pipe := p.getRoutePipeline(serverID)
fn := pipe.LeaseGet(key, options)

state := &leaseGetState{
state := getLeaseGetState()
*state = leaseGetState{
pipe: p,
serverID: serverID,
key: key,
Expand Down

0 comments on commit 74273eb

Please sign in to comment.