-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from syself/cache-client
✨ Add cache client for Hetzner robot
- Loading branch information
Showing
18 changed files
with
157 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/syself/hetzner-cloud-controller-manager/internal/robot/client" | ||
hrobot "github.com/syself/hrobot-go" | ||
"github.com/syself/hrobot-go/models" | ||
) | ||
|
||
var handler = &cacheRobotClient{} | ||
|
||
type cacheRobotClient struct { | ||
robotClient hrobot.RobotClient | ||
timeout time.Duration | ||
|
||
lastUpdate time.Time | ||
|
||
// cache | ||
l []models.Server | ||
m map[int]*models.Server | ||
} | ||
|
||
func NewClient(robotClient hrobot.RobotClient, cacheTimeout time.Duration) client.Client { | ||
handler.timeout = cacheTimeout | ||
handler.robotClient = robotClient | ||
return handler | ||
} | ||
|
||
func (c *cacheRobotClient) ServerGet(id int) (*models.Server, error) { | ||
if c.shouldSync() { | ||
server, err := c.robotClient.ServerGet(id) | ||
if err != nil { | ||
return server, err | ||
} | ||
|
||
// Add server to cache in case there is another Get call - but do not update time of last update. | ||
// That only makes sense for list calls. | ||
c.m[server.ServerNumber] = server | ||
return server, nil | ||
} | ||
|
||
server, found := c.m[id] | ||
if !found { | ||
// return not found error | ||
return nil, models.Error{Code: models.ErrorCodeServerNotFound, Message: "server not found"} | ||
} | ||
|
||
return server, nil | ||
} | ||
|
||
func (c *cacheRobotClient) ServerGetList() ([]models.Server, error) { | ||
if c.shouldSync() { | ||
list, err := c.robotClient.ServerGetList() | ||
if err != nil { | ||
return list, err | ||
} | ||
|
||
// populate list | ||
c.l = list | ||
|
||
// remove all entries from map and populate it freshly | ||
c.m = make(map[int]*models.Server) | ||
for i, server := range list { | ||
c.m[server.ServerNumber] = &list[i] | ||
} | ||
|
||
// set time of last update | ||
c.lastUpdate = time.Now() | ||
} | ||
|
||
return c.l, nil | ||
} | ||
|
||
func (c *cacheRobotClient) shouldSync() bool { | ||
// map is nil means we have no cached value yet | ||
if c.m == nil { | ||
c.m = make(map[int]*models.Server) | ||
return true | ||
} | ||
if time.Now().After(c.lastUpdate.Add(c.timeout)) { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package client | ||
|
||
import "github.com/syself/hrobot-go/models" | ||
|
||
type Client interface { | ||
ServerGet(id int) (*models.Server, error) | ||
ServerGetList() ([]models.Server, error) | ||
} |
Oops, something went wrong.