@@ -2,50 +2,55 @@ package provision
22
33import (
44 "context"
5- "crypto/ed25519"
65 "fmt"
6+ "time"
77
8- lru "github.com/hashicorp/golang-lru "
8+ cache "github.com/patrickmn/go-cache "
99 "github.com/pkg/errors"
10+ "github.com/rs/zerolog/log"
1011 "github.com/threefoldtech/zosbase/pkg/stubs"
1112)
1213
14+ const (
15+ keyExpiration = 60 * time .Minute
16+ keyCleanup = 10 * time .Minute
17+ )
18+
1319type substrateTwins struct {
1420 substrateGateway * stubs.SubstrateGatewayStub
15- mem * lru .Cache
21+ mem * cache .Cache
1622}
1723
1824// NewSubstrateTwins creates a substrate users db that implements the provision.Users interface.
1925func NewSubstrateTwins (substrateGateway * stubs.SubstrateGatewayStub ) (Twins , error ) {
20- cache , err := lru .New (1024 )
21- if err != nil {
22- return nil , err
23- }
24-
2526 return & substrateTwins {
2627 substrateGateway : substrateGateway ,
27- mem : cache ,
28+ mem : cache . New ( keyExpiration , keyCleanup ) ,
2829 }, nil
2930}
3031
3132// GetKey gets twins public key
3233func (s * substrateTwins ) GetKey (id uint32 ) ([]byte , error ) {
33- if value , ok := s .mem .Get (id ); ok {
34+ cacheKey := fmt .Sprint (id )
35+ if value , ok := s .mem .Get (cacheKey ); ok {
3436 return value .([]byte ), nil
3537 }
38+
39+ log .Debug ().Uint32 ("twin" , id ).Msg ("twin public key cache expired, fetching from substrate" )
3640 user , err := s .substrateGateway .GetTwin (context .Background (), id )
3741 if err != nil {
3842 return nil , errors .Wrapf (err , "could not get user with id '%d'" , id )
3943 }
4044
41- key := user .Account .PublicKey ()
42- s .mem .Add ( id , key )
43- return key , nil
45+ pk := user .Account .PublicKey ()
46+ s .mem .Set ( cacheKey , pk , cache . DefaultExpiration )
47+ return pk , nil
4448}
4549
4650type substrateAdmins struct {
47- twin uint32
48- pk ed25519.PublicKey
51+ substrateGateway * stubs.SubstrateGatewayStub
52+ twin uint32
53+ mem * cache.Cache
4954}
5055
5156// NewSubstrateAdmins creates a substrate twins db that implements the provision.Users interface.
@@ -56,13 +61,10 @@ func NewSubstrateAdmins(substrateGateway *stubs.SubstrateGatewayStub, farmID uin
5661 return nil , errors .Wrap (err , "failed to get farm" )
5762 }
5863
59- twin , err := substrateGateway .GetTwin (context .Background (), uint32 (farm .TwinID ))
60- if err != nil {
61- return nil , err
62- }
6364 return & substrateAdmins {
64- twin : uint32 (farm .TwinID ),
65- pk : twin .Account .PublicKey (),
65+ substrateGateway : substrateGateway ,
66+ twin : uint32 (farm .TwinID ),
67+ mem : cache .New (keyExpiration , keyCleanup ),
6668 }, nil
6769}
6870
@@ -72,5 +74,18 @@ func (s *substrateAdmins) GetKey(id uint32) ([]byte, error) {
7274 return nil , fmt .Errorf ("twin with id '%d' is not an admin" , id )
7375 }
7476
75- return []byte (s .pk ), nil
77+ cacheKey := fmt .Sprint (id )
78+ if value , ok := s .mem .Get (cacheKey ); ok {
79+ return value .([]byte ), nil
80+ }
81+
82+ log .Debug ().Uint32 ("twin" , id ).Msg ("admin public key cache expired, fetching from substrate" )
83+ twin , err := s .substrateGateway .GetTwin (context .Background (), id )
84+ if err != nil {
85+ return nil , errors .Wrapf (err , "could not get admin twin with id '%d'" , id )
86+ }
87+
88+ pk := twin .Account .PublicKey ()
89+ s .mem .Set (cacheKey , pk , cache .DefaultExpiration )
90+ return pk , nil
7691}
0 commit comments