Use jsoniter

This commit is contained in:
Ben Sarmiento
2023-12-02 06:54:38 +01:00
parent 91ca1ebf88
commit 01fe4f0a09
7 changed files with 64 additions and 41 deletions

3
go.mod
View File

@@ -12,7 +12,10 @@ require github.com/orcaman/concurrent-map/v2 v2.0.1
require github.com/panjf2000/ants/v2 v2.8.2
require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/scylladb/go-set v1.0.2 // indirect
)

8
go.sum
View File

@@ -13,10 +13,17 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn
github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
github.com/orcaman/concurrent-map/v2 v2.0.1/go.mod h1:9Eq3TG2oBe5FirmYWQfYO5iH1q0Jv47PLaNK++uCdOM=
github.com/panjf2000/ants/v2 v2.8.2 h1:D1wfANttg8uXhC9149gRt1PDQ+dLVFjNXkCEycMcvQQ=
@@ -35,6 +42,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=

View File

@@ -1,9 +1,10 @@
package router
import (
"encoding/json"
"fmt"
"net/http"
"os"
"runtime"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/dav"
@@ -14,8 +15,12 @@ import (
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type ZurgRouter struct {
getfile *universal.GetFile
torMgr *torrent.TorrentManager
@@ -172,24 +177,31 @@ func (zr *ZurgRouter) headFileHandler(resp http.ResponseWriter, req *http.Reques
universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, zr.torMgr, zr.log)
}
// Create a struct to hold the data
type RootResponse struct {
Version string `json:"zurg"`
Version string `json:"version"`
BuiltAt string `json:"built_at"`
GitCommit string `json:"git_commit"`
GoVersion string `json:"go_version"`
Dav string `json:"dav"`
Html string `json:"html"`
UserInfo *realdebrid.User `json:"user_info"`
UserInfo *realdebrid.User `json:"user_info"` // Replace UserInfoType with the actual type
MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB
TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB
Sys uint64 `json:"sys"` // System memory in MB
NumGC uint32 `json:"num_gc"` // Number of completed GC cycles
PID int `json:"pid"` // Process ID
}
func (zr *ZurgRouter) rootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
userInfo, err := zr.api.GetUserInformation()
if err != nil {
// Handle the error appropriately
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
response := RootResponse{
Version: version.GetVersion(),
BuiltAt: version.GetBuiltAt(),
@@ -198,9 +210,18 @@ func (zr *ZurgRouter) rootHandler(resp http.ResponseWriter, req *http.Request, p
Dav: fmt.Sprintf("//%s/dav/", req.Host),
Html: fmt.Sprintf("//%s/http/", req.Host),
UserInfo: userInfo,
MemAlloc: bToMb(mem.Alloc),
TotalAlloc: bToMb(mem.TotalAlloc),
Sys: bToMb(mem.Sys),
NumGC: mem.NumGC,
PID: os.Getpid(),
}
if err := json.NewEncoder(resp).Encode(response); err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
}
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

View File

@@ -1,7 +1,6 @@
package torrent
import (
"encoding/json"
"fmt"
"io"
"math"
@@ -296,11 +295,11 @@ func (t *TorrentManager) startRefreshJob() {
for {
<-time.After(time.Duration(t.Config.GetRefreshEverySeconds()) * time.Second)
checksum := t.getCurrentState()
if t.latestState.equal(checksum) {
continue
}
t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
// checksum := t.getCurrentState()
// if t.latestState.equal(checksum) {
// continue
// }
// t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
t.RefreshTorrents()
t.log.Info("Finished refreshing torrents")

View File

@@ -1,12 +1,15 @@
package torrent
import (
"encoding/json"
oldjson "encoding/json"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
jsoniter "github.com/json-iterator/go"
cmap "github.com/orcaman/concurrent-map/v2"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type Torrent struct {
AccessKey string `json:"AccessKey"`
SelectedFiles cmap.ConcurrentMap[string, *File] `json:"-"`
@@ -19,8 +22,7 @@ type Torrent struct {
func (t *Torrent) MarshalJSON() ([]byte, error) {
type Alias Torrent
temp := &struct {
SelectedFilesJson json.RawMessage `json:"SelectedFiles"`
// InstancesJson json.RawMessage `json:"Instances"`
SelectedFilesJson oldjson.RawMessage `json:"SelectedFiles"`
*Alias
}{
Alias: (*Alias)(t),
@@ -30,19 +32,13 @@ func (t *Torrent) MarshalJSON() ([]byte, error) {
return nil, err
}
temp.SelectedFilesJson = selectedFilesJson
// instancesJson, err := json.Marshal(t.Instances)
// if err != nil {
// return nil, err
// }
// temp.InstancesJson = instancesJson
return json.Marshal(temp)
}
func (t *Torrent) UnmarshalJSON(data []byte) error {
type Alias Torrent
temp := &struct {
SelectedFilesJson json.RawMessage `json:"SelectedFiles"`
// InstancesJson json.RawMessage `json:"Instances"`
SelectedFilesJson oldjson.RawMessage `json:"SelectedFiles"`
*Alias
}{
Alias: (*Alias)(t),
@@ -56,12 +52,6 @@ func (t *Torrent) UnmarshalJSON(data []byte) error {
return err
}
}
// if len(temp.InstancesJson) > 0 {
// instances := make([]*realdebrid.TorrentInfo, 0)
// if err := json.Unmarshal(temp.SelectedFilesJson, &instances); err != nil {
// return nil
// }
// }
return nil
}

View File

@@ -2,7 +2,6 @@ package realdebrid
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"

View File

@@ -1,11 +1,14 @@
package realdebrid
import (
"encoding/json"
"math"
"strings"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type FileJSON struct {
FileSize int `json:"filesize"`
Link string `json:"link"`