Skip to content

Commit

Permalink
Adding support for Memcached
Browse files Browse the repository at this point in the history
Adding support for providing a url for Memcached either locally
or remotely.
  • Loading branch information
stephen-pl committed Aug 16, 2023
1 parent 887cbcd commit 822596a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
23 changes: 16 additions & 7 deletions pkg/resolver/location_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,34 @@ func GetPublicIPInfo(ctx context.Context, ip string, token string) (IPInfo, erro
}

type LocationResolver struct {
localCache *ttlcache.Cache[string, IPInfo]
localCache *ttlcache.Cache[string, IPInfo]
ipInfoToken string
remoteTTL int
}

type LocationCachePayload struct {
Ip string `json:"ip"`
Ip_payload []byte `json:"ip_payload"`
RemoteTTL int `json:"ttl"`
IP string `json:"ip"`
IPPayload []byte `json:"ipPayload"`
RemoteTTL int `json:"ttl"`
}

func NewLocationResolver(ipInfoToken string, localTTL time.Duration, remoteTTL int) LocationResolver {
localCache := ttlcache.New[string, IPInfo](
func NewLocationResolver(ipInfoToken string, localTTL time.Duration, remoteTTL int) LocationResolver {

Check failure on line 133 in pkg/resolver/location_resolver.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected NewLocationResolver, expecting (
localCache := ttlcache.New[string, IPInfo](
//nolint:gomnd
ttlcache.WithTTL[string, IPInfo](localTTL),
ttlcache.WithTTL[string, IPInfo](localTTL),
ttlcache.WithDisableTouchOnHit[string, IPInfo]())


return LocationResolver{
localCache,
localCache,
ipInfoToken,
remoteTTL,
remoteTTL,
}
}

Check failure on line 148 in pkg/resolver/location_resolver.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected newline in argument list; possibly missing comma or )

Expand All @@ -147,12 +154,14 @@ func (l LocationResolver) ResolveIP(ctx context.Context, ip net.IP) (IPInfo, err
return ipInfo.Value(), nil
}

// 90% of the time we will try to check the remote cache
if os.Getenv("IP_INFO_CACHE_URL") != "" && rand.Intn(10) != 0 {
if os.Getenv("IP_INFO_CACHE_URL") != "" {
response, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, os.Getenv("IP_INFO_CACHE_URL")+"/getIpInfo?ip="+ipString, nil)
var ipInfo IPInfo
if response.Response.StatusCode == http.StatusOK && response.Body != nil {
json.NewDecoder(response.Body).Decode(&ipInfo)
err := json.NewDecoder(response.Body).Decode(&ipInfo)
if err != nil {
return IPInfo{}, errors.Wrap(err, "failed to decode IP info")
}
return ipInfo, nil
}
}
Expand All @@ -174,7 +183,7 @@ func (l LocationResolver) ResolveIP(ctx context.Context, ip net.IP) (IPInfo, err
if err != nil {
return IPInfo{}, errors.Wrap(err, "Could not serialize IPInfo")
}
http.NewRequestWithContext(context.Background(), http.MethodPost, os.Getenv("IP_INFO_CACHE_URL"), bytes.NewReader(requestBody))
_, _ = http.NewRequestWithContext(context.Background(), http.MethodPost, os.Getenv("IP_INFO_CACHE_URL"), bytes.NewReader(requestBody))
}
return ipInfo, nil
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/resolver/provider_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/base64"
"encoding/json"
"math/rand"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -33,7 +32,7 @@ type MinerInfo struct {

type ProviderCachePayload struct {
Provider string `json:"provider"`
ProviderPayload []byte `json:"provider_payload"`
ProviderPayload []byte `json:"providerPayload"`
TTL int `json:"ttl" default:"3600"`
}

Expand Down Expand Up @@ -68,7 +67,7 @@ func (p *ProviderResolver) ResolveProvider(ctx context.Context, provider string)
return minerInfo.Value(), nil
}

if os.Getenv("PROVIDER_CACHE_URL") != "" && rand.Intn(5) == 0 {
if os.Getenv("PROVIDER_CACHE_URL") != "" {
response, _ := http.NewRequestWithContext(context.Background(), http.MethodGet,
os.Getenv("PROVIDER_CACHE_URL")+"/getProviderInfo?provider="+provider, nil)

Expand Down Expand Up @@ -109,7 +108,7 @@ func (p *ProviderResolver) ResolveProvider(ctx context.Context, provider string)
return MinerInfo{}, errors.Wrap(err, "Could not serialize MinerInfo")
}

http.NewRequestWithContext(context.Background(), http.MethodPost, os.Getenv("PROVIDER_CACHE_URL"), bytes.NewReader(requestBody))
_, _ = http.NewRequestWithContext(context.Background(), http.MethodPost, os.Getenv("PROVIDER_CACHE_URL"), bytes.NewReader(requestBody))
}

return *minerInfo, nil
Expand Down

0 comments on commit 822596a

Please sign in to comment.