|
9 | 9 | "fmt" |
10 | 10 | "io" |
11 | 11 | "regexp" |
| 12 | + "time" |
12 | 13 |
|
13 | 14 | keybase1 "github.com/keybase/client/go/protocol/keybase1" |
14 | 15 | stellar1 "github.com/keybase/client/go/protocol/stellar1" |
@@ -824,6 +825,75 @@ func (u *User) IsCachedIdentifyFresh(upk *keybase1.UserPlusKeysV2AllIncarnations |
824 | 825 | return true |
825 | 826 | } |
826 | 827 |
|
| 828 | +func LoadHasRandomPw(mctx MetaContext, arg keybase1.LoadHasRandomPwArg) (res bool, err error) { |
| 829 | + mctx = mctx.WithLogTag("HASRPW") |
| 830 | + defer mctx.TraceTimed(fmt.Sprintf("User#LoadHasRandomPw(forceRepoll=%t)", arg.ForceRepoll), func() error { return err })() |
| 831 | + |
| 832 | + cacheKey := DbKey{ |
| 833 | + Typ: DBHasRandomPW, |
| 834 | + Key: mctx.ActiveDevice().UID().String(), |
| 835 | + } |
| 836 | + |
| 837 | + var cachedValue, hasCache bool |
| 838 | + if !arg.ForceRepoll { |
| 839 | + if hasCache, err = mctx.G().GetKVStore().GetInto(&cachedValue, cacheKey); err == nil { |
| 840 | + if hasCache && !cachedValue { |
| 841 | + mctx.Debug("Returning HasRandomPW=false from KVStore cache") |
| 842 | + return false, nil |
| 843 | + } |
| 844 | + // If it was never cached or user *IS* RandomPW right now, pass through |
| 845 | + // and call the API. |
| 846 | + } else { |
| 847 | + mctx.Debug("Unable to get cached value for HasRandomPW: %v", err) |
| 848 | + } |
| 849 | + } |
| 850 | + |
| 851 | + var initialTimeout time.Duration |
| 852 | + if !arg.ForceRepoll { |
| 853 | + // If we are do not need accurate response from the API server, make |
| 854 | + // the request with a timeout for quicker overall RPC response time |
| 855 | + // if network is bad/unavailable. |
| 856 | + initialTimeout = 3 * time.Second |
| 857 | + } |
| 858 | + |
| 859 | + var ret struct { |
| 860 | + AppStatusEmbed |
| 861 | + RandomPW bool `json:"random_pw"` |
| 862 | + } |
| 863 | + err = mctx.G().API.GetDecode(mctx, APIArg{ |
| 864 | + Endpoint: "user/has_random_pw", |
| 865 | + SessionType: APISessionTypeREQUIRED, |
| 866 | + InitialTimeout: initialTimeout, |
| 867 | + }, &ret) |
| 868 | + if err != nil { |
| 869 | + if !arg.ForceRepoll { |
| 870 | + if hasCache { |
| 871 | + // We are allowed to return cache if we have any. |
| 872 | + mctx.Warning("Unable to make a network request to has_random_pw. Returning cached value: %t", cachedValue) |
| 873 | + return cachedValue, nil |
| 874 | + } |
| 875 | + |
| 876 | + mctx.Warning("Unable to make a network request to has_random_pw and there is no cache. Erroring out.") |
| 877 | + } |
| 878 | + return res, err |
| 879 | + } |
| 880 | + |
| 881 | + if !hasCache || cachedValue != ret.RandomPW { |
| 882 | + // Cache current state. If we put `randomPW=false` in the cache, we will never |
| 883 | + // ever have to call to the network from this device, because it's not possible |
| 884 | + // to become `randomPW=true` again. If we cache `randomPW=true` we are going to |
| 885 | + // keep asking the network, but we will be resilient to bad network conditions |
| 886 | + // because we will have this cached state to fall back on. |
| 887 | + if err := mctx.G().GetKVStore().PutObj(cacheKey, nil, ret.RandomPW); err == nil { |
| 888 | + mctx.Debug("Adding HasRandomPW=%t to KVStore", ret.RandomPW) |
| 889 | + } else { |
| 890 | + mctx.Debug("Unable to add HasRandomPW state to KVStore") |
| 891 | + } |
| 892 | + } |
| 893 | + |
| 894 | + return ret.RandomPW, err |
| 895 | +} |
| 896 | + |
827 | 897 | // PartialCopy copies some fields of the User object, but not all. |
828 | 898 | // For instance, it doesn't copy the SigChain or IDTable, and it only |
829 | 899 | // makes a shallow copy of the ComputedKeyFamily. |
|
0 commit comments