func NewFunServant(cf *config.ConfigServant) (this *FunServantImpl) { this = &FunServantImpl{ conf: cf, digitNormalizer: regexp.MustCompile(`\d+`), proxyMode: config.Engine.IsProxyOnly(), } // http REST to export internal state server.RegisterHttpApi("/svt/{cmd}", func(w http.ResponseWriter, req *http.Request, params map[string]interface{}) (interface{}, error) { return this.handleHttpQuery(w, req, params) }).Methods("GET") this.sessions = cache.NewLruCache(cf.SessionMaxItems) this.mysqlMergeMutexMap = mutexmap.New(cf.Mysql.JsonMergeMaxOutstandingItems) this.ctxReasonPercentage = metrics.NewPercentCounter() metrics.Register("call.reason", this.ctxReasonPercentage) this.dbCacheHits = metrics.NewPercentCounter() metrics.Register("db.cache.hits", this.dbCacheHits) this.createServants() return }
func newMysql(dsn string, maxStmtCached int, bc *config.ConfigBreaker) *mysql { this := new(mysql) if bc == nil { bc = &config.ConfigBreaker{ FailureAllowance: 5, RetryTimeout: time.Second * 10, } } this.dsn = dsn this.breaker = &breaker.Consecutive{ FailureAllowance: bc.FailureAllowance, RetryTimeout: bc.RetryTimeout} if maxStmtCached > 0 { this.stmtsStore = cache.NewLruCache(maxStmtCached) this.stmtsStore.OnEvicted = func(key cache.Key, value interface{}) { query := key.(string) stmt := value.(*sql.Stmt) stmt.Close() log.Debug("[%s] stmt[%s] closed", this.dsn, query) } } return this }
func NewFunServant(cf *config.ConfigServant) (this *FunServantImpl) { this = &FunServantImpl{conf: cf} this.lc = cache.NewLruCache(this.conf.Lcache.LruMaxItems) memcacheServers := this.conf.Memcache.ServerList() this.mc = memcache.New(this.conf.Memcache.HashStrategy, memcacheServers...) log.Debug("memcache servers %v", memcacheServers) return }
func NewFunServant(cf *config.ConfigServant) (this *FunServantImpl) { this = &FunServantImpl{conf: cf} this.lc = cache.NewLruCache(this.conf.Lcache.LruMaxItems) this.lc.OnEvicted = this.onLcLruEvicted memcacheServers := this.conf.Memcache.ServerList() this.mc = memcache.New(this.conf.Memcache.HashStrategy, memcacheServers...) this.mc.Timeout = time.Duration(this.conf.Memcache.Timeout) * time.Second this.mc.MaxIdleConnsPerServer = this.conf.Memcache.MaxIdleConnsPerServer this.mg = mongo.New(this.conf.Mongodb) return }
func newStandardServerSelector(cf *config.ConfigMysql) (this *StandardServerSelector) { this = new(StandardServerSelector) this.conf = cf this.lookupCache = cache.NewLruCache(cf.LookupCacheMaxItems) this.clients = make(map[string]*mysql) for _, server := range cf.Servers { my := newMysql(server.DSN(), cf.CachePrepareStmtMaxItems, &cf.Breaker) for retries := uint(0); retries < cf.Breaker.FailureAllowance; retries++ { log.Debug("mysql connecting: %s", server.DSN()) if my.Open() == nil && my.Ping() == nil { // sql.Open() does not establish any connections to the database // sql.Ping() does break } my.breaker.Fail() } if !my.breaker.Open() { my.db.SetMaxIdleConns(cf.MaxIdleConnsPerServer) // TODO https://code.google.com/p/go/source/detail?r=8a7ac002f840 my.db.SetMaxOpenConns(cf.MaxConnsPerServer) this.clients[server.Pool] = my if cf.MaxIdleTime > 0 { go func() { for _ = range time.Tick(cf.MaxIdleTime) { if err := my.Ping(); err != nil { log.Error("mysql[%s]: %s", server.DSN(), err.Error()) my.breaker.Fail() } } }() } } } return }
func New(cf *config.ConfigLock) *Lock { this := &Lock{cf: cf} this.items = cache.NewLruCache(cf.MaxItems) return this }
// TODO kill some servant if new conf turns it off func (this *FunServantImpl) recreateServants(cf *config.ConfigServant) { log.Info("recreating servants...") if this.conf.IdgenWorkerId != cf.IdgenWorkerId { log.Debug("recreating servant: idgen") var err error this.idgen, err = idgen.NewIdGenerator(cf.IdgenWorkerId) if err != nil { panic(err) } } if cf.Lcache.Enabled() && this.conf.Lcache.MaxItems != cf.Lcache.MaxItems { log.Debug("recreating servant: lcache") this.lc = cache.NewLruCache(cf.Lcache.MaxItems) this.lc.OnEvicted = this.onLcLruEvicted } if cf.Memcache.Enabled() && !reflect.DeepEqual(*this.conf.Memcache, *cf.Memcache) { log.Debug("recreating servant: memcache") this.mc = memcache.New(cf.Memcache) } if cf.Redis.Enabled() && !reflect.DeepEqual(*this.conf.Redis, *cf.Redis) { log.Debug("recreating servant: redis") this.rd = redis.New(cf.Redis) } if cf.Mysql.Enabled() && !reflect.DeepEqual(*this.conf.Mysql, *cf.Mysql) { log.Debug("recreating servant: mysql") this.my = mysql.New(cf.Mysql) switch cf.Mysql.CacheStore { case "mem": this.dbCacheStore = store.NewMemStore(cf.Mysql.CacheStoreMemMaxItems) case "redis": this.dbCacheStore = store.NewRedisStore(cf.Mysql.CacheStoreRedisPool, cf.Redis) default: panic("unknown cache store") } } if cf.Mongodb.Enabled() && !reflect.DeepEqual(*this.conf.Mongodb, *cf.Mongodb) { log.Debug("recreating servant: mongodb") this.mg = mongo.New(cf.Mongodb) } if cf.Couchbase.Enabled() && !reflect.DeepEqual(*this.conf.Couchbase, *cf.Couchbase) { log.Debug("recreating servant: couchbase") var err error // pool is always 'default' this.cb, err = couch.New(cf.Couchbase.Servers, "default") if err != nil { log.Error("couchbase: %s", err) } } this.conf = cf log.Info("servants recreated") }
func (this *FunServantImpl) createServants() { log.Info("creating servants...") // proxy can dynamically auto discover peers if this.conf.Proxy.Enabled() { log.Debug("creating servant: proxy") this.proxy = proxy.New(this.conf.Proxy) } else { panic("peers proxy required") } log.Debug("creating servant: idgen") var err error this.idgen, err = idgen.NewIdGenerator(this.conf.IdgenWorkerId) if err != nil { panic(err) } if this.conf.Lcache.Enabled() { log.Debug("creating servant: lcache") this.lc = cache.NewLruCache(this.conf.Lcache.MaxItems) this.lc.OnEvicted = this.onLcLruEvicted } if this.conf.Memcache.Enabled() { log.Debug("creating servant: memcache") this.mc = memcache.New(this.conf.Memcache) } if this.conf.Redis.Enabled() { log.Debug("creating servant: redis") this.rd = redis.New(this.conf.Redis) } if this.conf.Lock.Enabled() { log.Debug("creating servant: lock") this.lk = lock.New(this.conf.Lock) } if this.conf.Mysql.Enabled() { log.Debug("creating servant: mysql") this.my = mysql.New(this.conf.Mysql) switch this.conf.Mysql.CacheStore { case "mem": this.dbCacheStore = store.NewMemStore(this.conf.Mysql.CacheStoreMemMaxItems) case "redis": this.dbCacheStore = store.NewRedisStore(this.conf.Mysql.CacheStoreRedisPool, this.conf.Redis) default: panic("unknown mysql cache store") } } if this.conf.Mongodb.Enabled() { log.Debug("creating servant: mongodb") this.mg = mongo.New(this.conf.Mongodb) if this.conf.Mongodb.DebugProtocol || this.conf.Mongodb.DebugHeartbeat { mgo.SetLogger(&mongoProtocolLogger{}) mgo.SetDebug(this.conf.Mongodb.DebugProtocol) } } if this.conf.Couchbase.Enabled() { log.Debug("creating servant: couchbase") var err error // pool is always 'default' this.cb, err = couch.New(this.conf.Couchbase.Servers, "default") if err != nil { log.Error("couchbase: %s", err) } } log.Info("servants created") }
func New(maxEntries int) *MutexMap { this := &MutexMap{} this.items = cache.NewLruCache(maxEntries) return this }
func NewMemStore(maxEntries int) *MemStore { this := &MemStore{data: cache.NewLruCache(maxEntries)} return this }