Skip to content

Commit

Permalink
feat(docs): add random example, update readme with info note
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Apr 3, 2016
1 parent 6122cba commit 87a7c05
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Simple, generic, domain-agnostic balancing algorithms for vinxi.

Currently provides `random` and `roundrobin` distribution algorithms.
New algorithms may be added in the future.

## Installation

```bash
Expand All @@ -14,6 +17,8 @@ See [godoc](https://godoc.org/github.com/vinxi/balancer) reference.

## Example

#### Round-robin balancing

```go
package main

Expand Down Expand Up @@ -43,6 +48,37 @@ func main() {
}
```

#### Random balancing

```go
package main

import (
"fmt"
"gopkg.in/vinxi/balancer.v0"
)

func main() {
servers := []string{
"http://1.server.com",
"http://2.server.com",
"http://3.server.com",
}

lb := balancer.NewRandom()

for i := 0; i < 9; i++ {
fmt.Println("Next balance round...")
server, err := lb.Balance(servers)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
fmt.Printf("Next target server: %s\n", server)
}
}
```

## License

MIT
26 changes: 26 additions & 0 deletions _examples/random/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"gopkg.in/vinxi/balancer.v0"
)

func main() {
servers := []string{
"http://1.server.com",
"http://2.server.com",
"http://3.server.com",
}

lb := balancer.NewRandom()

for i := 0; i < 9; i++ {
fmt.Println("Next balance round...")
server, err := lb.Balance(servers)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
fmt.Printf("Next target server: %s\n", server)
}
}

0 comments on commit 87a7c05

Please sign in to comment.