Config struct for memcache client

This commit is contained in:
Jonathan Lange
2016-07-04 13:25:45 +01:00
parent 96520d7a46
commit f7bdedc149
2 changed files with 23 additions and 9 deletions

View File

@@ -53,28 +53,37 @@ type MemcacheClient struct {
wait sync.WaitGroup
}
// MemcacheConfig defines how a MemcacheClient should be constructed.
type MemcacheConfig struct {
Host string
Timeout time.Duration
Service string
UpdateInterval time.Duration
Expiration int32
}
// NewMemcacheClient creates a new MemcacheClient that gets its server list
// from SRV and updates the server list on a regular basis.
func NewMemcacheClient(host string, timeout time.Duration, service string, updateInterval time.Duration, expiration int32) *MemcacheClient {
func NewMemcacheClient(config MemcacheConfig) *MemcacheClient {
var servers memcache.ServerList
client := memcache.NewFromSelector(&servers)
client.Timeout = timeout
client.Timeout = config.Timeout
newClient := &MemcacheClient{
client: client,
serverList: &servers,
expiration: expiration,
hostname: host,
service: service,
expiration: config.Expiration,
hostname: config.Host,
service: config.Service,
quit: make(chan struct{}),
}
err := newClient.updateMemcacheServers()
if err != nil {
log.Errorf("Error setting memcache servers to '%v': %v", host, err)
log.Errorf("Error setting memcache servers to '%v': %v", config.Host, err)
}
newClient.wait.Add(1)
go newClient.updateLoop(updateInterval)
go newClient.updateLoop(config.UpdateInterval)
return newClient
}

View File

@@ -110,8 +110,13 @@ func collectorFactory(userIDer multitenant.UserIDer, collectorURL, s3URL, natsHo
var memcacheClient *multitenant.MemcacheClient
if memcachedHostname != "" {
memcacheClient = multitenant.NewMemcacheClient(
memcachedHostname, memcachedTimeout, memcachedService,
memcacheUpdateInterval, memcacheExpiration,
multitenant.MemcacheConfig{
Host: memcachedHostname,
Timeout: memcachedTimeout,
Expiration: memcacheExpiration,
UpdateInterval: memcacheUpdateInterval,
Service: memcachedService,
},
)
}
awsCollector, err := multitenant.NewAWSCollector(