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

stickiness: avoid using unsafe #2023

Merged
merged 1 commit into from
Apr 25, 2018
Merged
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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop the ", _"; it's cleaner.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return bp.stickinessMDKey.Load().(string)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be nil. We need , _ otherwise it may panic.

return mdKey
}

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