From 70d28fa7c4733aa5d15b195b56beeae19c3b55a7 Mon Sep 17 00:00:00 2001 From: Seraphim Strub Date: Thu, 8 Aug 2024 18:15:31 +0000 Subject: [PATCH] feature: switch to nested struct and overwriting for cached versions --- pkg/abuseipdb/client.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/abuseipdb/client.go b/pkg/abuseipdb/client.go index 45c4f65..49d549b 100644 --- a/pkg/abuseipdb/client.go +++ b/pkg/abuseipdb/client.go @@ -66,6 +66,7 @@ const ( IotTargeted ) +// Client holds all the information to communicate with abuseipdb type Client struct { client *http.Client BaseURL *url.URL @@ -75,7 +76,11 @@ type Client struct { // ratelimit RateLimit *rate.Limit +} +// CachedClient wraps the Client and overwrites certain methods +type CachedClient struct { + *Client cache *bigcache.BigCache } @@ -95,9 +100,13 @@ func (c *Client) AddApiKey(key string) { c.APIKey = key } -// AddCache with "ttl", suggestion 30min -func (c *Client) AddCache(eviction time.Duration) { - c.cache, _ = bigcache.New(context.Background(), bigcache.DefaultConfig(eviction)) +// Cached returns CachedClient with "ttl", suggestion 30min +func (c *Client) Cached(eviction time.Duration) *CachedClient { + cache, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(eviction)) + return &CachedClient{ + Client: c, + cache: cache, + } } func (c *Client) initialize() { @@ -250,11 +259,8 @@ func (c *Client) Check(ctx context.Context, ip net.IP, opts *CheckOptions) (*Che return &result, nil } -// CheckCached same as Check but uses the client cache and the ip as a key (cache has to be added to client for this to work) -func (c *Client) CheckCached(ctx context.Context, ip net.IP, opts *CheckOptions) (*CheckResult, error) { - if c.cache == nil { - return nil, errors.New("cache is missing") - } +// Check uses the client cache and the ip as a key (cache has to be added to client for this to work) +func (c *CachedClient) Check(ctx context.Context, ip net.IP, opts *CheckOptions) (*CheckResult, error) { key := fmt.Sprintf("%s:%s", endpointCheck, ip.String()) if r, err := c.cache.Get(key); err == nil { result := CheckResult{} @@ -262,7 +268,7 @@ func (c *Client) CheckCached(ctx context.Context, ip net.IP, opts *CheckOptions) return &result, nil } } - if result, err := c.Check(ctx, ip, opts); err == nil { + if result, err := c.Client.Check(ctx, ip, opts); err == nil { resultEncoded := bytes.Buffer{} if jsonErr := json.NewEncoder(&resultEncoded).Encode(result); jsonErr != nil { return nil, jsonErr @@ -516,11 +522,8 @@ func (c *Client) CheckBlock(ctx context.Context, ipnNet *net.IPNet, opts *CheckB return &result, nil } -// CheckBlockCached same as CheckBlock but uses the client cache and the ipnet as a key (cache has to be added to client for this to work) -func (c *Client) CheckBlockCached(ctx context.Context, ipNet *net.IPNet, opts *CheckBlockOptions) (*CheckBlockResult, error) { - if c.cache == nil { - return nil, errors.New("cache is missing") - } +// CheckBlock uses the client cache and the ipnet as a key (cache has to be added to client for this to work) +func (c *CachedClient) CheckBlock(ctx context.Context, ipNet *net.IPNet, opts *CheckBlockOptions) (*CheckBlockResult, error) { key := fmt.Sprintf("%s:%s", endpointCheck, ipNet.String()) if r, err := c.cache.Get(key); err == nil { result := CheckBlockResult{} @@ -528,7 +531,7 @@ func (c *Client) CheckBlockCached(ctx context.Context, ipNet *net.IPNet, opts *C return &result, nil } } - if result, err := c.CheckBlock(ctx, ipNet, opts); err == nil { + if result, err := c.Client.CheckBlock(ctx, ipNet, opts); err == nil { resultEncoded := bytes.Buffer{} if jsonErr := json.NewEncoder(&resultEncoded).Encode(result); jsonErr != nil { return nil, jsonErr