[go: nahoru, domu]

Skip to content

Commit

Permalink
update to ubuntu 20.04, golang 1.13, add head timeout (geohot#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregjhogan committed Jan 8, 2021
1 parent 012c193 commit a0180ed
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM ubuntu:16.04
FROM ubuntu:20.04

ENV DEBIAN_FRONTEND noninteractive

# system basics
RUN apt-get update && \
Expand Down
8 changes: 6 additions & 2 deletions src/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"net/http"
"sort"
"strings"
"context"
"time"
)

// *** DB Type ***
Expand Down Expand Up @@ -178,8 +180,10 @@ func remote_get(remote string) (string, error) {
return string(body), nil
}

func remote_head(remote string) bool {
req, err := http.NewRequest("HEAD", remote, nil)
func remote_head(remote string, timeout time.Duration) bool {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "HEAD", remote, nil)
if err != nil {
return false
}
Expand Down
3 changes: 3 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type App struct {
subvolumes int
protect bool
md5sum bool
voltimeout time.Duration
}

func (a *App) UnlockKey(key []byte) {
Expand Down Expand Up @@ -68,6 +69,7 @@ func main() {
pvolumes := flag.String("volumes", "", "Volumes to use for storage, comma separated")
protect := flag.Bool("protect", false, "Force UNLINK before DELETE")
md5sum := flag.Bool("md5sum", true, "Calculate and store MD5 checksum of values")
voltimeout := flag.Duration("voltimeout", 1*time.Second, "Volume servers must respond to GET/HEAD requests in this amount of time or they are considered down, as duration")
flag.Parse()

volumes := strings.Split(*pvolumes, ",")
Expand Down Expand Up @@ -102,6 +104,7 @@ func main() {
subvolumes: *subvolumes,
protect: *protect,
md5sum: *md5sum,
voltimeout: *voltimeout,
}

if command == "server" {
Expand Down
3 changes: 2 additions & 1 deletion src/rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sync"
"strings"
"time"
)

type RebalanceRequest struct {
Expand All @@ -18,7 +19,7 @@ func rebalance(a *App, req RebalanceRequest) bool {
// find the volumes that are real
rvolumes := make([]string, 0)
for _, rv := range req.volumes {
if remote_head(fmt.Sprintf("http://%s%s", rv, kp)) {
if remote_head(fmt.Sprintf("http://%s%s", rv, kp), 1*time.Second) {
rvolumes = append(rvolumes, rv)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
good := false
for _, vn := range rand.Perm(len(rec.rvolumes)) {
remote = fmt.Sprintf("http://%s%s", rec.rvolumes[vn], key2path(key))
if remote_head(remote) {
if remote_head(remote, a.voltimeout) {
good = true
break
}
Expand Down
2 changes: 1 addition & 1 deletion tools/bringup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ PORT=3003 ./volume /tmp/volume3/ &
PORT=3004 ./volume /tmp/volume4/ &
PORT=3005 ./volume /tmp/volume5/ &

./mkv -volumes localhost:3001,localhost:3002,localhost:3003,localhost:3004,localhost:3005 -db /tmp/indexdb/ server
./mkv -port 3000 -volumes localhost:3001,localhost:3002,localhost:3003,localhost:3004,localhost:3005 -db /tmp/indexdb/ server

0 comments on commit a0180ed

Please sign in to comment.