You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

round_robin_loadbalance.go 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package loadbalance
  18. import (
  19. "math"
  20. "sort"
  21. "sync"
  22. "sync/atomic"
  23. getty "github.com/apache/dubbo-getty"
  24. )
  25. var sequence int32
  26. func RoundRobinLoadBalance(sessions *sync.Map, s string) getty.Session {
  27. // collect sync.Map adderToSession
  28. // filter out closed session instance
  29. adderToSession := make(map[string]getty.Session, 0)
  30. // map has no sequence, we should sort it to make sure the sequence is always the same
  31. adders := make([]string, 0)
  32. sessions.Range(func(key, value interface{}) bool {
  33. session := key.(getty.Session)
  34. if session.IsClosed() {
  35. sessions.Delete(key)
  36. } else {
  37. adderToSession[session.RemoteAddr()] = session
  38. adders = append(adders, session.RemoteAddr())
  39. }
  40. return true
  41. })
  42. sort.Strings(adders)
  43. // adderToSession eq 0 means there are no available session
  44. if len(adderToSession) == 0 {
  45. return nil
  46. }
  47. index := getPositiveSequence() % len(adderToSession)
  48. return adderToSession[adders[index]]
  49. }
  50. func getPositiveSequence() int {
  51. for {
  52. current := atomic.LoadInt32(&sequence)
  53. var next int32
  54. if current == math.MaxInt32 {
  55. next = 0
  56. } else {
  57. next = current + 1
  58. }
  59. if atomic.CompareAndSwapInt32(&sequence, current, next) {
  60. return int(current)
  61. }
  62. }
  63. }