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.

session_manager.go 3.1 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 getty
  18. import (
  19. "sync"
  20. "sync/atomic"
  21. "time"
  22. getty "github.com/apache/dubbo-getty"
  23. )
  24. var (
  25. MAX_CHECK_ALIVE_RETRY = 600
  26. CHECK_ALIVE_INTERNAL = 100
  27. allSessions = sync.Map{}
  28. // serverAddress -> rpc_client.Session -> bool
  29. serverSessions = sync.Map{}
  30. sessionSize int32 = 0
  31. sessionManager = &GettySessionManager{}
  32. )
  33. type GettySessionManager struct{}
  34. func (sessionManager *GettySessionManager) AcquireGettySession() getty.Session {
  35. // map 遍历是随机的
  36. var session getty.Session
  37. allSessions.Range(func(key, value interface{}) bool {
  38. session = key.(getty.Session)
  39. if session.IsClosed() {
  40. sessionManager.ReleaseGettySession(session)
  41. } else {
  42. return false
  43. }
  44. return true
  45. })
  46. if session != nil {
  47. return session
  48. }
  49. if sessionSize == 0 {
  50. ticker := time.NewTicker(time.Duration(CHECK_ALIVE_INTERNAL) * time.Millisecond)
  51. defer ticker.Stop()
  52. for i := 0; i < MAX_CHECK_ALIVE_RETRY; i++ {
  53. <-ticker.C
  54. allSessions.Range(func(key, value interface{}) bool {
  55. session = key.(getty.Session)
  56. if session.IsClosed() {
  57. sessionManager.ReleaseGettySession(session)
  58. } else {
  59. return false
  60. }
  61. return true
  62. })
  63. if session != nil {
  64. return session
  65. }
  66. }
  67. }
  68. return nil
  69. }
  70. func (sessionManager *GettySessionManager) AcquireGettySessionByServerAddress(serverAddress string) getty.Session {
  71. m, _ := serverSessions.LoadOrStore(serverAddress, &sync.Map{})
  72. sMap := m.(*sync.Map)
  73. var session getty.Session
  74. sMap.Range(func(key, value interface{}) bool {
  75. session = key.(getty.Session)
  76. if session.IsClosed() {
  77. sessionManager.ReleaseGettySession(session)
  78. } else {
  79. return false
  80. }
  81. return true
  82. })
  83. return session
  84. }
  85. func (sessionManager *GettySessionManager) ReleaseGettySession(session getty.Session) {
  86. allSessions.Delete(session)
  87. if !session.IsClosed() {
  88. m, _ := serverSessions.LoadOrStore(session.RemoteAddr(), &sync.Map{})
  89. sMap := m.(*sync.Map)
  90. sMap.Delete(session)
  91. session.Close()
  92. }
  93. atomic.AddInt32(&sessionSize, -1)
  94. }
  95. func (sessionManager *GettySessionManager) RegisterGettySession(session getty.Session) {
  96. allSessions.Store(session, true)
  97. m, _ := serverSessions.LoadOrStore(session.RemoteAddr(), &sync.Map{})
  98. sMap := m.(*sync.Map)
  99. sMap.Store(session, true)
  100. atomic.AddInt32(&sessionSize, 1)
  101. }