本文整理匯總了Golang中github.com/golang/groupcache.NewHTTPPool函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewHTTPPool函數的具體用法?Golang NewHTTPPool怎麽用?Golang NewHTTPPool使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewHTTPPool函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
// STARTINIT OMIT
me := "http://10.0.0.1"
peers := groupcache.NewHTTPPool(me)
// Whenever peers change:
peers.Set("http://10.0.0.1", "http://10.0.0.2", "http://10.0.0.3")
// ENDINIT OMIT
// STARTGROUP OMIT
var thumbNails = groupcache.NewGroup("thumbnail", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
fileName := key
dest.SetBytes(generateThumbnail(fileName))
return nil
}))
// ENDGROUP OMIT
var ctx groupcache.Context
var w http.ResponseWriter
var r *http.Request
// STARTUSE OMIT
var data []byte
err := thumbNails.Get(ctx, "big-file.jpg",
groupcache.AllocatingByteSliceSink(&data))
// ...
_ = err // OMIT
var modTime time.Time // OMIT
http.ServeContent(w, r, "big-file-thumb.jpg", modTime, bytes.NewReader(data))
// ENDUSE OMIT
}
示例2: main
func main() {
flag.Parse()
peers := gc.NewHTTPPool("http://localhost:" + *port)
peers.Set("http://localhost:8001", "http://localhost:8002")
cg = gc.NewGroup("QRCache", 1<<20, gc.GetterFunc(
func(ctx gc.Context, key string, dest gc.Sink) error {
fmt.Printf("asking for data of %s\n", key)
url := fmt.Sprintf("http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl=%s", key)
resp, err := http.Get(url)
if err != nil {
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
value := base64.StdEncoding.EncodeToString(body)
dest.SetBytes([]byte(value))
return nil
}))
// run groupcache process in goroutine
go http.ListenAndServe("localhost:"+*port, peers)
http.Handle("/", http.HandlerFunc(QR))
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
示例3: main
func main() {
flag.Parse()
// Setup the doozer connection.
d, err := doozer.Dial(daddr)
if err != nil {
log.Fatalf("connecting to doozer: %v\n", err)
}
defer d.Close()
// Setup the cache.
pool = groupcache.NewHTTPPool("http://" + addr)
dict = groupcache.NewGroup("dict", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
def, err := query(key)
if err != nil {
err = fmt.Errorf("querying remote dictionary: %v", err)
log.Println(err)
return err
}
log.Println("retrieved remote definition for", key)
dest.SetString(def)
return nil
}))
// Start watching for changes and signals.
go watch(d)
// Add the handler for definition requests and then start the
// server.
http.Handle("/define/", http.HandlerFunc(handler))
log.Println(http.ListenAndServe(addr, nil))
}
示例4: main
func main() {
me := ":" + os.Args[1]
peers := groupcache.NewHTTPPool("http://localhost" + me)
peers.Set("http://localhost:8081", "http://localhost:8082",
"http://localhost:8083")
helloworld := groupcache.NewGroup("helloworld",
1024*1024*10, groupcache.GetterFunc(
func(ctx groupcache.Context,
key string,
dest groupcache.Sink) error {
log.Println(me)
dest.SetString(me + "->" + key)
return nil
}))
fmt.Println("GroupName: ", helloworld.Name())
http.HandleFunc("/xbox/", func(w http.ResponseWriter, r *http.Request) {
parts := strings.SplitN(r.URL.Path[len("/xbox/"):], "/", 1)
for _, x := range parts {
fmt.Println("get: ", x)
}
if len(parts) != 1 {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
var data []byte
helloworld.Get(nil, parts[0], groupcache.AllocatingByteSliceSink(&data))
w.Write(data)
log.Println("Gets: ", helloworld.Stats.Gets.String())
log.Println("Load: ", helloworld.Stats.Loads.String())
log.Println("LocalLoad: ", helloworld.Stats.LocalLoads.String())
log.Println("PeerError: ", helloworld.Stats.PeerErrors.String())
log.Println("PeerLoad: ", helloworld.Stats.PeerLoads.String())
})
http.ListenAndServe(me, nil)
}
示例5: Pool
func Pool(ec2conn *ec2.EC2) *EC2CachePool {
localip := getLocalIP(ec2conn)
peers := &EC2CachePool{
groupcache.NewHTTPPool(fmt.Sprintf("http://%s:%s", localip, *cacheport)),
ec2conn,
}
peerAddrs, err := peers.discoverPeers()
if err != nil {
log.Fatal("discovery:", err)
}
log.Println("Alerting peers:", peerAddrs)
// Inform each peer that they should also discover peers
for _, peer := range peerAddrs {
client, err := rpc.DialHTTP("tcp", fmt.Sprintf("%s:%s", peer, *rpcport))
if err != nil {
log.Println("error dialing:", err)
continue
}
var reply int
err = client.Call("EC2CachePool.RefreshPeers", new(Args), &reply)
if err != nil {
log.Fatal("refresh rpc failure:", err)
}
log.Println("Refreshed peers on:", peer)
}
return peers
}
示例6: main
func main() {
// STARTINIT OMIT
me := "http://127.0.0.1:11211"
peers := groupcache.NewHTTPPool(me)
// Whenever peers change:
peers.Set("http://127.0.0.1:11211")
// ENDINIT OMIT
// STARTGROUP OMIT
var thumbNails = groupcache.NewGroup("thumbnail", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
dest.SetBytes(generateThumbnail(key))
return nil
}))
// ENDGROUP OMIT
var ctx groupcache.Context
// STARTUSE OMIT
var data []byte
thumbNails.Get(ctx, "big-file.jpg", groupcache.AllocatingByteSliceSink(&data))
fmt.Println(string(data))
}
示例7: DebugPool
func DebugPool() *DebugCachePool {
peers := &DebugCachePool{
groupcache.NewHTTPPool("http://localhost:9001"),
}
peers.Set("http://localhost:9001")
return peers
}
示例8: setupGroupcache
func setupGroupcache(config GroupcacheConfig) error {
if config.GB == 0 {
return nil
}
var cacheBytes int64
cacheBytes = int64(config.GB) << 30
pool := groupcache.NewHTTPPool(config.Host)
if pool != nil {
dvid.Infof("Initializing groupcache with %d GB at %s...\n", config.GB, config.Host)
manager.gcache.cache = groupcache.NewGroup("immutable", cacheBytes, groupcache.GetterFunc(
func(c groupcache.Context, key string, dest groupcache.Sink) error {
// Use KeyValueDB defined as context.
gctx, ok := c.(GroupcacheCtx)
if !ok {
return fmt.Errorf("bad groupcache context: expected GroupcacheCtx, got %v", c)
}
// First four bytes of key is instance ID to isolate groupcache collisions.
tk := TKey(key[4:])
data, err := gctx.KeyValueDB.Get(gctx.Context, tk)
if err != nil {
return err
}
return dest.SetBytes(data)
}))
manager.gcache.supported = make(map[dvid.DataSpecifier]struct{})
for _, dataspec := range config.Instances {
name := strings.Trim(dataspec, "\"")
parts := strings.Split(name, ":")
switch len(parts) {
case 2:
dataid := dvid.GetDataSpecifier(dvid.InstanceName(parts[0]), dvid.UUID(parts[1]))
manager.gcache.supported[dataid] = struct{}{}
default:
dvid.Errorf("bad data instance specification %q given for groupcache support in config file\n", dataspec)
}
}
// If we have additional peers, add them and start a listener via the HTTP port.
if len(config.Peers) > 0 {
peers := []string{config.Host}
peers = append(peers, config.Peers...)
pool.Set(peers...)
dvid.Infof("Groupcache configuration has %d peers in addition to local host.\n", len(config.Peers))
dvid.Infof("Starting groupcache HTTP server on %s\n", config.Host)
http.ListenAndServe(config.Host, http.HandlerFunc(pool.ServeHTTP))
}
}
return nil
}
示例9: main
func main() {
var err error
p = groupcache.NewHTTPPool("http://127.0.0.1:50000")
// update p's list
p.Set("http://127.0.0.1:50001", "http://127.0.0.1:50002")
// create a new group by name and set a function for get a value by key
// if you g.Get KEY at first time, it will store the value use sink, run the function
// if you g.Get KEY not at first time, actually it NOT entry the function
// does not support update a key's value
g = groupcache.NewGroup("dns", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) (err error) {
if ctx == nil {
ctx = "fuck"
}
log.Println(key, ctx)
err = dest.SetString(ctx.(string))
return
}))
// let it listen
go http.ListenAndServe("127.0.0.1:50000", nil)
// get a key's value into data
time.Sleep(2 * time.Second)
var data []byte
var key = "key"
err = g.Get("aa", key, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
log.Println("get error:", err)
}
log.Println(string(data))
time.Sleep(2 * time.Second)
key = "key1"
err = g.Get("bb", key, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
log.Println("get error:", err)
}
log.Println(string(data))
time.Sleep(2 * time.Second)
key = "key"
err = g.Get("cc", key, groupcache.AllocatingByteSliceSink(&data))
if err != nil {
log.Println("get error:", err)
}
log.Println(string(data))
}
示例10: initGroupcacheHTTPPool
func initGroupcacheHTTPPool() {
self := (&url.URL{Scheme: "http", Host: flagHTTP}).String()
var peers []string
peers = append(peers, self)
for _, p := range strings.Split(flagGroupcachePeers, ",") {
if p == "" {
continue
}
peer := (&url.URL{Scheme: "http", Host: p}).String()
peers = append(peers, peer)
}
pool := groupcache.NewHTTPPool(self)
pool.Context = imageserver_cache_groupcache.HTTPPoolContext
pool.Transport = imageserver_cache_groupcache.NewHTTPPoolTransport(nil)
pool.Set(peers...)
}
示例11: start
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)
}
示例12: initCacheGroup
func initCacheGroup() {
self := config.Get().CacheSelf()
pool := groupcache.NewHTTPPool(self)
pool.Set(config.Get().CachePeers()...)
if self != "" {
log.Println("Cache listen on:", strings.TrimLeft(self, "http://"))
go http.ListenAndServe(strings.TrimLeft(self, "http://"), http.HandlerFunc(pool.ServeHTTP))
}
cacheGroup = groupcache.NewGroup("imagio-storage", config.Get().CacheSize(), groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
dest.SetBytes(imgproc.Do(
Construct(new(query.Options), key).(*query.Options),
))
return nil
}),
)
}
示例13: main
func main() {
flag.Set("logtostderr", "true")
flag.Parse()
me := ":8080"
peers := groupcache.NewHTTPPool("http://localhost" + me)
peers.Set("http://localhost:8081", "http://localhost:8082", "http://localhost:8083")
helloworld := groupcache.NewGroup("helloworld", 1024*1024*1024*16, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
glog.Infof("%v, key = %v", me, key)
dest.SetString(key)
return nil
}))
glog.Infof("GroupName: %v", helloworld.Name())
http.HandleFunc("/xbox/",
func(w http.ResponseWriter, r *http.Request) {
parts := strings.SplitN(r.URL.Path[len("/xbox/"):], "/", 1)
glog.Infof("parts: %v", parts)
if len(parts) != 1 {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
var data []byte
helloworld.Get(nil, parts[0], groupcache.AllocatingByteSliceSink(&data))
w.Write(data)
glog.Infof("data: %s", data)
glog.Infof("Stats: %#v", helloworld.Stats)
//glog.Infof("Gets: %v", helloworld.Stats.Gets.String())
//glog.Infof("Load: %v", helloworld.Stats.Loads.String())
//glog.Infof("LocalLoad: %v", helloworld.Stats.LocalLoads.String())
//glog.Infof("PeerError: %v", helloworld.Stats.PeerErrors.String())
//glog.Infof("PeerLoad: %v", helloworld.Stats.PeerLoads.String())
})
http.ListenAndServe(me, nil)
}
示例14: ServeDir
func ServeDir(mountpoint string, target string, limit int, me string, peerlist []string) {
peers := groupcache.NewHTTPPool(me)
if peerlist != nil && len(peerlist) > 0 {
peers.Set(peerlist...)
}
filecache = groupcache.NewGroup("filecache", int64(limit)<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
contents, err := ioutil.ReadFile(key)
dest.SetBytes(contents)
return err
}))
c, err := fuse.Mount(mountpoint)
if err != nil {
log.Fatal(err)
}
fs.Serve(c, TargetDir{target})
}
示例15: main
func main() {
var port = flag.String("port", "8001", "groupcache port")
flag.Parse()
peers := groupcache.NewHTTPPool("http://localhost:" + *port)
client := new(client.Client)
var stringcache = groupcache.NewGroup("SlowDBCache", 64<<20, groupcache.GetterFunc(
func(ctx groupcache.Context, key string, dest groupcache.Sink) error {
result := client.Get(key)
fmt.Printf("asking for %s from dbserver\n", key)
dest.SetBytes([]byte(result))
return nil
}))
peers.Set("http://localhost:8001", "http://localhost:8002", "http://localhost:8003")
frontendServer := NewServer(stringcache)
i, err := strconv.Atoi(*port)
if err != nil {
// handle error
fmt.Println(err)
os.Exit(2)
}
var frontEndport = ":" + strconv.Itoa(i+1000)
go frontendServer.Start(frontEndport)
fmt.Println(stringcache)
fmt.Println("cachegroup slave starting on " + *port)
fmt.Println("frontend starting on " + frontEndport)
http.ListenAndServe("127.0.0.1:"+*port, http.HandlerFunc(peers.ServeHTTP))
}