Skip to content

Commit

Permalink
Drop custom env parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
p2004a committed Aug 24, 2023
1 parent 9ffea50 commit 4520cb5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 84 deletions.
60 changes: 0 additions & 60 deletions cmd/replicationprober/envhelpers.go

This file was deleted.

72 changes: 48 additions & 24 deletions cmd/replicationprober/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"fmt"
"log"
"net/http"
"os"
"sync"
"time"

"github.com/InfluxCommunity/influxdb3-go/influxdb3"
"github.com/caarlos0/env/v9"
"github.com/p2004a/spring-rapid-syncer/pkg/bunny"
"github.com/p2004a/spring-rapid-syncer/pkg/sfcache"
)
Expand Down Expand Up @@ -49,26 +49,55 @@ func getExpectedStorageRegions(ctx context.Context, bunnyClient *bunny.Client, s
return regions, nil
}

type config struct {
Bunny struct {
AccessKey string
StorageZone string
} `envPrefix:"BUNNY_"`
InfluxDb struct {
Url string
Token string
Database string
} `envPrefix:"INFLUXDB_"`
BaseUrl string `envDefault:"https://repos-cdn.beyondallreason.dev"`
MaxRegionDistanceKm float64 `envDefault:"400"`
StorageEdgeMapCacheDuration time.Duration `envDefault:"24h"`
VersionGzCacheDuration time.Duration `envDefault:"10s"`
RefreshReplicationCanaryPeriod time.Duration `envDefault:"5m"`
CheckReplicationStatusPeriod time.Duration `envDefault:"2m"`
CheckRedirectStatusPeriod time.Duration `envDefault:"2m"`
EnableStorageReplicationProber bool `envDefault:"true"`
EnableRedirectStatusProber bool `envDefault:"true"`
Port string `envDefault:"8080"`
}

func main() {
bunnyAccessKey := getRequiredStrEnv("BUNNY_ACCESS_KEY")
bunnyClient := bunny.NewClient(bunnyAccessKey)
cfg := config{}
if err := env.ParseWithOptions(&cfg, env.Options{
RequiredIfNoDef: true,
UseFieldNameByDefault: true,
}); err != nil {
log.Fatalf("%+v\n", err)

}

bunnyClient := bunny.NewClient(cfg.Bunny.AccessKey)

bunnyStorageZone := getRequiredStrEnv("BUNNY_STORAGE_ZONE")
ctx := context.Background()
bunnyStorageZoneClient, err := bunnyClient.NewStorageZoneClient(ctx, bunnyStorageZone)
bunnyStorageZoneClient, err := bunnyClient.NewStorageZoneClient(ctx, cfg.Bunny.StorageZone)
if err != nil {
log.Fatalf("Failed to create Bunny storage zone client: %v", err)
}

expectedStorageRegions, err := getExpectedStorageRegions(ctx, bunnyClient, bunnyStorageZone)
expectedStorageRegions, err := getExpectedStorageRegions(ctx, bunnyClient, cfg.Bunny.StorageZone)
if err != nil {
log.Fatalf("Failed to get expected storage regions: %v", err)
}

influxdbClient, err := influxdb3.New(influxdb3.ClientConfig{
Host: getRequiredStrEnv("INFLUXDB_URL"),
Token: getRequiredStrEnv("INFLUXDB_TOKEN"),
Database: getRequiredStrEnv("INFLUXDB_DATABASE"),
Host: cfg.InfluxDb.Url,
Token: cfg.InfluxDb.Token,
Database: cfg.InfluxDb.Database,
})
if err != nil {
log.Fatalf("Failed to create InfluxDB client: %v", err)
Expand All @@ -80,25 +109,25 @@ func main() {
},
bunny: bunnyClient,
bunnyStorageZone: bunnyStorageZoneClient,
baseUrl: getStrEnv("BASE_URL", "https://repos-cdn.beyondallreason.dev"),
maxRegionDistanceKm: getFloatEnv("MAX_REGION_DISTANCE_KM", 400),
baseUrl: cfg.BaseUrl,
maxRegionDistanceKm: cfg.MaxRegionDistanceKm,
expectedStorageRegions: expectedStorageRegions,
storageEdgeMapCache: sfcache.Cache[StorageEdgeMap]{
Timeout: getDurationEnv("STORAGE_EDGE_MAP_CACHE_DURATION", time.Hour*24),
Timeout: cfg.StorageEdgeMapCacheDuration,
},
versionsGzCache: sfcache.Cache[[]*replicatedFile]{
Timeout: getDurationEnv("VERSION_GZ_CACHE_DURATION", time.Second*10),
Timeout: cfg.VersionGzCacheDuration,
},
refreshReplicationCanaryPeriod: getDurationEnv("REFRESH_REPLICATION_CANARY_PERIOD", time.Minute*5),
checkReplicationStatusPeriod: getDurationEnv("CHECK_REPLICATION_STATUS_PERIOD", time.Minute*2),
refreshReplicationCanaryPeriod: cfg.RefreshReplicationCanaryPeriod,
checkReplicationStatusPeriod: cfg.CheckRedirectStatusPeriod,
influxdbClient: influxdbClient,
}

http.HandleFunc("/storageedgemap", server.HandleStorageEdgeMap)
http.HandleFunc("/replicationstatus_versionsgz", server.HandleReplicationStatusVersionsGz)
http.HandleFunc("/replicationstatus", server.HandleReplicationStatus)

if getBoolEnv("ENABLE_STORAGE_REPLICATION_PROBER", true) {
if cfg.EnableStorageReplicationProber {
go server.startCanaryUpdater(ctx)
go server.startReplicationStatusChecker(ctx)
}
Expand All @@ -108,19 +137,14 @@ func main() {
influxdbClient: influxdbClient,
repo: versionsGzRepo,
versionGzUrl: server.baseUrl + versionsGzFile,
probePeriod: getDurationEnv("CHECK_REDIRECT_STATUS_PERIOD", time.Minute*2),
probePeriod: cfg.CheckRedirectStatusPeriod,
edgeServerRefreshPeriod: time.Hour * 1,
}
if getBoolEnv("ENABLE_REDIRECT_STATUS_PROBER", true) {
if cfg.EnableRedirectStatusProber {
go redirectProber.startProber(ctx)
}

port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
if err := http.ListenAndServe(":"+port, nil); err != nil {
if err := http.ListenAndServe(":"+cfg.Port, nil); err != nil {
log.Fatal(err)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/apache/arrow/go/v12 v12.0.1 // indirect
github.com/apache/thrift v0.16.0 // indirect
github.com/caarlos0/env/v9 v9.0.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/apache/arrow/go/v12 v12.0.1 h1:JsR2+hzYYjgSUkBSaahpqCetqZMr76djX80fF/
github.com/apache/arrow/go/v12 v12.0.1/go.mod h1:weuTY7JvTG/HDPtMQxEUp7pU73vkLWMLpY67QwZ/WWw=
github.com/apache/thrift v0.16.0 h1:qEy6UW60iVOlUy+b9ZR0d5WzUWYGOo4HfopoyBaNmoY=
github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU=
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down

0 comments on commit 4520cb5

Please sign in to comment.