Skip to content

Commit

Permalink
update fml
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakuhito committed Apr 12, 2023
1 parent 00e27ce commit 5f26074
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
1 change: 1 addition & 0 deletions fml/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/goccy/go-json v0.10.1
github.com/gofiber/fiber/v2 v2.42.0
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
)

require (
Expand Down
1 change: 1 addition & 0 deletions fml/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
87 changes: 60 additions & 27 deletions fml/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main


import (
"github.com/gofiber/fiber/v2"
"github.com/goccy/go-json"
"golang.org/x/sync/singleflight"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -47,20 +48,50 @@ type CacheItem struct {
}

type Cache struct {
mu sync.Mutex
items map[string]*CacheItem
mu sync.Mutex
items map[string]*CacheItem
fetchGroup singleflight.Group
}

var cache = &Cache{
items: make(map[string]*CacheItem),
}

func (c *Cache) Get(key string) (*AllMempoolItemsResponse, bool) {
c.mu.Lock()
defer c.mu.Unlock()
func GetAllMempoolItemsResponse(request_url string) (AllMempoolItemsResponse, error) {
res, err := http.Get(request_url)
if err != nil {
return AllMempoolItemsResponse{}, err
}
defer res.Body.Close()

item, found := c.items[key]
if !found || time.Now().After(item.Expiry) {
return nil, false
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return AllMempoolItemsResponse{}, err
}

var resp AllMempoolItemsResponse
err = json.Unmarshal(resBody, &resp)
if err != nil {
return AllMempoolItemsResponse{}, err
}

return resp, nil
}

func (c *Cache) FetchAndUpdateCache(key string) (*AllMempoolItemsResponse, error) {
resp, err, _ := c.fetchGroup.Do(key, func() (interface{}, error) {
response, err := GetAllMempoolItemsResponse(key)
if err != nil {
return nil, err
}
c.Set(key, &response, 5*time.Second)
return &response, nil
})

if err != nil {
return nil, err
}
return item.Response, true
return resp.(*AllMempoolItemsResponse), nil
}

func (c *Cache) Set(key string, value *AllMempoolItemsResponse, duration time.Duration) {
Expand All @@ -73,8 +104,20 @@ func (c *Cache) Set(key string, value *AllMempoolItemsResponse, duration time.Du
}
}

var cache = &Cache{
items: make(map[string]*CacheItem),
func (c *Cache) Get(key string) (*AllMempoolItemsResponse, bool) {
c.mu.Lock()
item, found := c.items[key]
c.mu.Unlock()

if found && time.Now().After(item.Expiry) {
go func() {
if _, err := c.FetchAndUpdateCache(key); err != nil {
log.Printf("Error updating cache for key '%s': %v", key, err)
}
}()
}

return item.Response, found
}

func GetMempoolItemByParentCoinInfo(c *fiber.Ctx) error {
Expand All @@ -86,24 +129,14 @@ func GetMempoolItemByParentCoinInfo(c *fiber.Ctx) error {

cachedResponse, found := cache.Get(args.RequestURL)
if !found {
res, err := http.Get(args.RequestURL)
if err != nil {
return err
}
defer res.Body.Close()

resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}

var resp AllMempoolItemsResponse
err = json.Unmarshal(resBody, &resp)
resp, err := GetAllMempoolItemsResponse(args.RequestURL)
if err != nil {
return err
return c.JSON(fiber.Map{
"item": nil,
})
}

cache.Set(args.RequestURL, &resp, 10 * time.Second)
cache.Set(args.RequestURL, &resp, 5 * time.Second)
cachedResponse = &resp
}

Expand Down

0 comments on commit 5f26074

Please sign in to comment.