Refactoring variable names and variable types to saner versions.
This commit is contained in:
parent
c65a47f6e2
commit
1146736c2b
3 changed files with 40 additions and 38 deletions
|
@ -17,7 +17,7 @@ package realis
|
||||||
// Using a pattern described by Dave Cheney to differentiate errors
|
// Using a pattern described by Dave Cheney to differentiate errors
|
||||||
// https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully
|
// https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully
|
||||||
|
|
||||||
// Timeout errors are returned when a function is unable to continue executing due
|
// Timedout errors are returned when a function is unable to continue executing due
|
||||||
// to a time constraint or meeting a set number of retries.
|
// to a time constraint or meeting a set number of retries.
|
||||||
type timeout interface {
|
type timeout interface {
|
||||||
Timedout() bool
|
Timedout() bool
|
||||||
|
|
|
@ -26,7 +26,7 @@ import (
|
||||||
const (
|
const (
|
||||||
UpdateFailed = "update failed"
|
UpdateFailed = "update failed"
|
||||||
RolledBack = "update rolled back"
|
RolledBack = "update rolled back"
|
||||||
Timeout = "timeout"
|
Timedout = "timeout"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Monitor struct {
|
type Monitor struct {
|
||||||
|
@ -82,7 +82,7 @@ func (m *Monitor) JobUpdate(updateKey aurora.JobUpdateKey, interval int, timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
return false, errors.New(Timeout)
|
return false, errors.New(Timedout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,7 +117,7 @@ func (m *Monitor) ScheduleStatus(key *aurora.JobKey, instanceCount int32, desire
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
|
|
||||||
// If the timer runs out, return a timeout error to user
|
// If the timer runs out, return a timeout error to user
|
||||||
return false, errors.New(Timeout)
|
return false, errors.New(Timedout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -177,7 +177,7 @@ func (m *Monitor) HostMaintenance(hosts []string, modes []aurora.MaintenanceMode
|
||||||
hostResult[host] = false
|
hostResult[host] = false
|
||||||
}
|
}
|
||||||
|
|
||||||
return hostResult, errors.New(Timeout)
|
return hostResult, errors.New(Timedout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
68
realis.go
68
realis.go
|
@ -51,7 +51,7 @@ type RealisClient struct {
|
||||||
type RealisConfig struct {
|
type RealisConfig struct {
|
||||||
username, password string
|
username, password string
|
||||||
url string
|
url string
|
||||||
timeoutms int
|
timeout time.Duration
|
||||||
binTransport, jsonTransport bool
|
binTransport, jsonTransport bool
|
||||||
cluster *Cluster
|
cluster *Cluster
|
||||||
backoff Backoff
|
backoff Backoff
|
||||||
|
@ -59,8 +59,8 @@ type RealisConfig struct {
|
||||||
protoFactory thrift.TProtocolFactory
|
protoFactory thrift.TProtocolFactory
|
||||||
logger *LevelLogger
|
logger *LevelLogger
|
||||||
InsecureSkipVerify bool
|
InsecureSkipVerify bool
|
||||||
certspath string
|
certsPath string
|
||||||
clientkey, clientcert string
|
clientKey, clientCert string
|
||||||
options []ClientOption
|
options []ClientOption
|
||||||
debug bool
|
debug bool
|
||||||
zkOptions []ZKOpt
|
zkOptions []ZKOpt
|
||||||
|
@ -89,9 +89,9 @@ func SchedulerUrl(url string) ClientOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TimeoutMS(timeout int) ClientOption {
|
func Timeout(timeout time.Duration) ClientOption {
|
||||||
return func(config *RealisConfig) {
|
return func(config *RealisConfig) {
|
||||||
config.timeoutms = timeout
|
config.timeout = timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,13 +140,13 @@ func InsecureSkipVerify(InsecureSkipVerify bool) ClientOption {
|
||||||
|
|
||||||
func CertsPath(certspath string) ClientOption {
|
func CertsPath(certspath string) ClientOption {
|
||||||
return func(config *RealisConfig) {
|
return func(config *RealisConfig) {
|
||||||
config.certspath = certspath
|
config.certsPath = certspath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ClientCerts(clientKey, clientCert string) ClientOption {
|
func ClientCerts(clientKey, clientCert string) ClientOption {
|
||||||
return func(config *RealisConfig) {
|
return func(config *RealisConfig) {
|
||||||
config.clientkey, config.clientcert = clientKey, clientCert
|
config.clientKey, config.clientCert = clientKey, clientCert
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,7 +205,7 @@ func NewRealisClient(options ...ClientOption) (*RealisClient, error) {
|
||||||
config := &RealisConfig{}
|
config := &RealisConfig{}
|
||||||
|
|
||||||
// Default configs
|
// Default configs
|
||||||
config.timeoutms = 10000
|
config.timeout = 10 * time.Second
|
||||||
config.backoff = defaultBackoff
|
config.backoff = defaultBackoff
|
||||||
config.logger = &LevelLogger{log.New(os.Stdout, "realis: ", log.Ltime|log.Ldate|log.LUTC), false}
|
config.logger = &LevelLogger{log.New(os.Stdout, "realis: ", log.Ltime|log.Ldate|log.LUTC), false}
|
||||||
|
|
||||||
|
@ -266,7 +266,7 @@ func NewRealisClient(options ...ClientOption) (*RealisClient, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.jsonTransport {
|
if config.jsonTransport {
|
||||||
trans, err := newTJSONTransport(url, config.timeoutms, config)
|
trans, err := newTJSONTransport(url, config.timeout, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, NewTemporaryError(errors.Wrap(err, "Error creating realis"))
|
return nil, NewTemporaryError(errors.Wrap(err, "Error creating realis"))
|
||||||
}
|
}
|
||||||
|
@ -274,7 +274,7 @@ func NewRealisClient(options ...ClientOption) (*RealisClient, error) {
|
||||||
config.protoFactory = thrift.NewTJSONProtocolFactory()
|
config.protoFactory = thrift.NewTJSONProtocolFactory()
|
||||||
|
|
||||||
} else if config.binTransport {
|
} else if config.binTransport {
|
||||||
trans, err := newTBinTransport(url, config.timeoutms, config)
|
trans, err := newTBinTransport(url, config.timeout, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, NewTemporaryError(errors.Wrap(err, "Error creating realis"))
|
return nil, NewTemporaryError(errors.Wrap(err, "Error creating realis"))
|
||||||
}
|
}
|
||||||
|
@ -310,15 +310,15 @@ func GetDefaultClusterFromZKUrl(zkurl string) *Cluster {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCerts(certpath string) (*x509.CertPool, error) {
|
func GetCerts(certPath string) (*x509.CertPool, error) {
|
||||||
globalRootCAs := x509.NewCertPool()
|
globalRootCAs := x509.NewCertPool()
|
||||||
caFiles, err := ioutil.ReadDir(certpath)
|
caFiles, err := ioutil.ReadDir(certPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, cert := range caFiles {
|
for _, cert := range caFiles {
|
||||||
capathfile := filepath.Join(certpath, cert.Name())
|
caPathFile := filepath.Join(certPath, cert.Name())
|
||||||
caCert, err := ioutil.ReadFile(capathfile)
|
caCert, err := ioutil.ReadFile(caPathFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -328,33 +328,35 @@ func GetCerts(certpath string) (*x509.CertPool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a default Thrift Transport object for communications in gorealis using an HTTP Post Client
|
// Creates a default Thrift Transport object for communications in gorealis using an HTTP Post Client
|
||||||
func defaultTTransport(urlstr string, timeoutms int, config *RealisConfig) (thrift.TTransport, error) {
|
func defaultTTransport(url string, timeout time.Duration, config *RealisConfig) (thrift.TTransport, error) {
|
||||||
|
var transport http.Transport
|
||||||
|
|
||||||
jar, err := cookiejar.New(nil)
|
jar, err := cookiejar.New(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &thrift.THttpClient{}, errors.Wrap(err, "Error creating Cookie Jar")
|
return nil, errors.Wrap(err, "Error creating Cookie Jar")
|
||||||
}
|
}
|
||||||
var transport http.Transport
|
|
||||||
if config != nil {
|
if config != nil {
|
||||||
tlsConfig := &tls.Config{}
|
tlsConfig := &tls.Config{}
|
||||||
if config.InsecureSkipVerify {
|
if config.InsecureSkipVerify {
|
||||||
tlsConfig.InsecureSkipVerify = true
|
tlsConfig.InsecureSkipVerify = true
|
||||||
}
|
}
|
||||||
if config.certspath != "" {
|
if config.certsPath != "" {
|
||||||
rootCAs, err := GetCerts(config.certspath)
|
rootCAs, err := GetCerts(config.certsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.logger.Println("error occured couldn't fetch certs")
|
config.logger.Println("error occured couldn't fetch certs")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tlsConfig.RootCAs = rootCAs
|
tlsConfig.RootCAs = rootCAs
|
||||||
}
|
}
|
||||||
if config.clientkey != "" && config.clientcert == "" {
|
if config.clientKey != "" && config.clientCert == "" {
|
||||||
return nil, fmt.Errorf("have to provide both client key,cert. Only client key provided ")
|
return nil, fmt.Errorf("have to provide both client key,cert. Only client key provided ")
|
||||||
}
|
}
|
||||||
if config.clientkey == "" && config.clientcert != "" {
|
if config.clientKey == "" && config.clientCert != "" {
|
||||||
return nil, fmt.Errorf("have to provide both client key,cert. Only client cert provided ")
|
return nil, fmt.Errorf("have to provide both client key,cert. Only client cert provided ")
|
||||||
}
|
}
|
||||||
if config.clientkey != "" && config.clientcert != "" {
|
if config.clientKey != "" && config.clientCert != "" {
|
||||||
cert, err := tls.LoadX509KeyPair(config.clientcert, config.clientkey)
|
cert, err := tls.LoadX509KeyPair(config.clientCert, config.clientKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.logger.Println("error occured loading client certs and keys")
|
config.logger.Println("error occured loading client certs and keys")
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -364,15 +366,15 @@ func defaultTTransport(urlstr string, timeoutms int, config *RealisConfig) (thri
|
||||||
transport.TLSClientConfig = tlsConfig
|
transport.TLSClientConfig = tlsConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
trans, err := thrift.NewTHttpPostClientWithOptions(urlstr+"/api",
|
trans, err := thrift.NewTHttpPostClientWithOptions(url+"/api",
|
||||||
thrift.THttpClientOptions{Client: &http.Client{Timeout: time.Millisecond * time.Duration(timeoutms), Transport: &transport, Jar: jar}})
|
thrift.THttpClientOptions{Client: &http.Client{Timeout: timeout, Transport: &transport, Jar: jar}})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &thrift.THttpClient{}, errors.Wrap(err, "Error creating transport")
|
return nil, errors.Wrap(err, "Error creating transport")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := trans.Open(); err != nil {
|
if err := trans.Open(); err != nil {
|
||||||
return &thrift.THttpClient{}, errors.Wrapf(err, "Error opening connection to %s", urlstr)
|
return nil, errors.Wrapf(err, "Error opening connection to %s", url)
|
||||||
}
|
}
|
||||||
|
|
||||||
return trans, nil
|
return trans, nil
|
||||||
|
@ -380,13 +382,13 @@ func defaultTTransport(urlstr string, timeoutms int, config *RealisConfig) (thri
|
||||||
|
|
||||||
// Create a default configuration of the transport layer, requires a URL to test connection with.
|
// Create a default configuration of the transport layer, requires a URL to test connection with.
|
||||||
// Uses HTTP Post as transport layer and Thrift JSON as the wire protocol by default.
|
// Uses HTTP Post as transport layer and Thrift JSON as the wire protocol by default.
|
||||||
func newDefaultConfig(url string, timeoutms int, config *RealisConfig) (*RealisConfig, error) {
|
func newDefaultConfig(url string, timeout time.Duration, config *RealisConfig) (*RealisConfig, error) {
|
||||||
return newTJSONConfig(url, timeoutms, config)
|
return newTJSONConfig(url, timeout, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a realis config object using HTTP Post and Thrift JSON protocol to communicate with Aurora.
|
// Creates a realis config object using HTTP Post and Thrift JSON protocol to communicate with Aurora.
|
||||||
func newTJSONConfig(url string, timeoutms int, config *RealisConfig) (*RealisConfig, error) {
|
func newTJSONConfig(url string, timeout time.Duration, config *RealisConfig) (*RealisConfig, error) {
|
||||||
trans, err := defaultTTransport(url, timeoutms, config)
|
trans, err := defaultTTransport(url, timeout, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &RealisConfig{}, errors.Wrap(err, "Error creating realis config")
|
return &RealisConfig{}, errors.Wrap(err, "Error creating realis config")
|
||||||
}
|
}
|
||||||
|
@ -399,8 +401,8 @@ func newTJSONConfig(url string, timeoutms int, config *RealisConfig) (*RealisCon
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a realis config config using HTTP Post and Thrift Binary protocol to communicate with Aurora.
|
// Creates a realis config config using HTTP Post and Thrift Binary protocol to communicate with Aurora.
|
||||||
func newTBinaryConfig(url string, timeoutms int, config *RealisConfig) (*RealisConfig, error) {
|
func newTBinaryConfig(url string, timeout time.Duration, config *RealisConfig) (*RealisConfig, error) {
|
||||||
trans, err := defaultTTransport(url, timeoutms, config)
|
trans, err := defaultTTransport(url, timeout, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &RealisConfig{}, errors.Wrap(err, "Error creating realis config")
|
return &RealisConfig{}, errors.Wrap(err, "Error creating realis config")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue