Skip to content

Commit

Permalink
Merge pull request #929 from tursodatabase/aws-improvements
Browse files Browse the repository at this point in the history
AWS CLI Improvements
  • Loading branch information
glommer authored Dec 2, 2024
2 parents 81f642b + f3bead8 commit 4cf5c85
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 93 deletions.
43 changes: 0 additions & 43 deletions internal/cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
"fmt"
"math"
"os"
"sync"

"github.com/spf13/cobra"
"github.com/tursodatabase/turso-cli/internal"
Expand Down Expand Up @@ -144,52 +142,11 @@ func envAccessToken() (string, error) {
return token, nil
}

type latMap struct {
id string
lat int
}

func locations(client *turso.Client) (map[string]string, error) {
settings, _ := settings.ReadSettings()
return readLocations(settings, client)
}

func latencies(client *turso.Client) (map[string]int, error) {
settings, _ := settings.ReadSettings()
locations, err := readLocations(settings, client)
if err != nil {
return nil, err
}

var wg sync.WaitGroup
latencies := make(map[string]int)
c := make(chan latMap, len(locations))
for id := range locations {
wg.Add(1)
go func(id string) {
defer wg.Done()
measure := math.MaxInt
// XXX: Running this in different goroutines makes all latencies dogslow.
// Not sure if this is contention at the client or API level
for i := 0; i < 3; i++ {
d := turso.ProbeLocation(id)
if d != nil {
measure = int(math.Min(float64(d.Milliseconds()), float64(measure)))
}
}
c <- latMap{id: id, lat: measure}
}(id)
}

wg.Wait()
close(c)

for kvp := range c {
latencies[kvp.id] = kvp.lat
}
return latencies, nil
}

func readLocations(settings *settings.Settings, client *turso.Client) (map[string]string, error) {
if locations := locationsCache(); locations != nil {
return locations, nil
Expand Down
72 changes: 31 additions & 41 deletions internal/cmd/db_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

import (
"fmt"
"math"
"sort"
"strings"

"github.com/spf13/cobra"
"github.com/tursodatabase/turso-cli/internal"
Expand All @@ -13,7 +13,6 @@ import (

func init() {
dbCmd.AddCommand(regionsCmd)
addLatencyFlag(regionsCmd)
}

var regionsCmd = &cobra.Command{
Expand All @@ -39,55 +38,46 @@ var regionsCmd = &cobra.Command{

columns := make([]interface{}, 0)

lats := make(map[string]int)
var ids []string
if latencyFlag {
lats, err = latencies(client)
if err != nil {
return err
}
ids = maps.Keys(lats)
sort.Slice(ids, func(i, j int) bool {
return lats[ids[i]] < lats[ids[j]]
})
columns = append(columns, "ID")
columns = append(columns, "LOCATION")
columns = append(columns, "LATENCY↓")
} else {
ids = maps.Keys(locations)
sort.Strings(ids)
columns = append(columns, "ID↓")
columns = append(columns, "LOCATION")
}
ids := maps.Keys(locations)

tbl := turso.LocationsTable(columns)
awsIds := make([]string, 0, len(ids))
flyIds := make([]string, 0, len(ids))

for _, location := range ids {
description := locations[location]
lat, ok := lats[location]
var latency string
if ok && lat != math.MaxInt {
latency = fmt.Sprintf("%dms", lat)
for _, id := range ids {
if strings.HasPrefix(id, "aws-") {
awsIds = append(awsIds, id)
} else {
latency = "???"
flyIds = append(flyIds, id)
}
}

sort.Strings(awsIds)
sort.Strings(flyIds)

columns = append(columns, "ID↓")
columns = append(columns, "LOCATION")

flyTbl := turso.LocationsTable(columns)
awsTbl := turso.LocationsTable(columns)

fmt.Println(internal.Emph("Fly.io Regions:"))
for _, location := range flyIds {
description := locations[location]
if location == closest {
description = fmt.Sprintf("%s [default]", description)
if latencyFlag {
tbl.AddRow(internal.Emph(location), internal.Emph(description), internal.Emph(latency))
} else {
tbl.AddRow(internal.Emph(location), internal.Emph(description))
}
flyTbl.AddRow(internal.Emph(location), internal.Emph(description))
} else {
if latencyFlag {
tbl.AddRow(location, description, latency)
} else {
tbl.AddRow(location, description)
}
flyTbl.AddRow(location, description)
}
}
tbl.Print()
flyTbl.Print()

fmt.Println(internal.Emph("\nAWS (beta) Regions:"))
for _, location := range awsIds {
description := locations[location]
awsTbl.AddRow(location, description)
}
awsTbl.Print()
return nil
},
}
9 changes: 0 additions & 9 deletions internal/cmd/latencies.go

This file was deleted.

0 comments on commit 4cf5c85

Please sign in to comment.