Skip to content

Commit cb43221

Browse files
committed
refactor: move cache to pkg, simplify handlers (#42)
* refactor: move cache to pkg * refactor: simplify handlers Signed-off-by: Aviral Takkar <avtakkar@microsoft.com>
1 parent 3d06a92 commit cb43221

25 files changed

+43
-40
lines changed

build/package/peerd-helm/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ version: 0.0.3-alpha
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "0.0.6-alpha"
24+
appVersion: "0.0.7-alpha"

internal/files/store/file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"testing"
1111

1212
"github.com/azure/peerd/internal/files"
13-
"github.com/azure/peerd/internal/files/cache"
1413
remotetests "github.com/azure/peerd/internal/remote/tests"
14+
"github.com/azure/peerd/pkg/cache"
1515
"github.com/azure/peerd/pkg/discovery/routing/tests"
1616
)
1717

internal/files/store/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"os"
1010
"testing"
1111

12-
"github.com/azure/peerd/internal/files/cache"
12+
"github.com/azure/peerd/pkg/cache"
1313
"github.com/azure/peerd/pkg/metrics"
1414
)
1515

internal/files/store/mockstore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package store
55
import (
66
"context"
77

8-
"github.com/azure/peerd/internal/files/cache"
8+
"github.com/azure/peerd/pkg/cache"
99
"github.com/azure/peerd/pkg/discovery/routing"
1010
)
1111

internal/files/store/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
p2pcontext "github.com/azure/peerd/internal/context"
1313
"github.com/azure/peerd/internal/files"
14-
"github.com/azure/peerd/internal/files/cache"
1514
"github.com/azure/peerd/internal/remote"
15+
"github.com/azure/peerd/pkg/cache"
1616
"github.com/azure/peerd/pkg/discovery/routing"
1717
"github.com/azure/peerd/pkg/metrics"
1818
"github.com/azure/peerd/pkg/urlparser"
@@ -25,7 +25,7 @@ import (
2525
func NewFilesStore(ctx context.Context, r routing.Router) (FilesStore, error) {
2626
fs := &store{
2727
metricsRecorder: metrics.FromContext(ctx),
28-
cache: cache.New(ctx),
28+
cache: cache.New(ctx, int64(files.CacheBlockSize)),
2929
prefetchChan: make(chan prefetchableSegment, PrefetchWorkers),
3030
prefetchable: PrefetchWorkers > 0,
3131
router: r,

internal/handlers/files/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package handlers
3+
package files
44

55
import (
66
"context"

internal/handlers/files/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package handlers
3+
package files
44

55
import (
66
"context"

internal/handlers/files/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package handlers
3+
package files
44

55
import (
66
"crypto/rand"
77
"fmt"
88
"os"
99
"testing"
1010

11-
"github.com/azure/peerd/internal/files/cache"
11+
"github.com/azure/peerd/pkg/cache"
1212
)
1313

1414
func TestMain(m *testing.M) {

internal/handlers/root.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import (
99

1010
p2pcontext "github.com/azure/peerd/internal/context"
1111
filesStore "github.com/azure/peerd/internal/files/store"
12-
filesHandler "github.com/azure/peerd/internal/handlers/files"
13-
ociHandler "github.com/azure/peerd/internal/handlers/v2"
12+
"github.com/azure/peerd/internal/handlers/files"
13+
v2 "github.com/azure/peerd/internal/handlers/v2"
1414
"github.com/azure/peerd/pkg/containerd"
1515
"github.com/azure/peerd/pkg/discovery/routing"
1616
"github.com/gin-gonic/gin"
1717
"github.com/rs/zerolog"
1818
)
1919

20-
var fh *filesHandler.FilesHandler
21-
var v2h *ociHandler.V2Handler
20+
var fh *files.FilesHandler
21+
var v2h *v2.V2Handler
2222

2323
// Server creates a new HTTP server.
2424
func Handler(ctx context.Context, r routing.Router, containerdStore containerd.Store, fs filesStore.FilesStore) (http.Handler, error) {
2525
var err error
26-
fh = filesHandler.New(ctx, fs)
26+
fh = files.New(ctx, fs)
2727

28-
v2h, err = ociHandler.New(ctx, r, containerdStore)
28+
v2h, err = v2.New(ctx, r, containerdStore)
2929
if err != nil {
3030
return nil, err
3131
}

internal/handlers/v2/handler.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package handlers
3+
package v2
44

55
import (
66
"context"
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
p2pcontext "github.com/azure/peerd/internal/context"
12-
"github.com/azure/peerd/internal/oci"
1312
"github.com/azure/peerd/pkg/containerd"
1413
"github.com/azure/peerd/pkg/discovery/routing"
1514
"github.com/azure/peerd/pkg/metrics"
@@ -19,8 +18,8 @@ import (
1918

2019
// V2Handler describes a handler for OCI content.
2120
type V2Handler struct {
22-
mirror *oci.Mirror
23-
registry *oci.Registry
21+
mirror *Mirror
22+
registry *Registry
2423
metricsRecorder metrics.Metrics
2524
}
2625

@@ -90,8 +89,8 @@ func (h *V2Handler) fill(c *gin.Context) error {
9089
// New creates a new OCI content handler.
9190
func New(ctx context.Context, router routing.Router, containerdStore containerd.Store) (*V2Handler, error) {
9291
return &V2Handler{
93-
mirror: oci.NewMirror(router),
94-
registry: oci.NewRegistry(containerdStore),
92+
mirror: NewMirror(router),
93+
registry: NewRegistry(containerdStore),
9594
metricsRecorder: metrics.FromContext(ctx),
9695
}, nil
9796
}

internal/handlers/v2/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package handlers
1+
package v2
22

33
import (
44
"context"

internal/oci/mirror.go renamed to internal/handlers/v2/mirror.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package oci
3+
package v2
44

55
import (
66
"context"

internal/oci/mirror_test.go renamed to internal/handlers/v2/mirror_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package oci
3+
package v2
44

55
import (
66
"fmt"

internal/oci/registry.go renamed to internal/handlers/v2/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package oci
3+
package v2
44

55
import (
66
"fmt"

internal/oci/registry_test.go renamed to internal/handlers/v2/registry_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package oci
3+
package v2
44

55
import (
66
"net/http"

internal/remote/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Reader interface {
2020
Log() *zerolog.Logger
2121
}
2222

23-
// Error describes an error that occured during a remote operation.
23+
// Error describes an error that occurred during a remote operation.
2424
type Error struct {
2525
*http.Response
2626
error

internal/files/cache/cache.go renamed to pkg/cache/filecache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"sync/atomic"
1414
"time"
1515

16-
"github.com/azure/peerd/internal/files"
1716
"github.com/dgraph-io/ristretto"
1817
"github.com/rs/zerolog"
1918
)
@@ -149,7 +148,8 @@ func waitForSet() {
149148
}
150149

151150
// New creates a new cache of files.
152-
func New(ctx context.Context) Cache {
151+
// cacheBlockSize is the fixed size of the cache block in bytes, and is used to evaluate the cost of each item in the cache.
152+
func New(ctx context.Context, cacheBlockSize int64) Cache {
153153
log := zerolog.Ctx(ctx).With().Str("component", "cache").Logger()
154154

155155
atomic.StoreInt32(&fdCnt, 0)
@@ -176,7 +176,7 @@ func New(ctx context.Context) Cache {
176176
},
177177

178178
Cost: func(val interface{}) int64 {
179-
return int64(files.CacheBlockSize)
179+
return cacheBlockSize
180180
},
181181
}); err != nil {
182182
// This will call os.Exit(1)

internal/files/cache/cache_test.go renamed to pkg/cache/filecache_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ import (
1818
"golang.org/x/sync/errgroup"
1919
)
2020

21+
var (
22+
cacheBlockSize = int64(1 * 1024 * 1024)
23+
)
24+
2125
func TestGetKey(t *testing.T) {
2226
name := newRandomStringN(10)
2327
offset := int64(100)
24-
c := New(context.Background())
28+
c := New(context.Background(), cacheBlockSize)
2529
got := c.(*fileCache).getKey(name, offset)
2630
want := fmt.Sprintf("%v/%v/%v", Path, name, offset)
2731
if got != want {
@@ -30,7 +34,7 @@ func TestGetKey(t *testing.T) {
3034
}
3135

3236
func TestExists(t *testing.T) {
33-
c := New(context.Background())
37+
c := New(context.Background(), cacheBlockSize)
3438

3539
filesThatExist := []string{}
3640
for i := 0; i < 5; i++ {
@@ -113,7 +117,7 @@ func TestExists(t *testing.T) {
113117
}
114118

115119
func TestPutAndGetSize(t *testing.T) {
116-
c := New(context.Background())
120+
c := New(context.Background(), cacheBlockSize)
117121
var eg errgroup.Group
118122

119123
for i := 0; i < 1000; i++ {
@@ -150,7 +154,7 @@ func TestPutAndGetSize(t *testing.T) {
150154
func TestGetOrCreate(t *testing.T) {
151155
zerolog.TimeFieldFormat = time.RFC3339
152156
//c := New(zerolog.New(os.Stdout).With().Timestamp().Logger().WithContext(context.Background()))
153-
c := New(context.Background())
157+
c := New(context.Background(), cacheBlockSize)
154158
var eg errgroup.Group
155159

156160
fileNames := new(sync.Map)

internal/files/cache/interface.go renamed to pkg/cache/interface.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33
package cache
44

5-
// Cache describes the cache of files.
5+
// Cache describes a cache of files.
66
type Cache interface {
77
// Size gets the size of the file.
88
Size(path string) (int64, bool)
@@ -18,12 +18,12 @@ type Cache interface {
1818
}
1919

2020
var (
21-
// FilesCacheMaxCost is the capacity of the files cache in any unit.
21+
// FilesCacheMaxCost is the capacity of the files cache.
2222
FilesCacheMaxCost int64 = 4 * 1024 * 1024 * 1024 // 4 Gib
2323

24-
// MemoryCacheMaxCost is the capacity of the memory cache in any unit.
24+
// MemoryCacheMaxCost is the capacity of the memory cache.
2525
MemoryCacheMaxCost int64 = 1 * 1024 * 1024 * 1024 // 1 Gib
2626

2727
// Path is the path to the cache directory.
28-
Path string = "/tmp/distribution/p2p/cache"
28+
Path string = "/tmp/distribution/peerd/cache"
2929
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

pkg/peernet/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type network struct {
4848
var _ Network = &network{}
4949

5050
// DefaultTLSConfig creates a TLS config to use for this server.
51-
// This config does not require client certificate verification and is resuable.
51+
// This config does not require client certificate verification and is reusable.
5252
func (n *network) DefaultTLSConfig() *tls.Config {
5353
return n.defaultTLSConfig
5454
}

0 commit comments

Comments
 (0)