Skip to content

Commit

Permalink
change chunk_payload type
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Dec 16, 2024
1 parent b4ad47b commit 7427b3d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
16 changes: 5 additions & 11 deletions proxy/config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"net/url"

"github.com/absmach/propeller/proplet"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
Expand All @@ -21,13 +22,6 @@ const (
chunkSize = 512000
)

type ChunkPayload struct {
AppName string `json:"app_name"`
ChunkIdx int `json:"chunk_idx"`
TotalChunks int `json:"total_chunks"`
Data []byte `json:"data"`
}

type HTTPProxyConfig struct {
RegistryURL string `env:"REGISTRY_URL" envDefault:"localhost:5000"`
Authenticate bool `env:"AUTHENTICATE" envDefault:"false"`
Expand Down Expand Up @@ -128,15 +122,15 @@ func findLargestLayer(manifest *ocispec.Manifest) (ocispec.Descriptor, error) {
return largestLayer, nil
}

func createChunks(data []byte, containerName string) []ChunkPayload {
func createChunks(data []byte, containerName string) []proplet.ChunkPayload {
dataSize := len(data)
totalChunks := (dataSize + chunkSize - 1) / chunkSize

// log.Printf("Total data size: %d bytes (%.2f MB)", dataSize, float64(dataSize)/size)

Check failure on line 129 in proxy/config/http.go

View workflow job for this annotation

GitHub Actions / Lint and Build

commentedOutCode: may want to remove commented-out code (gocritic)
// log.Printf("Chunk size: %d bytes (500 KB)", chunkSize)
// log.Printf("Total chunks: %d", totalChunks)

chunks := make([]ChunkPayload, 0, totalChunks)
chunks := make([]proplet.ChunkPayload, 0, totalChunks)
for i := range make([]struct{}, totalChunks) {
start := i * chunkSize
end := start + chunkSize
Expand All @@ -147,7 +141,7 @@ func createChunks(data []byte, containerName string) []ChunkPayload {
chunkData := data[start:end]
log.Printf("Chunk %d size: %d bytes", i, len(chunkData))

chunks = append(chunks, ChunkPayload{
chunks = append(chunks, proplet.ChunkPayload{
AppName: containerName,
ChunkIdx: i,
TotalChunks: totalChunks,
Expand All @@ -158,7 +152,7 @@ func createChunks(data []byte, containerName string) []ChunkPayload {
return chunks
}

func (c *HTTPProxyConfig) FetchFromReg(ctx context.Context, containerName string) ([]ChunkPayload, error) {
func (c *HTTPProxyConfig) FetchFromReg(ctx context.Context, containerName string) ([]proplet.ChunkPayload, error) {
fullPath := fmt.Sprintf("%s/%s", c.RegistryURL, containerName)

repo, err := remote.NewRepository(fullPath)
Expand Down
3 changes: 2 additions & 1 deletion proxy/mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"time"

"github.com/absmach/propeller/proplet"
"github.com/absmach/propeller/proxy/config"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
Expand Down Expand Up @@ -109,7 +110,7 @@ func (c *RegistryClient) Subscribe(ctx context.Context, containerChan chan<- str
return nil
}

func (c *RegistryClient) PublishContainer(ctx context.Context, chunk config.ChunkPayload) error {
func (c *RegistryClient) PublishContainer(ctx context.Context, chunk proplet.ChunkPayload) error {
data, err := json.Marshal(chunk)
if err != nil {
return fmt.Errorf("failed to marshal chunk payload: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions proxy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"

"github.com/absmach/propeller/proplet"
"github.com/absmach/propeller/proxy/config"
"github.com/absmach/propeller/proxy/mqtt"
)
Expand All @@ -16,7 +17,7 @@ type ProxyService struct {
mqttClient *mqtt.RegistryClient
logger *slog.Logger
containerChan chan string
dataChan chan config.ChunkPayload
dataChan chan proplet.ChunkPayload
}

func NewService(ctx context.Context, mqttCfg *config.MQTTProxyConfig, httpCfg *config.HTTPProxyConfig, logger *slog.Logger) (*ProxyService, error) {
Expand All @@ -32,7 +33,7 @@ func NewService(ctx context.Context, mqttCfg *config.MQTTProxyConfig, httpCfg *c
mqttClient: mqttClient,
logger: logger,
containerChan: make(chan string, 1),
dataChan: make(chan config.ChunkPayload, chunkBuffer),
dataChan: make(chan proplet.ChunkPayload, chunkBuffer),
}, nil
}

Expand Down

0 comments on commit 7427b3d

Please sign in to comment.