rpcclient: Refactor cookie caching

This commit is contained in:
JeremyRand 2019-10-09 11:23:05 +00:00 committed by John C. Vernaleo
parent 280845a8a4
commit 915788b8e6
2 changed files with 28 additions and 42 deletions

View File

@ -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
}
}

View File

@ -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