54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package panel
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// aaPanel caches Go release lists until force=true is supplied. Refresh that
|
|
// metadata at most once a minute; installed SDKs still come from its real ls.
|
|
func (c *Client) goSDKInventory(ctx context.Context, refresh bool) ([]byte, error) {
|
|
c.goMetadataMu.Lock()
|
|
force := refresh && time.Now().After(c.goRefreshAfter)
|
|
if force {
|
|
c.goRefreshAfter = time.Now().Add(time.Minute)
|
|
}
|
|
cached, measured := append([]byte(nil), c.goLastGood...), c.goLastGoodAt
|
|
c.goMetadataMu.Unlock()
|
|
valid := func(body []byte, err error) bool {
|
|
var state struct {
|
|
Status any `json:"status"`
|
|
}
|
|
return err == nil && json.Unmarshal(body, &state) == nil && !failed(state.Status)
|
|
}
|
|
body, err := c.request(ctx, http.MethodPost, "/project/go/list_go_sdk/stype", nil)
|
|
good := valid(body, err)
|
|
if force && ctx.Err() == nil {
|
|
updated, updateErr := c.request(ctx, http.MethodPost, "/project/go/list_go_sdk/stype", url.Values{"force": {"true"}})
|
|
if valid(updated, updateErr) {
|
|
body, err, good = updated, nil, true
|
|
}
|
|
}
|
|
if good {
|
|
c.goMetadataMu.Lock()
|
|
c.goLastGood = append([]byte(nil), body...)
|
|
c.goLastGoodAt = time.Now()
|
|
c.goMetadataMu.Unlock()
|
|
return body, nil
|
|
}
|
|
if ctx.Err() != nil {
|
|
return nil, ctx.Err()
|
|
}
|
|
if len(cached) > 0 && time.Since(measured) < time.Minute {
|
|
return cached, nil
|
|
}
|
|
if err == nil {
|
|
err = errors.New("Go SDK metadata is temporarily unavailable")
|
|
}
|
|
return nil, err
|
|
}
|