Use sync.Once for loading ServerVersion (#456)
rename loadClientServerVersion -> loadServerVersion use sync.Once for loading ServerVersion Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/456 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-Authored-By: 6543 <6543@obermui.de> Co-Committed-By: 6543 <6543@obermui.de>
This commit is contained in:
parent
aa13606bc6
commit
60293eb2d1
2 changed files with 28 additions and 40 deletions
|
@ -28,17 +28,17 @@ func Version() string {
|
||||||
|
|
||||||
// Client represents a Gitea API client.
|
// Client represents a Gitea API client.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
url string
|
url string
|
||||||
accessToken string
|
accessToken string
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
otp string
|
otp string
|
||||||
sudo string
|
sudo string
|
||||||
debug bool
|
debug bool
|
||||||
client *http.Client
|
client *http.Client
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
serverVersion *version.Version
|
serverVersion *version.Version
|
||||||
versionLock sync.RWMutex
|
getVersionOnce sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response represents the gitea response
|
// Response represents the gitea response
|
||||||
|
|
|
@ -22,14 +22,8 @@ func (c *Client) ServerVersion() (string, *Response, error) {
|
||||||
// CheckServerVersionConstraint validates that the login's server satisfies a
|
// CheckServerVersionConstraint validates that the login's server satisfies a
|
||||||
// given version constraint such as ">= 1.11.0+dev"
|
// given version constraint such as ">= 1.11.0+dev"
|
||||||
func (c *Client) CheckServerVersionConstraint(constraint string) error {
|
func (c *Client) CheckServerVersionConstraint(constraint string) error {
|
||||||
c.versionLock.RLock()
|
if err := c.loadServerVersion(); err != nil {
|
||||||
if c.serverVersion == nil {
|
return err
|
||||||
c.versionLock.RUnlock()
|
|
||||||
if err := c.loadClientServerVersion(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c.versionLock.RUnlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
check, err := version.NewConstraint(constraint)
|
check, err := version.NewConstraint(constraint)
|
||||||
|
@ -51,14 +45,8 @@ var (
|
||||||
|
|
||||||
// checkServerVersionGreaterThanOrEqual is internally used to speed up things and ignore issues with prerelease
|
// checkServerVersionGreaterThanOrEqual is internally used to speed up things and ignore issues with prerelease
|
||||||
func (c *Client) checkServerVersionGreaterThanOrEqual(v *version.Version) error {
|
func (c *Client) checkServerVersionGreaterThanOrEqual(v *version.Version) error {
|
||||||
c.versionLock.RLock()
|
if err := c.loadServerVersion(); err != nil {
|
||||||
if c.serverVersion == nil {
|
return err
|
||||||
c.versionLock.RUnlock()
|
|
||||||
if err := c.loadClientServerVersion(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c.versionLock.RUnlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.serverVersion.GreaterThanOrEqual(v) {
|
if !c.serverVersion.GreaterThanOrEqual(v) {
|
||||||
|
@ -67,17 +55,17 @@ func (c *Client) checkServerVersionGreaterThanOrEqual(v *version.Version) error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadClientServerVersion init the serverVersion variable
|
// loadServerVersion init the serverVersion variable
|
||||||
func (c *Client) loadClientServerVersion() error {
|
func (c *Client) loadServerVersion() (err error) {
|
||||||
c.versionLock.Lock()
|
c.getVersionOnce.Do(func() {
|
||||||
defer c.versionLock.Unlock()
|
raw, _, err2 := c.ServerVersion()
|
||||||
|
if err2 != nil {
|
||||||
raw, _, err := c.ServerVersion()
|
err = err2
|
||||||
if err != nil {
|
return
|
||||||
return err
|
}
|
||||||
}
|
if c.serverVersion, err = version.NewVersion(raw); err != nil {
|
||||||
if c.serverVersion, err = version.NewVersion(raw); err != nil {
|
return
|
||||||
return err
|
}
|
||||||
}
|
})
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue