selector.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // SetServers changes a ServerList's set of servers at runtime and is
  37. // safe for concurrent use by multiple goroutines.
  38. //
  39. // Each server is given equal weight. A server is given more weight
  40. // if it's listed multiple times.
  41. //
  42. // SetServers returns an error if any of the server names fail to
  43. // resolve. No attempt is made to connect to the server. If any error
  44. // is returned, no changes are made to the ServerList.
  45. func (ss *ServerList) SetServers(servers ...string) error {
  46. naddr := make([]net.Addr, len(servers))
  47. for i, server := range servers {
  48. if strings.Contains(server, "/") {
  49. addr, err := net.ResolveUnixAddr("unix", server)
  50. if err != nil {
  51. return err
  52. }
  53. naddr[i] = addr
  54. } else {
  55. tcpaddr, err := net.ResolveTCPAddr("tcp", server)
  56. if err != nil {
  57. return err
  58. }
  59. naddr[i] = tcpaddr
  60. }
  61. }
  62. ss.mu.Lock()
  63. defer ss.mu.Unlock()
  64. ss.addrs = naddr
  65. return nil
  66. }
  67. // Each iterates over each server calling the given function
  68. func (ss *ServerList) Each(f func(net.Addr) error) error {
  69. ss.mu.RLock()
  70. defer ss.mu.RUnlock()
  71. for _, a := range ss.addrs {
  72. if err := f(a); nil != err {
  73. return err
  74. }
  75. }
  76. return nil
  77. }
  78. // keyBufPool returns []byte buffers for use by PickServer's call to
  79. // crc32.ChecksumIEEE to avoid allocations. (but doesn't avoid the
  80. // copies, which at least are bounded in size and small)
  81. var keyBufPool = sync.Pool{
  82. New: func() interface{} {
  83. b := make([]byte, 256)
  84. return &b
  85. },
  86. }
  87. func (ss *ServerList) PickServer(key string) (net.Addr, error) {
  88. ss.mu.RLock()
  89. defer ss.mu.RUnlock()
  90. if len(ss.addrs) == 0 {
  91. return nil, ErrNoServers
  92. }
  93. if len(ss.addrs) == 1 {
  94. return ss.addrs[0], nil
  95. }
  96. bufp := keyBufPool.Get().(*[]byte)
  97. n := copy(*bufp, key)
  98. cs := crc32.ChecksumIEEE((*bufp)[:n])
  99. keyBufPool.Put(bufp)
  100. return ss.addrs[cs%uint32(len(ss.addrs))], nil
  101. }