diff --git a/rpcclient/cookiefile.go b/rpcclient/cookiefile.go index 9311fbbf..b29e29b7 100644 --- a/rpcclient/cookiefile.go +++ b/rpcclient/cookiefile.go @@ -8,9 +8,7 @@ package rpcclient import ( "fmt" "io/ioutil" - "os" "strings" - "time" ) func readCookieFile(path string) (username, password string, err error) { @@ -29,36 +27,3 @@ func readCookieFile(path string) (username, password string, err error) { username, password = parts[0], parts[1] return } - -func cookieRetriever(path string) func() (username, password string, err error) { - lastCheckTime := time.Time{} - lastModTime := time.Time{} - - curUsername, curPassword := "", "" - var curError error - - doUpdate := func() { - if !lastCheckTime.IsZero() && time.Now().Before(lastCheckTime.Add(30*time.Second)) { - return - } - - lastCheckTime = time.Now() - - st, err := os.Stat(path) - if err != nil { - curError = err - return - } - - modTime := st.ModTime() - if !modTime.Equal(lastModTime) { - lastModTime = modTime - curUsername, curPassword, curError = readCookieFile(path) - } - } - - return func() (username, password string, err error) { - doUpdate() - return curUsername, curPassword, curError - } -} diff --git a/rpcclient/infrastructure.go b/rpcclient/infrastructure.go index fa9e00e2..42aeb49f 100644 --- a/rpcclient/infrastructure.go +++ b/rpcclient/infrastructure.go @@ -19,6 +19,7 @@ import ( "net" "net/http" "net/url" + "os" "strings" "sync" "sync/atomic" @@ -1106,9 +1107,11 @@ type ConnConfig struct { // instead of User and Pass if non-empty. CookiePath string - // retrieveCookie is a function that returns the cookie username and - // passphrase. - retrieveCookie func() (username, passphrase string, err error) + cookieLastCheckTime time.Time + cookieLastModTime time.Time + cookieLastUser string + cookieLastPass string + cookieLastErr error // Params is the string representing the network that the server // is running. If there is no parameter set in the config, then @@ -1174,12 +1177,30 @@ func (config *ConnConfig) getAuth() (username, passphrase string, err error) { return config.User, config.Pass, nil } - // Initialize the cookie retriever on first run. - if config.retrieveCookie == nil { - config.retrieveCookie = cookieRetriever(config.CookiePath) + return config.retrieveCookie() +} + +// retrieveCookie returns the cookie username and passphrase. +func (config *ConnConfig) retrieveCookie() (username, passphrase string, err error) { + if !config.cookieLastCheckTime.IsZero() && time.Now().Before(config.cookieLastCheckTime.Add(30*time.Second)) { + return config.cookieLastUser, config.cookieLastPass, config.cookieLastErr } - return config.retrieveCookie() + config.cookieLastCheckTime = time.Now() + + st, err := os.Stat(config.CookiePath) + if err != nil { + config.cookieLastErr = err + return config.cookieLastUser, config.cookieLastPass, config.cookieLastErr + } + + modTime := st.ModTime() + if !modTime.Equal(config.cookieLastModTime) { + config.cookieLastModTime = modTime + config.cookieLastUser, config.cookieLastPass, config.cookieLastErr = readCookieFile(config.CookiePath) + } + + return config.cookieLastUser, config.cookieLastPass, config.cookieLastErr } // newHTTPClient returns a new http client that is configured according to the