Skip to content

Commit 4493dcd

Browse files
committed
Add comments for robot cache refresh behavior
1 parent 5b3502b commit 4493dcd

5 files changed

Lines changed: 9 additions & 0 deletions

File tree

hcloud/instances_util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func getRobotServerByName(c robot.Client, node *corev1.Node) (server *hrobotmode
103103
}
104104
}
105105

106+
// No matching Robot server exists for this node, even after refreshing the cached list.
106107
return nil, nil
107108
}
108109

internal/robot/adapter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
hrobotmodels "github.com/syself/hrobot-go/models"
66
)
77

8+
// adapter wraps hrobot.RobotClient so it satisfies this package's Client interface.
89
type adapter struct {
910
hrobot.RobotClient
1011
}

internal/robot/cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type cacheRobotClient struct {
2020
servers []hrobotmodels.Server
2121
serversByID map[int]*hrobotmodels.Server
2222

23+
// forcedRefreshServerNames tracks which node names already triggered a forced refresh within the current cache timeout window.
2324
forcedRefreshServerNames map[string]time.Time
2425
}
2526

@@ -62,6 +63,7 @@ func (c *cacheRobotClient) ServerGetList() ([]hrobotmodels.Server, error) {
6263
return c.servers, nil
6364
}
6465

66+
// ServerGetListForceRefresh refreshes the server list immediately, unless the same node already forced a refresh within the current cache timeout window.
6567
func (c *cacheRobotClient) ServerGetListForceRefresh(nodeName string) ([]hrobotmodels.Server, error) {
6668
c.mutex.Lock()
6769
defer c.mutex.Unlock()
@@ -114,6 +116,7 @@ func (c *cacheRobotClient) refreshCache() error {
114116
return nil
115117
}
116118

119+
// nodeHasAlreadyForcedRefresh reports whether this node already triggered a forced refresh within the current cache timeout window and drops expired entries.
117120
func (c *cacheRobotClient) nodeHasAlreadyForcedRefresh(nodeName string) bool {
118121
forcedAt, found := c.forcedRefreshServerNames[nodeName]
119122
if !found {
@@ -128,6 +131,7 @@ func (c *cacheRobotClient) nodeHasAlreadyForcedRefresh(nodeName string) bool {
128131
return true
129132
}
130133

134+
// currentTime centralizes access to the clock so tests can inject a deterministic time source via c.now.
131135
func (c *cacheRobotClient) currentTime() time.Time {
132136
if c.now == nil {
133137
return time.Now()

internal/robot/cache_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/mocks"
1111
)
1212

13+
// TestCacheClientServerGetListForceRefreshSkipsRepeatedNameWithinTimeout verifies that repeated forced refreshes for the same node name reuse the cached result until the timeout expires.
1314
func TestCacheClientServerGetListForceRefreshSkipsRepeatedNameWithinTimeout(t *testing.T) {
1415
now := time.Date(2026, time.April, 7, 13, 0, 0, 0, time.UTC)
1516
mockClient := &mocks.RobotClient{}
@@ -36,6 +37,7 @@ func TestCacheClientServerGetListForceRefreshSkipsRepeatedNameWithinTimeout(t *t
3637
mockClient.AssertNumberOfCalls(t, "ServerGetList", 1)
3738
}
3839

40+
// TestCacheClientServerGetListForceRefreshExpiresPerName verifies that a node name can trigger another forced refresh after its timeout window has expired.
3941
func TestCacheClientServerGetListForceRefreshExpiresPerName(t *testing.T) {
4042
now := time.Date(2026, time.April, 7, 13, 0, 0, 0, time.UTC)
4143
mockClient := &mocks.RobotClient{}

internal/robot/ratelimit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (c *rateLimitClient) ServerGetList() ([]hrobotmodels.Server, error) {
4444
return servers, err
4545
}
4646

47+
// ServerGetListForceRefresh applies the same rate-limit guard to forced refreshes before delegating to the wrapped client.
4748
func (c *rateLimitClient) ServerGetListForceRefresh(nodeName string) ([]hrobotmodels.Server, error) {
4849
if c.isExceeded() {
4950
return nil, c.getRateLimitError()

0 commit comments

Comments
 (0)