tracker/server/store/store.go

132 lines
2.9 KiB
Go
Raw Normal View History

2016-01-25 06:41:39 +01:00
// Copyright 2016 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package store
import (
"errors"
"log"
"sync"
"time"
"gopkg.in/yaml.v2"
2016-03-03 02:18:55 +01:00
"github.com/chihaya/chihaya"
2016-01-25 06:41:39 +01:00
"github.com/chihaya/chihaya/server"
"github.com/chihaya/chihaya/tracker"
)
var theStore *Store
func init() {
server.Register("store", constructor)
}
// ErrResourceDoesNotExist is the error returned by all delete methods in the
// store if the requested resource does not exist.
var ErrResourceDoesNotExist = errors.New("resource does not exist")
2016-03-03 02:18:55 +01:00
func constructor(srvcfg *chihaya.ServerConfig, tkr *tracker.Tracker) (server.Server, error) {
2016-01-25 06:41:39 +01:00
if theStore == nil {
cfg, err := newConfig(srvcfg)
if err != nil {
return nil, errors.New("store: invalid store config: " + err.Error())
}
2016-03-11 21:09:49 +01:00
ps, err := OpenPeerStore(&cfg.PeerStore)
2016-02-16 21:47:40 +01:00
if err != nil {
return nil, err
}
2016-03-11 21:09:49 +01:00
ips, err := OpenIPStore(&cfg.IPStore)
2016-02-16 21:47:40 +01:00
if err != nil {
return nil, err
}
2016-03-11 21:09:49 +01:00
ss, err := OpenStringStore(&cfg.StringStore)
2016-03-03 09:39:19 +01:00
if err != nil {
return nil, err
}
2016-01-25 06:41:39 +01:00
theStore = &Store{
2016-02-16 21:47:40 +01:00
cfg: cfg,
tkr: tkr,
shutdown: make(chan struct{}),
2016-02-16 21:47:40 +01:00
PeerStore: ps,
IPStore: ips,
2016-03-03 09:39:19 +01:00
StringStore: ss,
2016-01-25 06:41:39 +01:00
}
}
return theStore, nil
}
2016-04-07 16:24:37 +02:00
// Config represents the configuration for the store.
2016-01-25 06:41:39 +01:00
type Config struct {
2016-03-11 21:09:49 +01:00
Addr string `yaml:"addr"`
RequestTimeout time.Duration `yaml:"request_timeout"`
ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"`
GCAfter time.Duration `yaml:"gc_after"`
PeerStore DriverConfig `yaml:"peer_store"`
IPStore DriverConfig `yaml:"ip_store"`
StringStore DriverConfig `yaml:"string_store"`
}
2016-04-07 16:24:37 +02:00
// DriverConfig represents the configuration for a store driver.
2016-03-11 21:09:49 +01:00
type DriverConfig struct {
Name string `yaml:"name"`
Config interface{} `yaml:"config"`
2016-01-25 06:41:39 +01:00
}
2016-03-03 02:18:55 +01:00
func newConfig(srvcfg *chihaya.ServerConfig) (*Config, error) {
2016-03-02 22:15:48 +01:00
bytes, err := yaml.Marshal(srvcfg.Config)
2016-01-25 06:41:39 +01:00
if err != nil {
return nil, err
}
var cfg Config
err = yaml.Unmarshal(bytes, &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
// MustGetStore is used by middleware to access the store.
//
// This function calls log.Fatal if a server hasn't been already created by
// the server package.
func MustGetStore() *Store {
if theStore == nil {
log.Fatal("store middleware used without store server")
}
return theStore
}
2016-04-07 16:24:37 +02:00
// Store provides storage for a tracker.
2016-01-25 06:41:39 +01:00
type Store struct {
cfg *Config
tkr *tracker.Tracker
shutdown chan struct{}
wg sync.WaitGroup
PeerStore
IPStore
2016-03-03 09:39:19 +01:00
StringStore
2016-01-25 06:41:39 +01:00
}
2016-04-07 16:24:37 +02:00
// Start starts the store drivers and blocks until all of them exit.
2016-01-25 06:41:39 +01:00
func (s *Store) Start() {
<-s.shutdown
s.wg.Wait()
log.Println("Store server shut down cleanly")
2016-01-25 06:41:39 +01:00
}
2016-04-07 16:24:37 +02:00
// Stop stops the store drivers and waits for them to exit.
2016-01-25 06:41:39 +01:00
func (s *Store) Stop() {
close(s.shutdown)
s.wg.Wait()
}