Skip to content

Instantly share code, notes, and snippets.

@philsong
Forked from icub3d/main.go
Created November 15, 2013 15:43
Show Gist options
  • Save philsong/7486388 to your computer and use it in GitHub Desktop.
Save philsong/7486388 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/golang/groupcache"
"io"
"net"
"net/http"
"os"
"strings"
)
// This instances address and pool.
var addr string
var pool *groupcache.HTTPPool
// This instances group.
var dns *groupcache.Group
func init() {
flag.StringVar(&addr, "addr", "127.0.0.1:50000",
"the addr:port this groupcache instance should run on.")
}
func main() {
flag.Parse()
start()
console()
}
func start() {
pool = groupcache.NewHTTPPool("http://" + addr)
dns = groupcache.NewGroup("dns", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
// Lookup the adddress based on the hostname (key).
addrs, err := net.LookupHost(key)
if err != nil {
return err
}
// We'll just store the first.
dest.SetString(addrs[0])
return nil
}))
// Run in the background so we can use the console.
go http.ListenAndServe(addr, nil)
}
func console() {
scanner := bufio.NewScanner(os.Stdin)
quit := false
for !quit {
fmt.Print("gc> ")
if !scanner.Scan() {
break
}
line := scanner.Text()
parts := strings.Split(line, " ")
cmd := parts[0]
args := parts[1:]
switch cmd {
case "peers":
pool.Set(args...)
case "stats":
stats := dns.CacheStats(groupcache.MainCache)
fmt.Println("Bytes: ", stats.Bytes)
fmt.Println("Items: ", stats.Items)
fmt.Println("Gets: ", stats.Gets)
fmt.Println("Hits: ", stats.Hits)
fmt.Println("Evictions:", stats.Evictions)
case "get":
var data []byte
err := dns.Get(nil, args[0],
groupcache.AllocatingByteSliceSink(&data))
if err != nil {
fmt.Println("get error:", err)
continue
}
fmt.Print(args[0], ":")
io.Copy(os.Stdout, bytes.NewReader(data))
fmt.Println()
case "quit":
quit = true
default:
fmt.Println("unrecognized command:", cmd, args)
}
}
if err := scanner.Err(); err != nil {
fmt.Println("reading stdin:", err)
}
}
# Console 1
$ ./gcl -addr=127.0.0.1:50000
gc> peers http://127.0.0.1:50000 http://127.0.0.1:50001 http://127.0.0.1:50002
gc> get www.google.com
www.google.com:74.125.224.176
gc> stats
Bytes: 28
Items: 1
Gets: 3
Hits: 2
Evictions: 0
gc> quit
# Console 2
$ ./gcl -addr=127.0.0.1:50001
gc> peers http://127.0.0.1:50000 http://127.0.0.1:50001 http://127.0.0.1:50002
gc> get www.google.com
www.google.com:74.125.224.176
gc> stats
Bytes: 0
Items: 0
Gets: 1
Hits: 0
Evictions: 0
gc> get www.google.com
www.google.com:74.125.224.176
gc> stats
Bytes: 28
Items: 1
Gets: 2
Hits: 0
Evictions: 0
gc> quit
# Console 3
$ ./gcl -addr=127.0.0.1:50002
gc> peers http://127.0.0.1:50000 http://127.0.0.1:50001 http://127.0.0.1:50002
gc> get www.google.com
www.google.com:74.125.224.176
gc> stats
Bytes: 0
Items: 0
Gets: 1
Hits: 0
Evictions: 0
gc> get www.google.com
www.google.com:74.125.224.176
gc> stats
Bytes: 28
Items: 1
Gets: 2
Hits: 0
Evictions: 0
gc> quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment