コード例 #1
0
func (p *RelayMsgParser) SummaryHandler() http.HandlerFunc {
	// Initialize cache container with 1 second TTL, checks running twice a second.
	c := cache.New(1*time.Second, 500*time.Millisecond)
	return func(w http.ResponseWriter, r *http.Request) {
		localpart := vestigo.Param(r, "localpart")

		// Check cache first
		jsonUntyped, found := c.Get(localpart)
		if found {
			jsonBytes := jsonUntyped.([]byte)
			log.Printf("SummarizeEvents (cache): hit for [%s]", localpart)
			w.Write(jsonBytes)
			return
		}

		rows, err := p.Dbh.Query(fmt.Sprintf(`
			SELECT subject, count(distinct(smtp_from))
				FROM %s.relay_messages
			 WHERE smtp_to = $1 ||'@'|| $2
			 GROUP BY 1
		`, p.Schema), localpart, p.Domain)
		if err != nil {
			log.Printf("SummarizeEvents (SELECT): %s", err)
			http.Error(w, "Database error", http.StatusInternalServerError)
			return
		}
		defer rows.Close()

		res := map[string][]SummaryResponse{}
		for rows.Next() {
			if rows.Err() == io.EOF {
				break
			}
			s := SummaryResponse{}
			if err = rows.Scan(&s.Subject, &s.Count); err != nil {
				log.Printf("SummarizeEvents (Scan): %s", err)
				http.Error(w, "Database error", http.StatusInternalServerError)
				return
			}
			res["results"] = append(res["results"], s)
		}
		if err = rows.Err(); err != nil {
			log.Printf("SummarizeEvents (Err): %s", err)
			http.Error(w, "Database error", http.StatusInternalServerError)
			return
		}

		jsonBytes, err := json.Marshal(res)
		if err != nil {
			log.Printf("SummarizeEvents (JSON): %s", err)
			http.Error(w, "Encoding error", http.StatusInternalServerError)
			return
		}

		// Add result to cache
		c.Set(localpart, jsonBytes, cache.DefaultExpiration)

		w.Write(jsonBytes)
	}
}
コード例 #2
0
func TestDetectsRemoval(t *testing.T) {
	metadataClient := metadata.NewClient(fakeMetadataURL)
	kubeClient := kubernetesclient.NewClient(kubeURL, false)
	c := cache.New(1*time.Minute, 1*time.Minute)

	metadataHandler.hosts = []metadata.Host{
		{
			Name:     "test1",
			Hostname: "test1",
			Labels:   map[string]string{},
		},
	}

	kubeHandler.nodes["test1"] = &model.Node{
		Metadata: &model.ObjectMeta{
			Labels: map[string]interface{}{
				"test1": "val1",
			},
			Annotations: map[string]interface{}{
				"io.rancher.labels.test1": "",
			},
			Name: "test1",
		},
	}

	sync(kubeClient, metadataClient, c)

	if _, ok := kubeHandler.nodes["test1"].Metadata.Labels["test1"]; ok {
		t.Error("Label test1 was not detected as removed")
	}
}
コード例 #3
0
ファイル: dnssec.go プロジェクト: yuewko/coredns
func NewDnssec(zones []string, keys []*DNSKEY, next middleware.Handler) Dnssec {
	return Dnssec{Next: next,
		zones:    zones,
		keys:     keys,
		cache:    gcache.New(defaultDuration, purgeDuration),
		inflight: new(singleflight.Group),
	}
}
コード例 #4
0
func StartHostLabelSync(interval int, kClient *kubernetesclient.Client) error {
	metadataClient, err := metadata.NewClientAndWait(metadataURL)
	if err != nil {
		log.Errorf("Error initializing metadata client: [%v]", err)
		return err
	}
	expiringCache := cache.New(cacheExpiryMinutes, 1*time.Minute)
	h := &hostLabelSyncer{
		kClient:        kClient,
		metadataClient: metadataClient,
		cache:          expiringCache,
	}
	metadataClient.OnChange(interval, h.syncHostLabels)
	return nil
}
コード例 #5
0
func populatedCache() *gocache.Cache {
	var metrics []map[string]int
	var cache *gocache.Cache
	var resp *Response

	metrics = append(metrics, map[string]int{"okay": 1})
	cache = gocache.New(time.Minute, 20*time.Second)
	resp = &Response{
		Name:    "check_test",
		Status:  0,
		Stdout:  []string{"Mocked"},
		Metrics: metrics,
	}
	cache.Set("check_test", resp, gocache.DefaultExpiration)

	return cache
}
コード例 #6
0
ファイル: api.go プロジェクト: MetalMatze/Krautreporter-API
func serve(logger log.Logger, config *Config, db *gorm.DB) func(*cli.Context) error {
	return func(c *cli.Context) error {
		router := gin.New()
		router.Use(gin.Recovery())

		router.GET("/", func(c *gin.Context) {
			c.String(http.StatusOK, "hi")
		})

		router.GET("/health", func(c *gin.Context) {
			status := http.StatusOK
			c.String(status, http.StatusText(status))
		})

		repo := repository.Repository{
			Cache:  gocache.New(5*time.Minute, 30*time.Second),
			DB:     db,
			Logger: logger,
		}
		ctrl := controller.Controller{
			Logger:     logger,
			Repository: repo,
		}

		router.GET("/authors", ctrl.GetAuthors)
		router.GET("/authors/:id", ctrl.GetAuthor)

		router.GET("/articles", ctrl.GetArticles)
		router.GET("/articles/:id", ctrl.GetArticle)

		router.GET("/crawls", ctrl.GetCrawls)

		go func() {
			http.Handle("/metrics", prometheus.Handler())
			if err := http.ListenAndServe(":8081", nil); err != nil {
				panic(err)
			}
		}()

		if err := router.Run(config.Addr); err != nil {
			return err
		}

		return nil
	}
}
コード例 #7
0
ファイル: godutch.go プロジェクト: otaviof/godutch
// Instantiates a new GoDutch, which will also spawn a new Panamax.
func NewGoDutch(cfg *Config) (*GoDutch, error) {
	var cache *gocache.Cache
	var p *Panamax
	var g *GoDutch
	var err error

	cache = gocache.New(time.Minute, 20*time.Second)

	if p, err = NewPanamax(cache); err != nil {
		return nil, err
	}

	g = &GoDutch{
		cfg:              cfg,
		p:                p,
		cache:            cache,
		ns:               nil,
		cs:               nil,
		lastRunThreshold: -1,
	}

	return g, nil
}
コード例 #8
0
func TestDetectsChange(t *testing.T) {
	metadataClient := metadata.NewClient(fakeMetadataURL)
	kubeClient := kubernetesclient.NewClient(kubeURL, false)
	c := cache.New(1*time.Minute, 1*time.Minute)

	metadataHandler.hosts = []metadata.Host{
		{
			Name:     "test3",
			Hostname: "test3",
			Labels: map[string]string{
				"test3": "val3",
			},
		},
	}

	kubeHandler.nodes["test3"] = &model.Node{
		Metadata: &model.ObjectMeta{
			Labels: map[string]interface{}{
				"test3": "valx",
			},
			Annotations: map[string]interface{}{
				"io.rancher.labels.test3": "",
			},
			Name: "test3",
		},
	}

	sync(kubeClient, metadataClient, c)

	if val := kubeHandler.nodes["test3"].Metadata.Labels["test3"]; val != "val3" {
		t.Error("Label test3 was not detected as changed")
	}

	if _, ok := kubeHandler.nodes["test3"].Metadata.Annotations["io.rancher.labels.test3"]; !ok {
		t.Error("Annotation was not set on addition of new label")
	}
}
コード例 #9
0
ファイル: query.go プロジェクト: coralproject/xenia
var (
	ErrNotFound = errors.New("Set Not found")
)

// =============================================================================

// c contans a cache of set values. The cache will maintain items for one
// second and then marked as expired. This is a very small cache so the
// gc time will be every hour.

const (
	expiration = time.Second
	cleanup    = time.Hour
)

var cache = gc.New(expiration, cleanup)

// =============================================================================

// EnsureIndexes perform index create commands against Mongo for the indexes
// specied in each query for the set. It will attempt to ensure all indexes
// regardless if one fails. Then reports all failures.
func EnsureIndexes(context interface{}, db *db.DB, set *Set) error {
	log.Dev(context, "EnsureIndexes", "Started : Name[%s]", set.Name)

	var errStr string

	for _, q := range set.Queries {
		if len(q.Indexes) == 0 {
			continue
		}
コード例 #10
0
ファイル: scrapers.go プロジェクト: ubuntu-si/arso-api
	"crypto/md5"
	"encoding/hex"
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/PuerkitoBio/goquery"
	cache "github.com/patrickmn/go-cache"
)

var cacheArso = cache.New(5*time.Minute, 30*time.Second)

// Potres holds info about earthquake
type Potres struct {
	Magnituda float64
	Lat       float64
	Lon       float64
	Datum     string
	Lokacija  string
}

// Postaja holds info about weather
type Postaja struct {
	XMLName       *xml.Name `xml:"data" json:",omitempty"`
	ID            string    `xml:"metData>domain_meteosiId"`
	Title         string    `xml:"metData>domain_longTitle"`
コード例 #11
0
ファイル: web.go プロジェクト: beppeben/m3m3
func NewWebHandler(it *core.ItemInteractor, repo Repository, c ServerConfig,
	e EmailUtils, s SysUtils) *WebserviceHandler {
	return &WebserviceHandler{itemInteractor: it, repo: repo, usercache: cache.New(24*time.Hour, time.Hour),
		config: c, eutils: e, sutils: s}
}
コード例 #12
0
ファイル: cache.go プロジェクト: yuewko/coredns
func NewCache(ttl int, zones []string, next middleware.Handler) Cache {
	return Cache{Next: next, Zones: zones, cache: gcache.New(defaultDuration, purgeDuration), cap: time.Duration(ttl) * time.Second}
}
コード例 #13
0
ファイル: store_memory.go プロジェクト: wppurking/limiter
// NewMemoryStoreWithOptions creates a new instance of memory store with options.
func NewMemoryStoreWithOptions(options StoreOptions) Store {
	return &MemoryStore{
		Prefix: options.Prefix,
		Cache:  cache.New(cache.NoExpiration, options.CleanUpInterval),
	}
}