Skip to content

Commit

Permalink
Revert "stickiness: avoid using unsafe (grpc#2023)"
Browse files Browse the repository at this point in the history
This reverts commit 7263089.
  • Loading branch information
menghanl committed Jun 25, 2018
1 parent 25717d2 commit b4a3144
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions picker_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"sync"
"sync/atomic"
"unsafe"

"golang.org/x/net/context"
"google.golang.org/grpc/balancer"
Expand All @@ -46,7 +47,7 @@ type pickerWrapper struct {
connErrMu sync.Mutex
connErr error

stickinessMDKey atomic.Value
stickinessMDKey unsafe.Pointer
stickiness *stickyStore
}

Expand All @@ -72,17 +73,18 @@ func (bp *pickerWrapper) connectionError() error {
}

func (bp *pickerWrapper) updateStickinessMDKey(newKey string) {
// No need to check ok because mdKey == "" if ok == false.
if oldKey, _ := bp.stickinessMDKey.Load().(string); oldKey != newKey {
bp.stickinessMDKey.Store(newKey)
oldKeyUnsafePtr := atomic.SwapPointer(&bp.stickinessMDKey, unsafe.Pointer(&newKey))
if ptr := (*string)(oldKeyUnsafePtr); ptr == nil || *ptr != newKey {
bp.stickiness.reset(newKey)
}
}

func (bp *pickerWrapper) getStickinessMDKey() string {
// No need to check ok because mdKey == "" if ok == false.
mdKey, _ := bp.stickinessMDKey.Load().(string)
return mdKey
mdKeyPtr := (*string)(atomic.LoadPointer(&bp.stickinessMDKey))
if mdKeyPtr != nil {
return *mdKeyPtr
}
return ""
}

func (bp *pickerWrapper) clearStickinessState() {
Expand Down

0 comments on commit b4a3144

Please sign in to comment.