mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 09:35:00 +09:00
GO-4140: add limits
Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
This commit is contained in:
parent
87508f083b
commit
c806bcb761
6 changed files with 7097 additions and 2464 deletions
|
@ -3,6 +3,7 @@ package payments
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
|
@ -137,6 +138,8 @@ type service struct {
|
|||
|
||||
multiplayerLimitsUpdater deletioncontroller.DeletionController
|
||||
fileLimitsUpdater filesync.FileSync
|
||||
membershipStatus model.MembershipStatus
|
||||
membershipStatusMu sync.Mutex
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
|
@ -184,10 +187,22 @@ func (s *service) getPeriodicStatus(ctx context.Context) error {
|
|||
|
||||
// get subscription status (from cache or from the PP node)
|
||||
// if status has changed -> it will send events, etc
|
||||
_, err := s.GetSubscriptionStatus(ctx, &pb.RpcMembershipGetStatusRequest{})
|
||||
status, err := s.GetSubscriptionStatus(ctx, &pb.RpcMembershipGetStatusRequest{})
|
||||
if err == nil {
|
||||
membershipStatus := status.GetData().Status
|
||||
s.membershipStatusMu.Lock()
|
||||
s.membershipStatus = membershipStatus
|
||||
s.membershipStatusMu.Unlock()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *service) MembershipStatus() model.MembershipStatus {
|
||||
s.membershipStatusMu.Lock()
|
||||
defer s.membershipStatusMu.Unlock()
|
||||
return s.membershipStatus
|
||||
}
|
||||
|
||||
func (s *service) sendMembershipUpdateEvent(status *pb.RpcMembershipGetStatusResponse) {
|
||||
s.eventSender.Broadcast(event.NewEventSingleMessage("", &pb.EventMessageValueOfMembershipUpdate{
|
||||
MembershipUpdate: &pb.EventMembershipUpdate{
|
||||
|
|
|
@ -22,12 +22,14 @@ func (mw *Middleware) PublishingCreate(ctx context.Context, req *pb.RpcPublishin
|
|||
res, err := publishService.Publish(ctx, req.SpaceId, req.ObjectId, req.Uri)
|
||||
log.Error("PublishingCreate called", zap.String("objectId", req.ObjectId))
|
||||
code := mapErrorCode(err,
|
||||
errToCode(nil, pb.RpcPublishingCreateResponseError_NULL))
|
||||
errToCode(nil, pb.RpcPublishingCreateResponseError_NULL),
|
||||
errToCode(err, pb.RpcPublishingCreateResponseError_UNKNOWN_ERROR),
|
||||
errToCode(publish.ErrLimitExceeded, pb.RpcPublishingCreateResponseError_LIMIT_EXCEEDED))
|
||||
|
||||
r := &pb.RpcPublishingCreateResponse{
|
||||
Error: &pb.RpcPublishingCreateResponseError{
|
||||
Code: code,
|
||||
Description: getErrorDescription(nil),
|
||||
Description: getErrorDescription(err),
|
||||
},
|
||||
Uri: res.Cid,
|
||||
// PublishCid: res.Cid,
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
@ -43,9 +44,16 @@ var (
|
|||
|
||||
const CName = "common.core.publishservice"
|
||||
|
||||
const (
|
||||
membershipLimit = 100 << 20
|
||||
defaultLimit = 10 << 20
|
||||
)
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
var cidBuilder = cid.V1Builder{Codec: cid.DagProtobuf, MhType: mh.SHA2_256}
|
||||
|
||||
var ErrLimitExceeded = errors.New("limit exceeded")
|
||||
|
||||
type PublishResult struct {
|
||||
Cid string
|
||||
Key string
|
||||
|
@ -71,14 +79,19 @@ type Service interface {
|
|||
Publish(ctx context.Context, spaceId, pageObjId, uri string) (res PublishResult, err error)
|
||||
}
|
||||
|
||||
type MembershipStatusProvider interface {
|
||||
MembershipStatus() model.MembershipStatus
|
||||
}
|
||||
|
||||
type service struct {
|
||||
commonFile fileservice.FileService
|
||||
fileSyncService filesync.FileSync
|
||||
spaceService space.Service
|
||||
dagService ipld.DAGService
|
||||
exportService export.Export
|
||||
publishClientService publishclient.Client
|
||||
accountService accountservice.Service
|
||||
commonFile fileservice.FileService
|
||||
fileSyncService filesync.FileSync
|
||||
spaceService space.Service
|
||||
dagService ipld.DAGService
|
||||
exportService export.Export
|
||||
publishClientService publishclient.Client
|
||||
accountService accountservice.Service
|
||||
membershipStatusProvider MembershipStatusProvider
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
|
@ -93,7 +106,7 @@ func (s *service) Init(a *app.App) error {
|
|||
s.exportService = app.MustComponent[export.Export](a)
|
||||
s.publishClientService = app.MustComponent[publishclient.Client](a)
|
||||
s.accountService = app.MustComponent[accountservice.Service](a)
|
||||
|
||||
s.membershipStatusProvider = app.MustComponent[MembershipStatusProvider](a)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -137,6 +150,7 @@ func (s *service) exportToDir(ctx context.Context, spaceId, pageId string) (dirE
|
|||
Zip: false,
|
||||
Path: tempDir,
|
||||
ObjectIds: []string{pageId},
|
||||
NoProgress: true,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -367,6 +381,7 @@ func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, u
|
|||
// "meta": { "root-page", "inviteLink", and other things}
|
||||
// then, add this `index.json` in `publishTmpFolder`
|
||||
|
||||
limit := s.getPublishLimit()
|
||||
tempPublishDir := filepath.Join(os.TempDir(), uniqName())
|
||||
defer os.RemoveAll(tempPublishDir)
|
||||
|
||||
|
@ -382,17 +397,30 @@ func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, u
|
|||
PbFiles: make(map[string]string),
|
||||
}
|
||||
|
||||
var size int64
|
||||
for _, entry := range dirEntries {
|
||||
if entry.IsDir() {
|
||||
var dirFiles []fs.DirEntry
|
||||
dirName := entry.Name()
|
||||
|
||||
if dirName == "files" {
|
||||
err = os.CopyFS(filepath.Join(tempPublishDir, "files"), os.DirFS(filepath.Join(exportPath, "files")))
|
||||
if dirName == export.Files {
|
||||
fileDir := filepath.Join(tempPublishDir, export.Files)
|
||||
err = os.CopyFS(fileDir, os.DirFS(filepath.Join(exportPath, export.Files)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = filepath.Walk(fileDir, func(path string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() {
|
||||
size = size + info.Size()
|
||||
if size > limit {
|
||||
return ErrLimitExceeded
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -403,7 +431,6 @@ func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, u
|
|||
|
||||
for _, file := range dirFiles {
|
||||
withDirName := filepath.Join(dirName, file.Name())
|
||||
|
||||
var snapshotData []byte
|
||||
snapshotData, err = os.ReadFile(filepath.Join(exportPath, withDirName))
|
||||
if err != nil {
|
||||
|
@ -455,10 +482,18 @@ func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, u
|
|||
return
|
||||
}
|
||||
|
||||
stat, err := file.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
size = size + stat.Size()
|
||||
if size > limit {
|
||||
return ErrLimitExceeded
|
||||
}
|
||||
|
||||
log.Error("publishing started", zap.String("pageid", pageId), zap.String("uri", uri))
|
||||
publishReq := &publishapi.PublishRequest{
|
||||
|
@ -484,6 +519,15 @@ func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, u
|
|||
|
||||
}
|
||||
|
||||
func (s *service) getPublishLimit() int64 {
|
||||
status := s.membershipStatusProvider.MembershipStatus()
|
||||
limit := defaultLimit
|
||||
if status == model.Membership_StatusActive {
|
||||
limit = membershipLimit
|
||||
}
|
||||
return int64(limit)
|
||||
}
|
||||
|
||||
func (s *service) Publish(ctx context.Context, spaceId, pageId, uri string) (res PublishResult, err error) {
|
||||
log.Info("Publish called", zap.String("pageId", pageId))
|
||||
err = s.publishToPublishServer(ctx, spaceId, pageId, uri)
|
||||
|
|
|
@ -24183,6 +24183,7 @@ Middleware-to-front-end response, that can contain a NULL error or a non-NULL er
|
|||
| BAD_INPUT | 2 | |
|
||||
| NO_SUCH_OBJECT | 101 | |
|
||||
| NO_SUCH_SPACE | 102 | |
|
||||
| LIMIT_EXCEEDED | 103 | |
|
||||
|
||||
|
||||
|
||||
|
|
9468
pb/commands.pb.go
9468
pb/commands.pb.go
File diff suppressed because it is too large
Load diff
|
@ -1395,6 +1395,7 @@ message Rpc {
|
|||
BAD_INPUT = 2;
|
||||
NO_SUCH_OBJECT = 101;
|
||||
NO_SUCH_SPACE = 102;
|
||||
LIMIT_EXCEEDED = 103;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue