Skip to content

Commit

Permalink
update to go 1.23
Browse files Browse the repository at this point in the history
update deps
  • Loading branch information
vintikzzz committed Oct 22, 2024
1 parent aec1015 commit ca5029e
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 1,917 deletions.
48 changes: 25 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
module github.com/webtor-io/torrent-store

go 1.23

require (
github.com/anacrolix/torrent v1.47.0
github.com/aws/aws-sdk-go v1.44.163
github.com/aws/aws-sdk-go v1.55.5
github.com/dgraph-io/badger/v3 v3.2103.5
github.com/go-redis/redis v6.15.9+incompatible
github.com/golang/protobuf v1.5.2
github.com/golang/protobuf v1.5.4
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
github.com/urfave/cli v1.22.10
github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli v1.22.16
github.com/webtor-io/abuse-store v0.0.0-20221220184115-12e6c0615a10
github.com/webtor-io/common-services v0.0.0-20221220182322-cdc4055631c8
github.com/webtor-io/common-services v0.0.0-20241022160325-d391acd827ab
github.com/webtor-io/lazymap v0.0.0-20221030185154-1799721becef
golang.org/x/sys v0.3.0 // indirect
golang.org/x/sys v0.26.0 // indirect
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37 // indirect
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.28.1
google.golang.org/protobuf v1.35.1
)

require github.com/webtor-io/stoplist v0.0.0-20230128160543-ea87bdc34deb
Expand All @@ -26,12 +28,13 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/go-pg/migrations/v8 v8.1.0 // indirect
github.com/go-pg/pg/v10 v10.10.7 // indirect
github.com/go-pg/pg/v10 v10.13.0 // indirect
github.com/go-pg/zerochecker v0.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.0.0 // indirect
Expand All @@ -41,25 +44,24 @@ require (
github.com/huandu/xstrings v1.4.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/compress v1.15.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/bufpool v0.1.11 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mellium.im/sasl v0.3.1 // indirect
mellium.im/sasl v0.3.2 // indirect
)

go 1.17
1,883 changes: 63 additions & 1,820 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion services/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ func (s *GRPCServer) Serve() error {

func (s *GRPCServer) Close() {
if s.ln != nil {
s.ln.Close()
_ = s.ln.Close()
}
}
16 changes: 10 additions & 6 deletions services/providers/badger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package providers

import (
"context"
"github.com/pkg/errors"
"time"

badger "github.com/dgraph-io/badger/v3"
Expand Down Expand Up @@ -50,10 +52,10 @@ func (s *Badger) Name() string {
return "badger"
}

func (s *Badger) Touch(h string) (err error) {
func (s *Badger) Touch(_ context.Context, h string) (err error) {
err = s.db.Update(func(txn *badger.Txn) error {
i, err := txn.Get([]byte(h))
if err == badger.ErrKeyNotFound {
if errors.Is(err, badger.ErrKeyNotFound) {
return ss.ErrNotFound

} else {
Expand All @@ -67,18 +69,18 @@ func (s *Badger) Touch(h string) (err error) {
return
}

func (s *Badger) Push(h string, torrent []byte) (err error) {
func (s *Badger) Push(_ context.Context, h string, torrent []byte) (err error) {
err = s.db.Update(func(txn *badger.Txn) error {
e := badger.NewEntry([]byte(h), torrent).WithTTL(s.exp)
return txn.SetEntry(e)
})
return
}

func (s *Badger) Pull(h string) (torrent []byte, err error) {
func (s *Badger) Pull(_ context.Context, h string) (torrent []byte, err error) {
err = s.db.View(func(txn *badger.Txn) (err error) {
i, err := txn.Get([]byte(h))
if err == badger.ErrKeyNotFound {
if errors.Is(err, badger.ErrKeyNotFound) {
return ss.ErrNotFound
} else {
err = i.Value(func(val []byte) error {
Expand All @@ -92,5 +94,7 @@ func (s *Badger) Pull(h string) (torrent []byte, err error) {
}

func (s *Badger) Close() {
s.db.Close()
_ = s.db.Close()
}

var _ ss.StoreProvider = (*Badger)(nil)
18 changes: 11 additions & 7 deletions services/providers/redis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package providers

import (
"context"
"github.com/pkg/errors"
"time"

"github.com/go-redis/redis"
Expand Down Expand Up @@ -49,10 +51,10 @@ func (s *Redis) Name() string {
return "redis"
}

func (s *Redis) Touch(h string) (err error) {
func (s *Redis) Touch(ctx context.Context, h string) (err error) {
cl := s.cl.Get()

res, err := cl.Expire(h, s.exp).Result()
res, err := cl.Expire(ctx, h, s.exp).Result()

if err != nil {
return err
Expand All @@ -63,16 +65,18 @@ func (s *Redis) Touch(h string) (err error) {
return nil
}

func (s *Redis) Push(h string, torrent []byte) (err error) {
func (s *Redis) Push(ctx context.Context, h string, torrent []byte) (err error) {
cl := s.cl.Get()
return cl.Set(h, torrent, s.exp).Err()
return cl.Set(ctx, h, torrent, s.exp).Err()
}

func (s *Redis) Pull(h string) (torrent []byte, err error) {
func (s *Redis) Pull(ctx context.Context, h string) (torrent []byte, err error) {
cl := s.cl.Get()
torrent, err = cl.Get(h).Bytes()
if err == redis.Nil {
torrent, err = cl.Get(ctx, h).Bytes()
if errors.Is(err, redis.Nil) {
return nil, ss.ErrNotFound
}
return
}

var _ ss.StoreProvider = (*Redis)(nil)
26 changes: 12 additions & 14 deletions services/providers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"context"
"crypto/md5"
"encoding/base64"
"io"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/urfave/cli"
cs "github.com/webtor-io/common-services"
ss "github.com/webtor-io/torrent-store/services"
"io"
)

const (
Expand Down Expand Up @@ -56,10 +54,8 @@ func (s *S3) Name() string {
return "s3"
}

func (s *S3) Touch(h string) (err error) {
func (s *S3) Touch(ctx context.Context, h string) (err error) {
cl := s.cl.Get()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
r, err := cl.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(h),
Expand All @@ -70,7 +66,9 @@ func (s *S3) Touch(h string) (err error) {
}
return err
}
defer r.Body.Close()
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(r.Body)
return nil
}

Expand All @@ -80,10 +78,8 @@ func (s *S3) makeAWSMD5(b []byte) *string {
return aws.String(m)
}

func (s *S3) Push(h string, torrent []byte) (err error) {
func (s *S3) Push(ctx context.Context, h string, torrent []byte) (err error) {
cl := s.cl.Get()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err = cl.PutObjectWithContext(ctx,
&s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Expand All @@ -94,10 +90,8 @@ func (s *S3) Push(h string, torrent []byte) (err error) {
return
}

func (s *S3) Pull(h string) (torrent []byte, err error) {
func (s *S3) Pull(ctx context.Context, h string) (torrent []byte, err error) {
cl := s.cl.Get()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
r, err := cl.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(h),
Expand All @@ -108,6 +102,10 @@ func (s *S3) Pull(h string) (torrent []byte, err error) {
}
return nil, err
}
defer r.Body.Close()
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(r.Body)
return io.ReadAll(r.Body)
}

var _ ss.StoreProvider = (*S3)(nil)
54 changes: 27 additions & 27 deletions services/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ func NewServer(s *Store, a *Abuse, sl *Stoplist) *Server {

func (s *Server) Pull(ctx context.Context, in *pb.PullRequest) (*pb.PullReply, error) {
t := time.Now()
log := log.WithField("infoHash", in.GetInfoHash())
log.Info("pull torrent request")
hLog := log.WithField("infoHash", in.GetInfoHash())
hLog.Info("pull torrent request")

err := s.isAbused(in.GetInfoHash())
if err == ErrAbuse {
log.WithField("duration", time.Since(t)).Warn("abused")
if errors.Is(err, ErrAbuse) {
hLog.WithField("duration", time.Since(t)).Warn("abused")
return nil, status.Errorf(codes.PermissionDenied, "Restricted by the rightholder infoHash=%v", in.GetInfoHash())
} else if err != nil {
log.WithField("duration", time.Since(t)).WithError(err).Error("failed to check abuse")
hLog.WithField("duration", time.Since(t)).WithError(err).Error("failed to check abuse")
return nil, errors.Wrapf(err, "failed to check abuse infoHash=%v", in.GetInfoHash())
}

torrent, err := s.s.Pull(in.GetInfoHash())
if err == ErrNotFound {
log.WithField("duration", time.Since(t)).Info("torrent not found")
torrent, err := s.s.Pull(ctx, in.GetInfoHash())
if errors.Is(err, ErrNotFound) {
hLog.WithField("duration", time.Since(t)).Info("torrent not found")
return nil, status.Errorf(codes.NotFound, "Unable to find torrent for infoHash=%v", in.GetInfoHash())
} else if err != nil {
log.WithField("duration", time.Since(t)).WithError(err).Error("failed to pull")
hLog.WithField("duration", time.Since(t)).WithError(err).Error("failed to pull")
return nil, errors.Wrapf(err, "failed to pull torrent infoHash=%v", in.GetInfoHash())
}
err = s.checkStoplist(torrent, log, t, in.GetInfoHash())
err = s.checkStoplist(torrent, hLog, t, in.GetInfoHash())
if err != nil {
return nil, err
}
log.WithField("len", len(torrent)).WithField("duration", time.Since(t)).Info("sending torrent response")
hLog.WithField("len", len(torrent)).WithField("duration", time.Since(t)).Info("sending torrent response")
return &pb.PullReply{Torrent: []byte(torrent)}, nil
}

Expand Down Expand Up @@ -83,29 +83,29 @@ func (s *Server) Push(ctx context.Context, in *pb.PushRequest) (*pb.PushReply, e
return nil, err
}
infoHash := mi.HashInfoBytes().HexString()
log := log.WithField("infoHash", infoHash)
log.Info("push torrent request")
hLog := log.WithField("infoHash", infoHash)
hLog.Info("push torrent request")

err = s.checkStoplist(in.GetTorrent(), log, t, infoHash)
err = s.checkStoplist(in.GetTorrent(), hLog, t, infoHash)
if err != nil {
return nil, err
}

err = s.isAbused(infoHash)
if err == ErrAbuse {
log.WithField("duration", time.Since(t)).Warn("abused")
if errors.Is(err, ErrAbuse) {
hLog.WithField("duration", time.Since(t)).Warn("abused")
return nil, status.Errorf(codes.PermissionDenied, "Restricted by the rightholder infoHash=%v", infoHash)
} else if err != nil {
log.WithField("duration", time.Since(t)).WithError(err).Error("failed to check abuse")
hLog.WithField("duration", time.Since(t)).WithError(err).Error("failed to check abuse")
return nil, errors.Wrapf(err, "failed to check abuse infoHash=%v", infoHash)
}
err = s.s.Push(infoHash, in.GetTorrent())
err = s.s.Push(ctx, infoHash, in.GetTorrent())
if err != nil {
log.WithField("duration", time.Since(t)).WithError(err).Error("failed to push")
hLog.WithField("duration", time.Since(t)).WithError(err).Error("failed to push")
return nil, errors.Wrapf(err, "failed to push torrent infoHash=%v", infoHash)
}

log.WithField("len", len(in.GetTorrent())).WithField("duration", time.Since(t)).Info("torrent succesfully pushed")
hLog.WithField("len", len(in.GetTorrent())).WithField("duration", time.Since(t)).Info("torrent succesfully pushed")
return &pb.PushReply{InfoHash: infoHash}, nil
}

Expand All @@ -119,18 +119,18 @@ func (s *Server) isAbused(h string) error {
func (s *Server) Touch(ctx context.Context, in *pb.TouchRequest) (*pb.TouchReply, error) {
t := time.Now()
infoHash := in.GetInfoHash()
log := log.WithField("infoHash", infoHash)
log.Info("touch torrent request")
hLog := log.WithField("infoHash", infoHash)
hLog.Info("touch torrent request")

err := s.s.Touch(infoHash)
if err == ErrNotFound {
log.WithField("duration", time.Since(t)).Info("torrent not found")
err := s.s.Touch(ctx, infoHash)
if errors.Is(err, ErrNotFound) {
hLog.WithField("duration", time.Since(t)).Info("torrent not found")
return nil, status.Errorf(codes.NotFound, "torrent not found infoHash=%v", infoHash)
} else if err != nil {
log.WithField("duration", time.Since(t)).WithError(err).Error("failed to touch")
hLog.WithField("duration", time.Since(t)).WithError(err).Error("failed to touch")
return nil, errors.Wrapf(err, "failed to touch torrent infoHash=%v", infoHash)
}

log.WithField("duration", time.Since(t)).Info("sending touch reply")
hLog.WithField("duration", time.Since(t)).Info("sending touch reply")
return &pb.TouchReply{}, nil
}
Loading

0 comments on commit ca5029e

Please sign in to comment.