Skip to content

Commit

Permalink
stickiness: avoid using unsafe (grpc#2023)
Browse files Browse the repository at this point in the history
App Engine doesn't allow use of unsafe.
  • Loading branch information
menghanl committed Apr 25, 2018
1 parent 247a5d4 commit 7263089
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions picker_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io"
"sync"
"sync/atomic"
"unsafe"

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

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

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

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

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

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

0 comments on commit 7263089

Please sign in to comment.