Ejemplo n.º 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)
	}
}
Ejemplo n.º 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")
	}
}
Ejemplo n.º 3
0
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),
	}
}
Ejemplo n.º 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
}
Ejemplo n.º 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
}
Ejemplo n.º 6
0
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
	}
}
Ejemplo n.º 7
0
// 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
}
Ejemplo n.º 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")
	}
}
Ejemplo n.º 9
0
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
		}
Ejemplo n.º 10
0
	"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"`
Ejemplo n.º 11
0
Archivo: web.go Proyecto: 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}
}
Ejemplo n.º 12
0
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}
}
Ejemplo n.º 13
0
// 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),
	}
}