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:
6543 2020-11-13 09:03:33 +08:00
parent aa13606bc6
commit 60293eb2d1
2 changed files with 28 additions and 40 deletions

View file

@ -38,7 +38,7 @@ type Client struct {
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

View file

@ -22,15 +22,9 @@ 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 {
c.versionLock.RUnlock()
if err := c.loadClientServerVersion(); err != nil {
return err return err
} }
} else {
c.versionLock.RUnlock()
}
check, err := version.NewConstraint(constraint) check, err := version.NewConstraint(constraint)
if err != nil { if err != nil {
@ -51,15 +45,9 @@ 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 {
c.versionLock.RUnlock()
if err := c.loadClientServerVersion(); err != nil {
return err return err
} }
} else {
c.versionLock.RUnlock()
}
if !c.serverVersion.GreaterThanOrEqual(v) { if !c.serverVersion.GreaterThanOrEqual(v) {
return fmt.Errorf("gitea server at %s is older than %s", c.url, v.Original()) return fmt.Errorf("gitea server at %s is older than %s", c.url, v.Original())
@ -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 err return
} }
return nil })
return
} }