Skip to content

Commit

Permalink
Add caching of nodes for rol
Browse files Browse the repository at this point in the history
  • Loading branch information
labkode committed Jan 26, 2017
1 parent 68ba838 commit fcd2c67
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 17 deletions.
6 changes: 4 additions & 2 deletions etc/oc-monolithic.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"ocfsm_data_driver_temporary_folder": "/tmp",
"ocfsm_data_driver_max_sql_iddle": 0,
"ocfsm_data_driver_max_sql_concurrent": 0,
"ocfsm_data_driver_dsn": "root:passwd@tcp(localhost:3306)/owncloud",
"ocfsm_data_driver_dsn": "root:labkode@tcp(localhost:3306)/owncloud",
"token_driver": "jwttokendriver",
"jwt_token_driver_key": "",
"basic_auth_middleware": "local",
"basic_auth_middleware_cookie_name": "oc_sessionpassphrase",
"cors_middleware_enabled": true,
"cors_middleware_access_control_allow_origin": "*",
Expand All @@ -30,5 +31,6 @@
"oc_web_service": "local",
"data_web_service_max_upload_file_size": 10000000000,
"oc_web_service_max_upload_file_size": 10000000000,
"oc_web_service_chunks_folder": "/tmp/chunks"
"oc_web_service_chunks_folder": "/tmp/chunks",
"registry_driver": "etcd"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/clawio/clawiod/root"
"github.com/go-kit/kit/log/levels"
"github.com/patrickmn/go-cache"
"io/ioutil"
"math/rand"
"net/http"
Expand All @@ -18,15 +19,22 @@ type webServiceClient struct {
cm root.ContextManager
client *http.Client
registryDriver root.RegistryDriver
cache *cache.Cache
}

// New returns an implementation of DataDriver.
func New(logger levels.Levels, cm root.ContextManager, registryDriver root.RegistryDriver) root.AuthenticationWebServiceClient{
rand.Seed(time.Now().Unix()) // initialize global pseudorandom generator
return &webServiceClient{logger: logger, cm: cm, client: http.DefaultClient, registryDriver: registryDriver}
func New(logger levels.Levels, cm root.ContextManager, registryDriver root.RegistryDriver) root.AuthenticationWebServiceClient {
cache := cache.New(time.Second*10, time.Second*10)
rand.Seed(time.Now().Unix()) // initialize global pseudo-random generator
return &webServiceClient{logger: logger, cm: cm, client: http.DefaultClient, registryDriver: registryDriver, cache: cache}
}

func (c *webServiceClient) getAuthenticationURL(ctx context.Context) (string, error) {
u, ok := c.cache.Get("url")
if ok {
return u.(string), nil
}

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
Expand All @@ -42,7 +50,9 @@ func (c *webServiceClient) getAuthenticationURL(ctx context.Context) (string, er
c.logger.Info().Log("msg", "got authentication-nodes", "numnodes", len(nodes))
chosenNode := nodes[rand.Intn(len(nodes))]
c.logger.Info().Log("msg", "authentication-node chosen", "authentication-node-url", chosenNode.URL())
return chosenNode.URL() + "/auth", nil
chosenURL := chosenNode.URL() + "/auth"
c.cache.Set("url", chosenURL, cache.NoExpiration)
return chosenURL, nil
}

func (c *webServiceClient) Token(ctx context.Context, username, password string) (string, error) {
Expand Down
14 changes: 12 additions & 2 deletions root/datawebserviceclient/datawebserviceclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/clawio/clawiod/root"
"github.com/go-kit/kit/log/levels"
"github.com/patrickmn/go-cache"
"io"
"math/rand"
"net/http"
Expand All @@ -17,15 +18,22 @@ type webServiceClient struct {
cm root.ContextManager
client *http.Client
registryDriver root.RegistryDriver
cache *cache.Cache
}

// New returns an implementation of DataDriver.
func New(logger levels.Levels, cm root.ContextManager, registryDriver root.RegistryDriver) root.DataWebServiceClient {
cache := cache.New(time.Second*10, time.Second*10)
rand.Seed(time.Now().Unix()) // initialize global pseudorandom generator
return &webServiceClient{logger: logger, cm: cm, client: http.DefaultClient, registryDriver: registryDriver}
return &webServiceClient{logger: logger, cm: cm, client: http.DefaultClient, registryDriver: registryDriver, cache: cache}
}

func (c *webServiceClient) getDataURL(ctx context.Context) (string, error) {
u, ok := c.cache.Get("url")
if ok {
return u.(string), nil
}

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
Expand All @@ -41,7 +49,9 @@ func (c *webServiceClient) getDataURL(ctx context.Context) (string, error) {
c.logger.Info().Log("msg", "got data-nodes", "numnodes", len(nodes))
chosenNode := nodes[rand.Intn(len(nodes))]
c.logger.Info().Log("msg", "data-node chosen", "data-node-url", chosenNode.URL())
return chosenNode.URL() + "/data", nil
chosenURL := chosenNode.URL() + "/data"
c.cache.Set("url", chosenURL, cache.NoExpiration)
return chosenURL, nil
}
func (c *webServiceClient) UploadFile(ctx context.Context, user root.User, path string, r io.ReadCloser, clientChecksum string) error {
traceID := c.cm.MustGetTraceID(ctx)
Expand Down
14 changes: 12 additions & 2 deletions root/metadatawebserviceclient/metadatawebserviceclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@ import (
"fmt"
"github.com/clawio/clawiod/root"
"github.com/go-kit/kit/log/levels"
"github.com/patrickmn/go-cache"
"io/ioutil"
"math/rand"
"net/http"
"time"
)

type webServiceClient struct {
logger levels.Levels
cm root.ContextManager
client *http.Client
registryDriver root.RegistryDriver
cache *cache.Cache
}

func New(logger levels.Levels, cm root.ContextManager, registryDriver root.RegistryDriver) root.MetaDataWebServiceClient {
return &webServiceClient{logger: logger, cm: cm, registryDriver: registryDriver, client: http.DefaultClient}
cache := cache.New(time.Second*10, time.Second*10)
return &webServiceClient{logger: logger, cm: cm, registryDriver: registryDriver, client: http.DefaultClient, cache: cache}
}

func (c *webServiceClient) getMetaDataURL(ctx context.Context) (string, error) {
u, ok := c.cache.Get("url")
if ok {
return u.(string), nil
}

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
Expand All @@ -39,7 +48,8 @@ func (c *webServiceClient) getMetaDataURL(ctx context.Context) (string, error) {
c.logger.Info().Log("msg", "got metadata-nodes", "numnodes", len(nodes))
chosenNode := nodes[rand.Intn(len(nodes))]
c.logger.Info().Log("msg", "metadata-node chosen", "metadata-node-url", chosenNode.URL())
return chosenNode.URL() + "/meta", nil
chosenURL := chosenNode.URL() + "/meta"
return chosenURL, nil
}

func (c *webServiceClient) Examine(ctx context.Context, user root.User, path string) (root.FileInfo, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@ import (
"fmt"
"github.com/clawio/clawiod/root"
"github.com/go-kit/kit/log/levels"
"github.com/patrickmn/go-cache"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"time"
)

type service struct {
logger levels.Levels
registryDriver root.RegistryDriver
cache *cache.Cache
}

func New(logger levels.Levels, registryDriver root.RegistryDriver) (root.WebService, error) {
cache := cache.New(time.Second*10, time.Second*10)
return &service{
logger: logger,
registryDriver: registryDriver,
cache: cache,
}, nil
}

func (s *service) getProxy(ctx context.Context) (*httputil.ReverseProxy, error) {
p, ok := s.cache.Get("proxy")
if ok {
return p.(*httputil.ReverseProxy), nil
}

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
Expand All @@ -43,7 +53,9 @@ func (s *service) getProxy(ctx context.Context) (*httputil.ReverseProxy, error)
if err != nil {
return nil, err
}
return httputil.NewSingleHostReverseProxy(u), nil
proxy := httputil.NewSingleHostReverseProxy(u)
s.cache.Set("proxy", proxy, cache.NoExpiration)
return proxy, nil
}

func (s *service) IsProxy() bool {
Expand Down
14 changes: 13 additions & 1 deletion root/proxieddatawebservice/proxieddatawebservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@ import (
"fmt"
"github.com/clawio/clawiod/root"
"github.com/go-kit/kit/log/levels"
"github.com/patrickmn/go-cache"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"time"
)

type service struct {
logger levels.Levels
registryDriver root.RegistryDriver
cache *cache.Cache
}

func New(logger levels.Levels, registryDriver root.RegistryDriver) (root.WebService, error) {
cache := cache.New(time.Second * 10, time.Second * 10)
return &service{
logger: logger,
registryDriver: registryDriver,
cache: cache,
}, nil
}

func (s *service) getProxy(ctx context.Context) (*httputil.ReverseProxy, error) {
p, ok := s.cache.Get("proxy")
if ok {
return p.(*httputil.ReverseProxy), nil
}

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
Expand All @@ -43,7 +53,9 @@ func (s *service) getProxy(ctx context.Context) (*httputil.ReverseProxy, error)
if err != nil {
return nil, err
}
return httputil.NewSingleHostReverseProxy(u), nil
proxy := httputil.NewSingleHostReverseProxy(u)
s.cache.Set("proxy", proxy, cache.NoExpiration)
return proxy, nil
}

func (s *service) IsProxy() bool {
Expand Down
14 changes: 13 additions & 1 deletion root/proxiedmetadatawebservice/proxiedmetadatawebservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@ import (
"fmt"
"github.com/clawio/clawiod/root"
"github.com/go-kit/kit/log/levels"
"github.com/patrickmn/go-cache"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"time"
)

type service struct {
logger levels.Levels
registryDriver root.RegistryDriver
cache *cache.Cache
}

func New(logger levels.Levels, registryDriver root.RegistryDriver) (root.WebService, error) {
cache := cache.New(time.Second*10, time.Second*10)
return &service{
logger: logger,
registryDriver: registryDriver,
cache: cache,
}, nil
}

func (s *service) getProxy(ctx context.Context) (*httputil.ReverseProxy, error) {
p, ok := s.cache.Get("proxy")
if ok {
return p.(*httputil.ReverseProxy), nil
}

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
Expand All @@ -43,7 +53,9 @@ func (s *service) getProxy(ctx context.Context) (*httputil.ReverseProxy, error)
if err != nil {
return nil, err
}
return httputil.NewSingleHostReverseProxy(u), nil
proxy := httputil.NewSingleHostReverseProxy(u)
s.cache.Set("proxy", proxy, cache.NoExpiration)
return proxy, nil
}

func (s *service) IsProxy() bool {
Expand Down
14 changes: 13 additions & 1 deletion root/proxiedocwebservice/proxiedocwebservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@ import (
"fmt"
"github.com/clawio/clawiod/root"
"github.com/go-kit/kit/log/levels"
"github.com/patrickmn/go-cache"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"time"
)

type service struct {
logger levels.Levels
registryDriver root.RegistryDriver
cache *cache.Cache
}

func New(logger levels.Levels, registryDriver root.RegistryDriver) (root.WebService, error) {
cache := cache.New(time.Second*10, time.Second*10)
return &service{
logger: logger,
registryDriver: registryDriver,
cache: cache,
}, nil
}

func (s *service) getProxy(ctx context.Context) (*httputil.ReverseProxy, error) {
p, ok := s.cache.Get("proxy")
if ok {
return p.(*httputil.ReverseProxy), nil
}

// TODO(labkode) the logic for choosing a node is very rudimentary.
// In the future would be nice to have at least RoundRobin.
// Thanks that clients are registry aware we an use our own algorithms
Expand All @@ -43,7 +53,9 @@ func (s *service) getProxy(ctx context.Context) (*httputil.ReverseProxy, error)
if err != nil {
return nil, err
}
return httputil.NewSingleHostReverseProxy(u), nil
proxy := httputil.NewSingleHostReverseProxy(u)
s.cache.Set("proxy", proxy, cache.NoExpiration)
return proxy, nil
}

func (s *service) IsProxy() bool {
Expand Down
6 changes: 3 additions & 3 deletions root/remotebasicauthmiddleware/remotebasicauthmiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

type middleware struct {
cookieName string
cm root.ContextManager
cookieName string
cm root.ContextManager
authenticationWebServiceClient root.AuthenticationWebServiceClient
tokenDriver root.TokenDriver
tokenDriver root.TokenDriver
}

func New(cm root.ContextManager, authenticationWebServiceClient root.AuthenticationWebServiceClient, tokenDriver root.TokenDriver, cookieName string) root.BasicAuthMiddleware {
Expand Down

0 comments on commit fcd2c67

Please sign in to comment.