项目作者: lafikl

项目描述 :
A Go library that implements Consistent Hashing and Consistent Hashing With Bounded Loads.
高级语言: Go
项目地址: git://github.com/lafikl/consistent.git
创建时间: 2017-06-11T03:04:00Z
项目社区:https://github.com/lafikl/consistent

开源协议:MIT License

下载


Package consistent

A Golang implementation of Consistent Hashing and Consistent Hashing With Bounded Loads.

https://en.wikipedia.org/wiki/Consistent_hashing

https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html

Consistent Hashing Example

  1. package main
  2. import (
  3. "log"
  4. "github.com/lafikl/consistent"
  5. )
  6. func main() {
  7. c := consistent.New()
  8. // adds the hosts to the ring
  9. c.Add("127.0.0.1:8000")
  10. c.Add("92.0.0.1:8000")
  11. // Returns the host that owns `key`.
  12. //
  13. // As described in https://en.wikipedia.org/wiki/Consistent_hashing
  14. //
  15. // It returns ErrNoHosts if the ring has no hosts in it.
  16. host, err := c.Get("/app.html")
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. log.Println(host)
  21. }

Consistent Hashing With Bounded Loads Example

  1. package main
  2. import (
  3. "log"
  4. "github.com/lafikl/consistent"
  5. )
  6. func main() {
  7. c := consistent.New()
  8. // adds the hosts to the ring
  9. c.Add("127.0.0.1:8000")
  10. c.Add("92.0.0.1:8000")
  11. // It uses Consistent Hashing With Bounded loads
  12. // https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
  13. // to pick the least loaded host that can serve the key
  14. //
  15. // It returns ErrNoHosts if the ring has no hosts in it.
  16. //
  17. host, err := c.GetLeast("/app.html")
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. // increases the load of `host`, we have to call it before sending the request
  22. c.Inc(host)
  23. // send request or do whatever
  24. log.Println("send request to", host)
  25. // call it when the work is done, to update the load of `host`.
  26. c.Done(host)
  27. }

Docs

https://godoc.org/github.com/lafikl/consistent

License

  1. MIT License
  2. Copyright (c) 2017 Khalid Lafi
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.