xuzifu666 commented on code in PR #995:
URL:
https://github.com/apache/incubator-seata-go/pull/995#discussion_r2556302983
##########
pkg/remoting/loadbalance/round_robin_loadbalance.go:
##########
@@ -28,29 +28,129 @@ import (
var sequence int32
+type rrSnapshot struct {
+ sessions []getty.Session
+}
+
+type rrSelector struct {
+ sessions *sync.Map
+ snapshot atomic.Value
+ mu sync.Mutex
+}
+
+// RoundRobinLoadBalance selects a session using round-robin algorithm
func RoundRobinLoadBalance(sessions *sync.Map, s string) getty.Session {
- // collect sync.Map adderToSession
- // filter out closed session instance
- adderToSession := make(map[string]getty.Session, 0)
- // map has no sequence, we should sort it to make sure the sequence is
always the same
- adders := make([]string, 0)
- sessions.Range(func(key, value interface{}) bool {
+ // create selector directly without caching to avoid pointer-based
cache issues
+ selector := &rrSelector{sessions: sessions}
+ selector.snapshot.Store((*rrSnapshot)(nil))
+
+ seq := getPositiveSequence()
+ return selector.selectWithSeq(seq)
+}
+
+func (r *rrSelector) getValidSnapshot() *rrSnapshot {
+ v := r.snapshot.Load()
+ if v == nil {
+ return nil
+ }
+ snap := v.(*rrSnapshot)
+ if snap == nil || len(snap.sessions) == 0 {
+ return nil
+ }
+ return snap
+}
+
+func (r *rrSelector) selectWithSeq(seq int) getty.Session {
+ const maxRetries = 3
Review Comment:
+1 for the changes of PR
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]