Skip to content

Commit

Permalink
remove fml
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakuhito committed Apr 18, 2023
1 parent 76597ec commit fb90abf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 95 deletions.
19 changes: 3 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,16 @@ WORKDIR /tibet
RUN apt-get update && \
apt-get install -y git supervisor

# Install Go 1.16
RUN wget -q https://golang.org/dl/go1.16.10.linux-amd64.tar.gz -O go1.16.tar.gz && \
tar -C /usr/local -xzf go1.16.tar.gz && \
rm go1.16.tar.gz

# Set up the Go environment
ENV GOPATH /go
ENV PATH $PATH:/usr/local/go/bin:$GOPATH/bin

# Cache-busting argument
ARG CACHE_DATE
ARG VERSION

# Clone the repository
RUN git clone https://github.com/Yakuhito/tibet.git .

# Compile and run the Go application
WORKDIR /tibet/fml
RUN go build -o fml main.go
RUN chmod +x fml

# Create and activate a virtual environment
WORKDIR /tibet
RUN python -m venv venv
Expand All @@ -43,9 +33,6 @@ RUN pip install -r api-requirements.txt
RUN pip install --extra-index-url https://pypi.chia.net/simple/ chia-internal-custody
RUN pip install --extra-index-url https://pypi.chia.net/simple/ chia-dev-tools

# Run server & fml (supervisord)
RUN mv supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Start supervisord to manage both the Go application and the Uvicorn server
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Start the Uvicorn server
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]

51 changes: 51 additions & 0 deletions Dockerfile.with-fml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Use the official Python image as the base image
FROM python:3.9

# Set the working directory
WORKDIR /tibet

# Install Git
RUN apt-get update && \
apt-get install -y git supervisor

# Install Go 1.16
RUN wget -q https://golang.org/dl/go1.16.10.linux-amd64.tar.gz -O go1.16.tar.gz && \
tar -C /usr/local -xzf go1.16.tar.gz && \
rm go1.16.tar.gz

# Set up the Go environment
ENV GOPATH /go
ENV PATH $PATH:/usr/local/go/bin:$GOPATH/bin

# Cache-busting argument
ARG CACHE_DATE

# Clone the repository
RUN git clone https://github.com/Yakuhito/tibet.git .

# Compile and run the Go application
WORKDIR /tibet/fml
RUN go build -o fml main.go
RUN chmod +x fml

# Create and activate a virtual environment
WORKDIR /tibet
RUN python -m venv venv
RUN /bin/bash -c "source venv/bin/activate"

# Install requirements from requirements.txt
RUN pip install -r requirements.txt

# Install requirements from api-requirements.txt
RUN pip install -r api-requirements.txt

# Run your additional pip install commands here
RUN pip install --extra-index-url https://pypi.chia.net/simple/ chia-internal-custody
RUN pip install --extra-index-url https://pypi.chia.net/simple/ chia-dev-tools

# Run server & fml (supervisord)
RUN mv supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Start supervisord to manage both the Go application and the Uvicorn server
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

Binary file modified fml/fml
Binary file not shown.
83 changes: 4 additions & 79 deletions fml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ 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"
)

type Coin struct {
Expand Down Expand Up @@ -42,21 +38,6 @@ type GetMempoolItemByParentCoinInfoArgs struct {
RequestURL string `json:"request_url"`
}

type CacheItem struct {
Response *AllMempoolItemsResponse
Expiry time.Time
}

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

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

func GetAllMempoolItemsResponse(request_url string) (AllMempoolItemsResponse, error) {
res, err := http.Get(request_url)
if err != nil {
Expand All @@ -78,80 +59,24 @@ func GetAllMempoolItemsResponse(request_url string) (AllMempoolItemsResponse, er
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 resp.(*AllMempoolItemsResponse), nil
}

func (c *Cache) Set(key string, value *AllMempoolItemsResponse, duration time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()

c.items[key] = &CacheItem{
Response: value,
Expiry: time.Now().Add(duration),
}
}

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 nil, false
}

return item.Response, found
}

func GetMempoolItemByParentCoinInfo(c *fiber.Ctx) error {
args := new(GetMempoolItemByParentCoinInfoArgs)

if err := c.BodyParser(args); err != nil {
return err
}

cachedResponse, found := cache.Get(args.RequestURL)
if !found {
resp, err := GetAllMempoolItemsResponse(args.RequestURL)
if err != nil {
return c.JSON(fiber.Map{
"item": nil,
})
}

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

if !cachedResponse.Success {
response, err := GetAllMempoolItemsResponse(args.RequestURL)
if err != nil {
return c.JSON(fiber.Map{
"item": nil,
})
}

var item SpendBundle
found = false
var found bool = false

for _, v := range cachedResponse.MempoolItems {
for _, v := range response.MempoolItems {
for _, cs := range v.SpendBundle.CoinSpends {
if cs.Coin.ParentCoinInfo == args.ParentCoinInfo {
found = true
Expand Down

0 comments on commit fb90abf

Please sign in to comment.