Skip to content

Commit

Permalink
clientv3: fix isOptsWithFromKey/isOptsWithPrefix
Browse files Browse the repository at this point in the history
- Addressing: etcd-io#13332
- Backporting: etcd-io#13334

Signed-off-by: vivekpatani <9080894+vivekpatani@users.noreply.github.com>
  • Loading branch information
vivekpatani committed Jul 20, 2022
1 parent d58a0c0 commit 4fef7fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
27 changes: 25 additions & 2 deletions clientv3/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type Op struct {
cmps []Cmp
thenOps []Op
elseOps []Op

isOptsWithFromKey bool
isOptsWithPrefix bool
}

// accessors / mutators
Expand Down Expand Up @@ -216,6 +219,10 @@ func (op Op) isWrite() bool {
return op.t != tRange
}

func NewOp() *Op {
return &Op{key: []byte("")}
}

// OpGet returns "get" operation based on given key and operation options.
func OpGet(key string, opts ...OpOption) Op {
// WithPrefix and WithFromKey are not supported together
Expand Down Expand Up @@ -387,6 +394,7 @@ func WithPrefix() OpOption {
return
}
op.end = getPrefix(op.key)
op.isOptsWithPrefix = true
}
}

Expand All @@ -406,6 +414,7 @@ func WithFromKey() OpOption {
op.key = []byte{0}
}
op.end = []byte("\x00")
op.isOptsWithFromKey = true
}
}

Expand Down Expand Up @@ -554,7 +563,21 @@ func toLeaseTimeToLiveRequest(id LeaseID, opts ...LeaseOption) *pb.LeaseTimeToLi
}

// isWithPrefix returns true if WithPrefix is being called in the op
func isWithPrefix(opts []OpOption) bool { return isOpFuncCalled("WithPrefix", opts) }
func isWithPrefix(opts []OpOption) bool {
ret := NewOp()
for _, opt := range opts {
opt(ret)
}

return ret.isOptsWithPrefix
}

// isWithFromKey returns true if WithFromKey is being called in the op
func isWithFromKey(opts []OpOption) bool { return isOpFuncCalled("WithFromKey", opts) }
func isWithFromKey(opts []OpOption) bool {
ret := NewOp()
for _, opt := range opts {
opt(ret)
}

return ret.isOptsWithFromKey
}
18 changes: 0 additions & 18 deletions clientv3/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ package clientv3

import (
"math/rand"
"reflect"
"runtime"
"strings"
"time"
)

Expand All @@ -32,18 +29,3 @@ func jitterUp(duration time.Duration, jitter float64) time.Duration {
multiplier := jitter * (rand.Float64()*2 - 1)
return time.Duration(float64(duration) * (1 + multiplier))
}

// Check if the provided function is being called in the op options.
func isOpFuncCalled(op string, opts []OpOption) bool {
for _, opt := range opts {
v := reflect.ValueOf(opt)
if v.Kind() == reflect.Func {
if opFunc := runtime.FuncForPC(v.Pointer()); opFunc != nil {
if strings.Contains(opFunc.Name(), op) {
return true
}
}
}
}
return false
}

0 comments on commit 4fef7fc

Please sign in to comment.