mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
fix(feishu): invalidate cached token on auth error to enable retry recovery (#1318)
The Lark SDK v3's built-in token retry loop does not clear stale tokens from cache when the server returns error 99991663 (tenant_access_token invalid), causing all API calls to fail until the token naturally expires (~2 hours). - Add tokenCache struct (implementing larkcore.Cache) with Get/Set/InvalidateAll methods and proper expired-entry cleanup - Wire custom cache into lark.NewClient via WithTokenCache() - Add invalidateTokenOnAuthError helper called in all API methods
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package feishu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// tokenCache implements larkcore.Cache with an extra InvalidateAll method.
|
||||
// This works around a bug in the Lark SDK v3 where the built-in token retry
|
||||
// loop does not clear stale tokens from cache on auth errors.
|
||||
type tokenCache struct {
|
||||
mu sync.RWMutex
|
||||
store map[string]*tokenEntry
|
||||
}
|
||||
|
||||
type tokenEntry struct {
|
||||
value string
|
||||
expireAt time.Time
|
||||
}
|
||||
|
||||
func newTokenCache() *tokenCache {
|
||||
return &tokenCache{store: make(map[string]*tokenEntry)}
|
||||
}
|
||||
|
||||
func (c *tokenCache) Set(_ context.Context, key, value string, ttl time.Duration) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.store[key] = &tokenEntry{value: value, expireAt: time.Now().Add(ttl)}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *tokenCache) Get(_ context.Context, key string) (string, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
e, ok := c.store[key]
|
||||
if !ok {
|
||||
return "", nil
|
||||
}
|
||||
if e.expireAt.Before(time.Now()) {
|
||||
delete(c.store, key)
|
||||
return "", nil
|
||||
}
|
||||
return e.value, nil
|
||||
}
|
||||
|
||||
// InvalidateAll removes all cached tokens, forcing fresh acquisition.
|
||||
func (c *tokenCache) InvalidateAll() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
clear(c.store)
|
||||
}
|
||||
Reference in New Issue
Block a user