Skip to content

Commit

Permalink
api structure simplified (breaking changes), project directory struct…
Browse files Browse the repository at this point in the history
…ure refactored
  • Loading branch information
piotrkowalczuk committed Nov 26, 2017
1 parent 1ca0d8f commit 5377ed5
Show file tree
Hide file tree
Showing 53 changed files with 1,333 additions and 1,531 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Project
.idea
vendor
mnemosyne-inpkg
_vendor-*
.tmp
bin
Expand Down
18 changes: 10 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
VERSION?=$(shell git describe --tags --always --dirty)
SERVICE=mnemosyne
VERSION=$(shell git describe --tags --always --dirty)
ifeq ($(version),)
TAG=VERSION
else
TAG=$(version)
endif

PACKAGE=github.com/piotrkowalczuk/mnemosyne
PACKAGE_CMD_DAEMON=$(PACKAGE)/cmd/$(SERVICE)d
Expand All @@ -15,10 +20,7 @@ version:
echo ${VERSION} > VERSION.txt

gen:
go generate .
go generate ./${SERVICE}d
go generate ./${SERVICE}rpc
ls -al ./${SERVICE}rpc | grep "pb.go"
./scripts/generate.sh

build:
CGO_ENABLED=0 GOOS=linux go build -ldflags "${LDFLAGS}" -installsuffix cgo -a -o bin/${SERVICE}d ${PACKAGE_CMD_DAEMON}
Expand All @@ -39,9 +41,9 @@ get:
go get -u github.com/golang/dep/cmd/dep
dep ensure

publish:
publish: build
docker build \
--build-arg VCS_REF=${VCS_REF} \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
-t piotrkowalczuk/${SERVICE}:${VERSION} .
docker push piotrkowalczuk/${SERVICE}:${VERSION}
-t piotrkowalczuk/${SERVICE}:${TAG} .
docker push piotrkowalczuk/${SERVICE}:${TAG}
1 change: 0 additions & 1 deletion VERSION.txt

This file was deleted.

8 changes: 4 additions & 4 deletions cmd/mnemosyned/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"time"

"github.com/piotrkowalczuk/mnemosyne/mnemosyned"
"github.com/piotrkowalczuk/mnemosyne/internal/storage"
)

var (
Expand Down Expand Up @@ -61,12 +61,12 @@ func (c *configuration) init() {
flag.Var(&c.cluster.seeds, "cluster.seeds", "")
flag.StringVar(&c.catalog.http, "catalog.http", "http://localhost:8500/v1/catalog/service/mnemosyned", "address of service catalog ")
flag.StringVar(&c.catalog.dns, "catalog.dns", "", "dns server address that can resolve SRV lookup")
flag.DurationVar(&c.session.ttl, "ttl", mnemosyned.DefaultTTL, "session time to live, after which session is deleted")
flag.DurationVar(&c.session.ttc, "ttc", mnemosyned.DefaultTTC, "session time to cleanup, how offten cleanup will be performed")
flag.DurationVar(&c.session.ttl, "ttl", storage.DefaultTTL, "session time to live, after which session is deleted")
flag.DurationVar(&c.session.ttc, "ttc", storage.DefaultTTC, "session time to cleanup, how offten cleanup will be performed")
flag.StringVar(&c.logger.environment, "log.environment", "production", "logger environment config")
flag.StringVar(&c.logger.level, "log.level", "info", "logger level")
flag.BoolVar(&c.monitoring.enabled, "monitoring", false, "toggle application monitoring")
flag.StringVar(&c.storage, "storage", mnemosyned.StorageEnginePostgres, "storage engine") // TODO: change to in memory when implemented
flag.StringVar(&c.storage, "storage", storage.EnginePostgres, "storage engine") // TODO: change to in memory when implemented
flag.StringVar(&c.postgres.address, "postgres.address", "postgres://localhost?sslmode=disable", "storage postgres connection string")
flag.StringVar(&c.postgres.table, "postgres.table", "session", "postgres table name")
flag.StringVar(&c.postgres.schema, "postgres.schema", "mnemosyne", "postgres schema name")
Expand Down
3 changes: 0 additions & 3 deletions doc.go

This file was deleted.

99 changes: 99 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cache

import (
"sync"
"time"

"github.com/piotrkowalczuk/mnemosyne/mnemosynerpc"
"github.com/prometheus/client_golang/prometheus"
)

// DefaultSize determines how big cache should be at the beginning.
const DefaultSize = 100000

type Entry struct {
Ses mnemosynerpc.Session
Exp time.Time
Refresh bool
}

type Cache struct {
data map[uint64]*Entry
dataLock sync.RWMutex
TTL time.Duration
// monitoring
hitsTotal prometheus.Counter
missesTotal prometheus.Counter
refreshTotal prometheus.Counter
}

func New(ttl time.Duration, namespace string) *Cache {
return &Cache{
TTL: ttl,
data: make(map[uint64]*Entry, DefaultSize),
hitsTotal: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "cache",
Name: "hits_total",
Help: "Total number of cache hits.",
}),
missesTotal: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "cache",
Name: "misses_total",
Help: "Total number of cache misses.",
}),
refreshTotal: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "cache",
Name: "refresh_total",
Help: "Total number of times cache Refresh.",
}),
}
}

func (c *Cache) Refresh(k uint64) {
c.refreshTotal.Add(1)

c.dataLock.Lock()
c.data[k].Refresh = true
c.dataLock.Unlock()
}

func (c *Cache) Put(k uint64, ses mnemosynerpc.Session) {
c.dataLock.Lock()
c.data[k] = &Entry{Ses: ses, Exp: time.Now().Add(c.TTL), Refresh: false}
c.dataLock.Unlock()
}

func (c *Cache) Del(k uint64) {
c.dataLock.Lock()
delete(c.data, k)
c.dataLock.Unlock()
}

func (c *Cache) Read(k uint64) (*Entry, bool) {
c.dataLock.RLock()
entry, ok := c.data[k]
c.dataLock.RUnlock()
if ok {
c.hitsTotal.Add(1)
} else {
c.missesTotal.Add(1)
}
return entry, ok
}

// Collect implements prometheus Collector interface.
func (c *Cache) Collect(in chan<- prometheus.Metric) {
c.hitsTotal.Collect(in)
c.refreshTotal.Collect(in)
c.missesTotal.Collect(in)
}

// Describe implements prometheus Collector interface.
func (c *Cache) Describe(in chan<- *prometheus.Desc) {
c.hitsTotal.Describe(in)
c.refreshTotal.Describe(in)
c.missesTotal.Describe(in)
}
14 changes: 7 additions & 7 deletions mnemosyned/backpack.go → internal/model/backpack.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mnemosyned
package model

import (
"bytes"
Expand All @@ -7,10 +7,10 @@ import (
"errors"
)

type bag map[string]string
type Bag map[string]string

// Scan satisfy sql.Scanner interface.
func (b *bag) Scan(src interface{}) (err error) {
func (b *Bag) Scan(src interface{}) (err error) {
switch t := src.(type) {
case []byte:
err = gob.NewDecoder(bytes.NewReader(t)).Decode(b)
Expand All @@ -22,7 +22,7 @@ func (b *bag) Scan(src interface{}) (err error) {
}

// Value satisfy driver.Valuer interface.
func (b bag) Value() (driver.Value, error) {
func (b Bag) Value() (driver.Value, error) {
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(b)
if err != nil {
Expand All @@ -32,15 +32,15 @@ func (b bag) Value() (driver.Value, error) {
return buf.Bytes(), nil
}

func (b *bag) set(key, value string) {
func (b *Bag) Set(key, value string) {
(*b)[key] = value
}

func (b *bag) get(key string) string {
func (b *Bag) Get(key string) string {
return (*b)[key]
}

func (b *bag) has(key string) bool {
func (b *Bag) Has(key string) bool {
_, ok := (*b)[key]

return ok
Expand Down
46 changes: 46 additions & 0 deletions internal/model/backpack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package model_test

import (
"testing"

"github.com/piotrkowalczuk/mnemosyne/internal/model"
)

func TestBackpack(t *testing.T) {
b := model.Bag{}

b.Set("key", "value")

if !b.Has("key") {
t.Errorf("bagpack should have specified key")
}

if b.Get("key") != "value" {
t.Errorf("bagpack should have specified key")
}
}

func TestBackpack_Scan(t *testing.T) {
b := model.Bag{"A": "B"}
val, err := b.Value()
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
b.Set("A", "C")
if err = b.Scan(val); err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
if b.Get("A") != "B" {
t.Errorf("wrong output, got %s", b.Get("A"))
}

exp := "unsupported data source type"
b = model.Bag{}
if err := b.Scan("data"); err != nil {
if err.Error() != exp {
t.Errorf("wrong error, expected %s but got %s", exp, err.Error())
}
} else {
t.Error("error expected, got nil")
}
}
Loading

0 comments on commit 5377ed5

Please sign in to comment.