diff --git a/pkg/adapter/controllers/tv_controller.go b/pkg/adapter/controllers/tv_controller.go index c9835f8..cf063cf 100644 --- a/pkg/adapter/controllers/tv_controller.go +++ b/pkg/adapter/controllers/tv_controller.go @@ -32,7 +32,7 @@ type TVController struct { Interactor usecase.TVInteractor } -func NewTVController(rwDB, roDB *gorm.DB) *TVController { +func NewTVController(rwDB, roDB *gorm.DB, httpClient *http.Client) *TVController { return &TVController{ Interactor: usecase.TVInteractor{ TVRepository: &gateway.TVRepository{ @@ -40,7 +40,8 @@ func NewTVController(rwDB, roDB *gorm.DB) *TVController { RODB: roDB, }, BybitRepository: &gateway.BybitRepository{ - Client: nil, + Client: nil, + HTTPClient: httpClient, }, }, } diff --git a/pkg/adapter/gateway/bybit_repository.go b/pkg/adapter/gateway/bybit_repository.go index 4128a0f..169cecb 100644 --- a/pkg/adapter/gateway/bybit_repository.go +++ b/pkg/adapter/gateway/bybit_repository.go @@ -23,12 +23,13 @@ type ( BaseURL string APIKey string APISecretKey string + HTTPClient *http.Client Client *rest.ByBit } ) func (r *BybitRepository) Set(req domain.TV) { - r.Client, r.BaseURL = bybit.Init(req) + r.Client, r.BaseURL = bybit.Init(req, r.HTTPClient) r.APIKey = req.APIKey r.APISecretKey = req.APISecretKey } @@ -273,7 +274,6 @@ func (r *BybitRepository) signedRequestWithHeader(method, path string, body []by url := fmt.Sprintf("%s%s", r.BaseURL, path) - client := &http.Client{} req, _ := http.NewRequest(method, url, payload) req.Header.Add("X-BAPI-API-KEY", r.APIKey) req.Header.Add("X-BAPI-SIGN", crypto.HexEncodeToString(hmacSigned)) @@ -281,7 +281,7 @@ func (r *BybitRepository) signedRequestWithHeader(method, path string, body []by req.Header.Add("X-BAPI-TIMESTAMP", nowTimeInMilli) req.Header.Add("X-BAPI-RECV-WINDOW", "5000") - resp, err := client.Do(req) + resp, err := r.HTTPClient.Do(req) if err != nil { return "", err } diff --git a/pkg/external/bybit/bybit.go b/pkg/external/bybit/bybit.go index 57f2324..5c1f12f 100644 --- a/pkg/external/bybit/bybit.go +++ b/pkg/external/bybit/bybit.go @@ -19,35 +19,19 @@ along with this program. If not, see . package bybit import ( - "net" "net/http" - "time" "github.com/frankrap/bybit-api/rest" "github.com/rluisr/tvbit-bot/pkg/domain" ) -var BaseURL = "https://api.bybit.com/" +var BaseURL string -func Init(req domain.TV) (*rest.ByBit, string) { +func Init(req domain.TV, httpClient *http.Client) (*rest.ByBit, string) { if req.IsTestNet { BaseURL = "https://api-testnet.bybit.com/" - } - - httpClient := &http.Client{ - Transport: &http.Transport{ - DialContext: (&net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, - }).DialContext, - MaxIdleConns: 128, - MaxIdleConnsPerHost: 100, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 10 * time.Second, - ResponseHeaderTimeout: 10 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - }, - Timeout: 60 * time.Second, + } else { + BaseURL = "https://api.bybit.com/" } return rest.New(httpClient, BaseURL, req.APIKey, req.APISecretKey, false), BaseURL diff --git a/pkg/external/cron.go b/pkg/external/cron.go index 2aba1c0..ce4d2fc 100644 --- a/pkg/external/cron.go +++ b/pkg/external/cron.go @@ -35,7 +35,7 @@ func cron() { Verbose: true, }) - task.Task("* * * * *", func(ctx context.Context) (int, error) { + task.Task("0 * * * *", func(ctx context.Context) (int, error) { settings, err := tvController.Interactor.TVRepository.GetSettings() if err != nil { return 0, err @@ -79,15 +79,11 @@ func cron() { if err != nil { return 1, err } - - balance = decimal.NewFromFloat(bybitDerivWallet.Equity) - totalRPL = decimal.NewFromFloat(bybitDerivWallet.CumRealisedPnl) - walletHistories = append(walletHistories, domain.WalletHistory{ SettingID: setting.ID, Type: "usdt", - Balance: balance, - TotalRPL: totalRPL, + Balance: decimal.NewFromFloat(bybitDerivWallet.Equity), + TotalRPL: decimal.NewFromFloat(bybitDerivWallet.CumRealisedPnl), }) } } diff --git a/pkg/external/http.go b/pkg/external/http.go new file mode 100644 index 0000000..504b443 --- /dev/null +++ b/pkg/external/http.go @@ -0,0 +1,45 @@ +/* + * + * tvbit-bot + * Copyright (C) 2022 rluisr(Takuya Hasegawa) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * / + */ + +package external + +import ( + "net" + "net/http" + "time" +) + +func NewHTTPClient() *http.Client { + return &http.Client{ + Transport: &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + MaxIdleConns: 128, + MaxIdleConnsPerHost: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ResponseHeaderTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + }, + Timeout: 60 * time.Second, + } +} diff --git a/pkg/external/init.go b/pkg/external/init.go index 5cbf5c0..570fbdd 100644 --- a/pkg/external/init.go +++ b/pkg/external/init.go @@ -38,7 +38,8 @@ func Init() (err error) { return err } - tvController = controllers.NewTVController(rwDB, roDB) + httpClient := NewHTTPClient() + tvController = controllers.NewTVController(rwDB, roDB, httpClient) settingController = controllers.NewSettingController(rwDB, roDB) return nil