Переведите приём и чтение вложений на асинхронный контракт
This commit is contained in:
@@ -2,17 +2,20 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type S3 struct {
|
||||
Endpoint, Region, Bucket string
|
||||
AccessKey, SecretKey string
|
||||
MaxBytes, MaxPixels int64
|
||||
StagingRetention time.Duration
|
||||
}
|
||||
|
||||
func LoadS3(lookup Lookup) (S3, error) {
|
||||
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000}
|
||||
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000, StagingRetention: 24 * time.Hour}
|
||||
for key, dest := range map[string]*string{"S3_ENDPOINT": &cfg.Endpoint, "S3_REGION": &cfg.Region, "S3_BUCKET": &cfg.Bucket} {
|
||||
if v, ok := lookup(key); ok {
|
||||
*dest = v
|
||||
@@ -38,6 +41,14 @@ func LoadS3(lookup Lookup) (S3, error) {
|
||||
*p.dest = n
|
||||
}
|
||||
}
|
||||
if raw, ok := lookup("STAGING_RETENTION_SECONDS"); ok {
|
||||
const maxSeconds = math.MaxInt64 / int64(time.Second)
|
||||
n, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || n < 1 || n > maxSeconds {
|
||||
return S3{}, fmt.Errorf("STAGING_RETENTION_SECONDS must be between 1 and %d", maxSeconds)
|
||||
}
|
||||
cfg.StagingRetention = time.Duration(n) * time.Second
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package config_test
|
||||
import (
|
||||
"messenger/internal/platform/config"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestS3LimitsAndSecretFiles(t *testing.T) {
|
||||
@@ -25,3 +26,21 @@ func TestS3LimitsAndSecretFiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestS3RejectsInvalidStagingRetention(t *testing.T) {
|
||||
values := map[string]string{"S3_ACCESS_KEY_ID_FILE": secretFile(t, "access\n"), "S3_SECRET_ACCESS_KEY_FILE": secretFile(t, "private\n")}
|
||||
cfg, err := config.LoadS3(env(values))
|
||||
if err != nil || cfg.StagingRetention != 24*time.Hour {
|
||||
t.Fatalf("default retention %v %v", cfg.StagingRetention, err)
|
||||
}
|
||||
values["STAGING_RETENTION_SECONDS"] = "7200"
|
||||
cfg, err = config.LoadS3(env(values))
|
||||
if err != nil || cfg.StagingRetention != 2*time.Hour {
|
||||
t.Fatalf("custom retention %v %v", cfg.StagingRetention, err)
|
||||
}
|
||||
for _, value := range []string{"0", "-1", "abc", "9223372036854775807"} {
|
||||
values["STAGING_RETENTION_SECONDS"] = value
|
||||
if _, err := config.LoadS3(env(values)); err == nil {
|
||||
t.Errorf("accepted invalid retention %s", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// Package faultbarrier provides one-shot synchronization for controlled process-failure experiments.
|
||||
package faultbarrier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Wait claims an armed phase and blocks until its hit file disappears.
|
||||
// A nonempty id identifies a newly staged attachment before its job exists.
|
||||
func Wait(ctx context.Context, base, id string) error {
|
||||
if err := os.Rename(base+".arm", base+".hit"); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if id != "" {
|
||||
if err := os.WriteFile(base+".id", []byte(id), 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
t := time.NewTicker(50 * time.Millisecond)
|
||||
defer t.Stop()
|
||||
for {
|
||||
if _, err := os.Stat(base + ".hit"); os.IsNotExist(err) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-t.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package faultbarrier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestArmedBarrierRecordsIDAndWaitsForRelease(t *testing.T) {
|
||||
base := filepath.Join(t.TempDir(), "digest-staged")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
if err := Wait(ctx, base, "unused"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(base + ".id"); !os.IsNotExist(err) {
|
||||
t.Fatal("unarmed barrier created identity")
|
||||
}
|
||||
if err := os.WriteFile(base+".arm", nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- Wait(ctx, base, "attachment-id") }()
|
||||
for {
|
||||
b, err := os.ReadFile(base + ".id")
|
||||
if err == nil && string(b) == "attachment-id" {
|
||||
break
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
t.Fatal("missing staged identity")
|
||||
case <-time.After(10 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
t.Fatal("continued before release")
|
||||
default:
|
||||
}
|
||||
if err := os.Remove(base + ".hit"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := <-done; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user