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

130 lines
3.3KB

  1. /*
  2. Copyright 2011 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package memcache
  14. import (
  15. "hash/crc32"
  16. "net"
  17. "strings"
  18. "sync"
  19. )
  20. // ServerSelector is the interface that selects a memcache server
  21. // as a function of the item's key.
  22. //
  23. // All ServerSelector implementations must be safe for concurrent use
  24. // by multiple goroutines.
  25. type ServerSelector interface {
  26. // PickServer returns the server address that a given item
  27. // should be shared onto.
  28. PickServer(key string) (net.Addr, error)
  29. Each(func(net.Addr) error) error
  30. }
  31. // ServerList is a simple ServerSelector. Its zero value is usable.
  32. type ServerList struct {
  33. mu sync.RWMutex
  34. addrs []net.Addr
  35. }
  36. // staticAddr caches the Network() and String() values from any net.Addr.
  37. type staticAddr struct {
  38. ntw, str string
  39. }
  40. func newStaticAddr(a net.Addr) net.Addr {
  41. return &staticAddr{
  42. ntw: a.Network(),
  43. str: a.String(),
  44. }
  45. }
  46. func (s *staticAddr) Network() string { return s.ntw }
  47. func (s *staticAddr) String() string { return s.str }
  48. // SetServers changes a ServerList's set of servers at runtime and is
  49. // safe for concurrent use by multiple goroutines.
  50. //
  51. // Each server is given equal weight. A server is given more weight
  52. // if it's listed multiple times.
  53. //
  54. // SetServers returns an error if any of the server names fail to
  55. // resolve. No attempt is made to connect to the server. If any error
  56. // is returned, no changes are made to the ServerList.
  57. func (ss *ServerList) SetServers(servers ...string) error {
  58. naddr := make([]net.Addr, len(servers))
  59. for i, server := range servers {
  60. if strings.Contains(server, "/") {
  61. addr, err := net.ResolveUnixAddr("unix", server)
  62. if err != nil {
  63. return err
  64. }
  65. naddr[i] = newStaticAddr(addr)
  66. } else {
  67. tcpaddr, err := net.ResolveTCPAddr("tcp", server)
  68. if err != nil {
  69. return err
  70. }
  71. naddr[i] = newStaticAddr(tcpaddr)
  72. }
  73. }
  74. ss.mu.Lock()
  75. defer ss.mu.Unlock()
  76. ss.addrs = naddr
  77. return nil
  78. }
  79. // Each iterates over each server calling the given function
  80. func (ss *ServerList) Each(f func(net.Addr) error) error {
  81. ss.mu.RLock()
  82. defer ss.mu.RUnlock()
  83. for _, a := range ss.addrs {
  84. if err := f(a); nil != err {
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. // keyBufPool returns []byte buffers for use by PickServer's call to
  91. // crc32.ChecksumIEEE to avoid allocations. (but doesn't avoid the
  92. // copies, which at least are bounded in size and small)
  93. var keyBufPool = sync.Pool{
  94. New: func() interface{} {
  95. b := make([]byte, 256)
  96. return &b
  97. },
  98. }
  99. func (ss *ServerList) PickServer(key string) (net.Addr, error) {
  100. ss.mu.RLock()
  101. defer ss.mu.RUnlock()
  102. if len(ss.addrs) == 0 {
  103. return nil, ErrNoServers
  104. }
  105. if len(ss.addrs) == 1 {
  106. return ss.addrs[0], nil
  107. }
  108. bufp := keyBufPool.Get().(*[]byte)
  109. n := copy(*bufp, key)
  110. cs := crc32.ChecksumIEEE((*bufp)[:n])
  111. keyBufPool.Put(bufp)
  112. return ss.addrs[cs%uint32(len(ss.addrs))], nil
  113. }
上海开阖软件有限公司 沪ICP备12045867号-1