Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Ent Part 1 #359

Merged
merged 4 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,18 @@ jobs:
- name: Get sources
uses: actions/checkout@v3

- name: Set up Go 1.18
- name: Set up Go 1.20
uses: actions/setup-go@v3
with:
go-version: '1.18'

- name: Remove old static libs if modified
if: needs.check.outputs.changed == 'true'
run: |
rm -r internal/parser/lib

- name: Download new static libs if modified
if: needs.check.outputs.changed == 'true'
uses: actions/download-artifact@v3
with:
name: ${{ matrix.os }}-libs
path: internal/parser/lib
go-version: '1.20'

- name: Run go mod tidy
run: go mod tidy

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.1
args: --timeout=500s
skip-cache: true
run: |
go install github.com/golangci/golangci-lint/cmd/golangci-lint@251ceaa228607dd3e0371694a1ab2c45d21cb744
golangci-lint run --timeout=500s

- name: Run tests
run: go test -timeout 15m -v ./...
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ linters:
disable:
- godox # Annoying, we have too many TODOs at the moment :p
- scopelint # Deprecated, replaced by exportloopref, which is enabled by default.
- errorlint # Too many false positives

issues:
exclude-rules:
Expand Down
6 changes: 5 additions & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"time"

"github.com/ProtonMail/gluon/async"
"github.com/ProtonMail/gluon/db"
"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/gluon/internal/backend"
"github.com/ProtonMail/gluon/internal/db"
"github.com/ProtonMail/gluon/internal/db_impl/ent_db"
"github.com/ProtonMail/gluon/internal/session"
"github.com/ProtonMail/gluon/limits"
"github.com/ProtonMail/gluon/profiling"
Expand All @@ -36,6 +37,7 @@ type serverBuilder struct {
imapLimits limits.IMAP
uidValidityGenerator imap.UIDValidityGenerator
panicHandler async.PanicHandler
dbCI db.ClientInterface
}

func newBuilder() (*serverBuilder, error) {
Expand All @@ -48,6 +50,7 @@ func newBuilder() (*serverBuilder, error) {
imapLimits: limits.DefaultLimits(),
uidValidityGenerator: imap.DefaultEpochUIDValidityGenerator(),
panicHandler: async.NoopPanicHandler{},
dbCI: ent_db.NewEntDBBuilder(),
}, nil
}

Expand Down Expand Up @@ -86,6 +89,7 @@ func (builder *serverBuilder) build() (*Server, error) {
builder.loginJailTime,
builder.imapLimits,
builder.panicHandler,
builder.dbCI,
)
if err != nil {
return nil, err
Expand Down
64 changes: 26 additions & 38 deletions connector/mock_connector/connector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions db/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package db

import (
"context"
"path/filepath"
)

const ChunkLimit = 1000

type Client interface {
Init(ctx context.Context) error
Read(ctx context.Context, op func(context.Context, ReadOnly) error) error
Write(ctx context.Context, op func(context.Context, Transaction) error) error
Close() error
}

type ClientInterface interface {
New(path string, userID string) (Client, bool, error)
Delete(path string, userID string) error
}

func GetDeferredDeleteDBPath(dir string) string {
return filepath.Join(dir, "deferred_delete")
}

func ClientReadType[T any](ctx context.Context, c Client, op func(context.Context, ReadOnly) (T, error)) (T, error) {
var result T

err := c.Read(ctx, func(ctx context.Context, read ReadOnly) error {
var err error

result, err = op(ctx, read)

return err
})

return result, err
}

func ClientWriteType[T any](ctx context.Context, c Client, op func(context.Context, Transaction) (T, error)) (T, error) {
var result T

err := c.Write(ctx, func(ctx context.Context, t Transaction) error {
var err error

result, err = op(ctx, t)

return err
})

return result, err
}
48 changes: 48 additions & 0 deletions db/deferred_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package db

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/google/uuid"
)

// DeleteDB will rename all the database files for the given user to a directory within the same folder to avoid
// issues with ent not being able to close the database on demand. The database will be cleaned up on the next
// run on the Gluon server.
func DeleteDB(dir, userID string) error {
// Rather than deleting the files immediately move them to a directory to be cleaned up later.
deferredDeletePath := GetDeferredDeleteDBPath(dir)

if err := os.MkdirAll(deferredDeletePath, 0o700); err != nil {
return fmt.Errorf("failed to create deferred delete dir: %w", err)
}

matchingFiles, err := filepath.Glob(filepath.Join(dir, userID+"*"))
if err != nil {
return fmt.Errorf("failed to match db files:%w", err)
}

for _, file := range matchingFiles {
// Use new UUID to avoid conflict with existing files
if err := os.Rename(file, filepath.Join(deferredDeletePath, uuid.NewString())); err != nil {
return fmt.Errorf("failed to move db file '%v' :%w", file, err)
}
}

return nil
}

// DeleteDeferredDBFiles deletes all data from previous databases that were scheduled for removal.
func DeleteDeferredDBFiles(dir string) error {
deferredDeleteDir := GetDeferredDeleteDBPath(dir)
if err := os.RemoveAll(deferredDeleteDir); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
}

return nil
}
14 changes: 14 additions & 0 deletions db/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package db

import "errors"

var ErrNotFound = errors.New("value not found")
var ErrTransactionFailed = errors.New("transaction failed")

func IsErrNotFound(err error) bool {
if err == nil {
return false
}

return errors.Is(err, ErrNotFound)
}
13 changes: 13 additions & 0 deletions db/ops.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package db

type ReadOnly interface {
MailboxReadOps
MessageReadOps
SubscriptionReadOps
}

type Transaction interface {
MailboxWriteOps
MessageWriteOps
SubscriptionWriteOps
}
Loading