Skip to content

Commit 2351eb7

Browse files
committed
refresh session when reuse cached client
1 parent 1d00af3 commit 2351eb7

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

proxmox/service.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package proxmox
22

33
import (
4+
"context"
45
"crypto/sha256"
56
"crypto/tls"
67
"errors"
@@ -52,7 +53,17 @@ func GetOrCreateService(params Params) (*Service, error) {
5253

5354
key := retrieveSessionKey(params)
5455
if cachedSession, ok := sessionCache.Load(key); ok {
55-
return cachedSession.(*Service), nil
56+
svc := cachedSession.(*Service)
57+
if err := svc.restclient.MakeNewSession(context.Background()); err != nil {
58+
// failed to refresh session. just try to create new svc
59+
s, err := NewService(params)
60+
if err != nil {
61+
return nil, err
62+
}
63+
sessionCache.Store(key, s)
64+
return s, nil
65+
}
66+
return svc, nil
5667
}
5768

5869
s, err := NewService(params)

rest/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func NewRESTClient(baseUrl string, opts ...ClientOption) (*RESTClient, error) {
6464
if client.token == "" && client.session == nil && client.credentials != nil {
6565
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Minute)
6666
defer cancel()
67-
if err := client.makeNewSession(ctx); err != nil {
67+
if err := client.MakeNewSession(ctx); err != nil {
6868
return nil, err
6969
}
7070
}
@@ -209,7 +209,7 @@ func (c *RESTClient) makeAuthHeaders() http.Header {
209209
return header
210210
}
211211

212-
func (c *RESTClient) makeNewSession(ctx context.Context) error {
212+
func (c *RESTClient) MakeNewSession(ctx context.Context) error {
213213
var err error
214214
c.session, err = c.PostTicket(ctx, *c.credentials)
215215
if err != nil {

0 commit comments

Comments
 (0)