44 "fmt"
55 "net/http"
66 "regexp"
7+ "time"
78 "unicode/utf8"
89
910 "github.com/rodaine/table"
@@ -52,8 +53,8 @@ type ClosestLocationResponse struct {
5253 Server string
5354}
5455
55- func (c * LocationsClient ) Closest (regionUrl string ) (string , error ) {
56- r , err := c .client .Get (regionUrl , nil )
56+ func (c * LocationsClient ) Closest () (string , error ) {
57+ r , err := c .client .Get ("https://region.turso.io" , nil )
5758 if err != nil {
5859 return "" , fmt .Errorf ("failed to request closest: %s" , err )
5960 }
@@ -72,6 +73,35 @@ func (c *LocationsClient) Closest(regionUrl string) (string, error) {
7273 return data .Server , nil
7374}
7475
76+ func ProbeLocation (location string ) * time.Duration {
77+ client := & http.Client {Timeout : 2 * time .Second }
78+ req , err := http .NewRequest ("GET" , "http://region.turso.io:8080/" , nil )
79+ if err != nil {
80+ return nil
81+ }
82+ req .Header .Add ("fly-prefer-region" , location )
83+
84+ start := time .Now ()
85+ r , err := client .Do (req )
86+ if err != nil {
87+ return nil
88+ }
89+ defer r .Body .Close ()
90+
91+ dur := time .Since (start )
92+ if r .StatusCode != http .StatusOK {
93+ return nil
94+ }
95+ data , err := unmarshal [ClosestLocationResponse ](r )
96+ if err != nil {
97+ return nil
98+ }
99+ if data .Server != location {
100+ return nil
101+ }
102+ return & dur
103+ }
104+
75105func LocationsTable (columns []interface {}) table.Table {
76106 regex := regexp .MustCompile (`\x1b\[[0-9;]*m` )
77107 return table .New (columns ... ).WithWidthFunc (func (s string ) int {
0 commit comments