Exemplo n.º 1
0
func NewThrottler() *ClientThrottle {
	r := ClientThrottle{
		c:       cache.NewLRUCache(maxHosts),
		blocked: cache.NewLRUCache(maxHosts),
		stop:    make(chan bool),
	}
	go r.cleanup()
	return &r
}
Exemplo n.º 2
0
func (self *SchemaInfo) Open(ConnFactory CreateConnectionFunc, cachingInfo map[string]uint64) {
	conn, err := ConnFactory()
	if err != nil {
		panic(NewTabletError(FATAL, "Could not get connection: %v", err))
	}
	defer conn.Close()

	if cachingInfo == nil {
		cachingInfo = make(map[string]uint64)
	}
	self.LastReload = time.Now()
	tables, err := conn.ExecuteFetch([]byte("show tables"), 10000)
	if err != nil {
		panic(NewTabletError(FATAL, "Could not get table list: %v", err))
	}
	self.Tables = make(map[string]*TableInfo, len(tables.Rows))
	self.Tables["dual"] = NewTableInfo(conn, "dual", 0)
	for _, row := range tables.Rows {
		tableName := row[0].(string)
		tableInfo := NewTableInfo(conn, tableName, cachingInfo[tableName])
		if tableInfo == nil {
			continue
		}
		self.Tables[tableName] = tableInfo
	}
	self.Queries = cache.NewLRUCache(uint64(self.QueryCacheSize))
	self.ConnFactory = ConnFactory
	go self.SchemaReloader()
}
Exemplo n.º 3
0
func (self *TableInfo) initRowCache(conn *DBConnection, cacheSize uint64) {
	if self.PKColumns == nil {
		relog.Warning("Table %s has no primary key. Will not be cached.", self.Name)
		return
	}
	rowInfo, err := conn.ExecuteFetch([]byte(fmt.Sprintf("select * from %s where 1!=1", self.Name)), 10000)
	if err != nil {
		relog.Warning("Failed to fetch column info for %s, table will not be cached: %s", self.Name, err.Error())
		return
	}
	self.Fields = rowInfo.Fields
	self.CacheType = 1
	self.CacheSize = cacheSize
	self.RowCache = cache.NewLRUCache(self.CacheSize)
}
Exemplo n.º 4
0
func main() {
	cache := cache.NewLRUCache(cache_size_limit)

	context, _ := zmq.NewContext()
	receiver, _ := context.NewSocket(zmq.SUB)
	receiver.SetSockOptString(zmq.SUBSCRIBE, "")
	receiver.Connect("tcp://master.eve-emdr.com:8050")
	receiver.Connect("tcp://secondary.eve-emdr.com:8050")
	sender, _ := context.NewSocket(zmq.PUB)
	sender.Bind("tcp://0.0.0.0:8050")

	println("Listening on port 8050...")

	for {
		msg, zmq_err := receiver.Recv(0)

		if zmq_err != nil {
			println("RECV ERROR:", zmq_err.Error())
		}

		var h hash.Hash = fnv.New32()
		h.Write(msg)

		checksum := h.Sum([]byte{})
		cache_key := fmt.Sprintf("%x", checksum)

		cache_item, cache_hit := cache.Get(cache_key)
		if cache_hit {
			// We've already seen this before, ignore it.
			continue
		}

		// At this point, we know we've encountered a message we haven't
		// seen in the recent past.
		cache_item = &CacheValue{found: true}
		// Insert the cache entry to prevent future re-sends of this message.
		cache.Set(cache_key, cache_item)

		// A cache miss means that the incoming message is not a dupe.
		// Send the message to subscribers.
		sender.Send(msg, 0)
	}
}
Exemplo n.º 5
0
func NewConsolidator() *Consolidator {
	self := &Consolidator{queries: make(map[string]*Result), consolidations: cache.NewLRUCache(1000)}
	http.Handle("/debug/consolidations", self)
	return self
}
Exemplo n.º 6
0
func main() {
	if os.Getenv("ACCESS_TOKEN") == "" {
		// TODO: Add direct link to Development section of the README
		log.Fatal("ACCESS_TOKEN was not found!")
	}

	port := os.Getenv("PORT")
	if port == "" {
		port = "8888"
	}

	lru := cache.NewLRUCache(CacheCapacity)

	resp, err := http.Get("https://raw.github.com/progrium/viewdocs/master/docs/template.html")
	if err != nil || resp.StatusCode == 404 {
		log.Fatal("Unable to fetch default template")
	}
	body, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	DefaultTemplate = string(body)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.RequestURI == "/" {
			http.Redirect(w, r, "http://progrium.viewdocs.io/viewdocs", 301)
			return
		}
		if r.RequestURI == "/favicon.ico" {
			return
		}
		switch r.Method {
		case "GET":
			user, repo, ref, doc := parseRequest(r)
			log.Printf("Building docs for '%s/%s' (ref: %s)", user, repo, ref)
			key := user + ":" + repo + ":" + doc + ":" + ref
			value, ok := lru.Get(key)
			var output string
			if !ok {
				output, err = fetchAndRenderDoc(user, repo, ref, doc)
				if err != nil {
					w.WriteHeader(http.StatusInternalServerError)
					w.Write([]byte(err.Error()))
					return
				}
				lru.Set(key, &CacheValue{output, time.Now().Unix()})
				log.Println("CACHE MISS:", key, lru.StatsJSON())
			} else {
				output = value.(*CacheValue).Value
				if time.Now().Unix()-value.(*CacheValue).CreatedAt > CacheTTL {
					lru.Delete(key)
				}
			}
			w.Write([]byte(output))
		default:
			w.WriteHeader(http.StatusMethodNotAllowed)
		}
	})
	log.Println("Listening on port " + port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}
Exemplo n.º 7
0
func newPeerStore() *peerStore {
	return &peerStore{
		infoHashPeers:        cache.NewLRUCache(maxInfoHashes),
		localActiveDownloads: make(map[InfoHash]bool),
	}
}