Skip to content

Commit

Permalink
Upgrade sbb service performance
Browse files Browse the repository at this point in the history
  • Loading branch information
k930503 committed Jan 22, 2025
1 parent 95dadad commit 4c3b947
Show file tree
Hide file tree
Showing 6 changed files with 1,070 additions and 426 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ build: ## Builds the binary locally into ./dist

.PHONY: build-docker
build-docker: ## Builds a docker image with the node binary
docker build -t zkevm-node -f ./Dockerfile .
sudo docker build -t zkevm-node -f ./Dockerfile .

.PHONY: build-docker-nc
build-docker-nc: ## Builds a docker image with the node binary - but without build cache
Expand Down
9 changes: 9 additions & 0 deletions log/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package log

const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
)
93 changes: 93 additions & 0 deletions sbbclient/sbbclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package sbbclient

import (
"bytes"
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"time"
)

// Client defines typed wrappers for the SBB RPC API.
type SbbClient struct {
httpClient *http.Client
}

func New() *SbbClient {
return &SbbClient{
httpClient: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
},
},
}
}

func makeRequest(ctx context.Context, url string, body interface{}) (*http.Request, error) {
b, err := json.Marshal(body)

req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(b))
if err != nil {
return nil, err
}

// 헤더 설정 (Cache-Control: no-cache 추가)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cache-Control", "no-cache") // 캐시 방지
return req, nil
}

func (sc *SbbClient) Send(ctx context.Context, url string, body interface{}, result any) error {
req, err := makeRequest(ctx, url, body)
if err != nil {
return err
}

res, err := sc.httpClient.Do(req)
if err != nil {
return err
}
// 해야하는지 확인 필요
defer func() {
if err = res.Body.Close(); err != nil {

}
}()

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

var jsonRpcResponse JSONRPCResponse
if err = json.Unmarshal(resBody, &jsonRpcResponse); err != nil {
return err
}
if jsonRpcResponse.Error != nil {
return errors.New(jsonRpcResponse.Error.Message)
}
if jsonRpcResponse.Result != nil && result != nil {
if err = json.Unmarshal(jsonRpcResponse.Result, result); err != nil {
return err
}
}
return nil
}

// JSON-RPC 응답 형식 정의
type JSONRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
Result json.RawMessage `json:"result"`
Error *RPCError `json:"error,omitempty"`
ID int `json:"id"`
}

type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data,omitempty"`
}
Loading

0 comments on commit 4c3b947

Please sign in to comment.