2
0
Эх сурвалжийг харах

:wrench: Add default timeout for ledger persist calls

Ettore Di Giacinto 3 жил өмнө
parent
commit
d3f0ffb470

+ 4 - 4
api/api.go

@@ -25,7 +25,7 @@ func getFileSystem() http.FileSystem {
 	return http.FS(fsys)
 }
 
-func API(l string, ledger *blockchain.Ledger) error {
+func API(l string, defaultInterval, timeout time.Duration, ledger *blockchain.Ledger) error {
 	ec := echo.New()
 	assetHandler := http.FileServer(getFileSystem())
 
@@ -111,7 +111,7 @@ func API(l string, ledger *blockchain.Ledger) error {
 		key := c.Param("key")
 		value := c.Param("value")
 
-		ledger.Persist(context.Background(), 5*time.Second, bucket, key, value)
+		ledger.Persist(context.Background(), defaultInterval, timeout, bucket, key, value)
 		return c.JSON(http.StatusOK, announcing)
 	})
 
@@ -119,7 +119,7 @@ func API(l string, ledger *blockchain.Ledger) error {
 	ec.DELETE("/api/ledger/:bucket", func(c echo.Context) error {
 		bucket := c.Param("bucket")
 
-		ledger.AnnounceDeleteBucket(context.Background(), 5*time.Second, bucket)
+		ledger.AnnounceDeleteBucket(context.Background(), defaultInterval, timeout, bucket)
 		return c.JSON(http.StatusOK, announcing)
 	})
 
@@ -127,7 +127,7 @@ func API(l string, ledger *blockchain.Ledger) error {
 		bucket := c.Param("bucket")
 		key := c.Param("key")
 
-		ledger.AnnounceDeleteBucketKey(context.Background(), 5*time.Second, bucket, key)
+		ledger.AnnounceDeleteBucketKey(context.Background(), defaultInterval, timeout, bucket, key)
 		return c.JSON(http.StatusOK, announcing)
 	})
 

+ 3 - 1
cmd/api.go

@@ -1,6 +1,8 @@
 package cmd
 
 import (
+	"time"
+
 	"github.com/mudler/edgevpn/api"
 	"github.com/mudler/edgevpn/pkg/edgevpn"
 	"github.com/urfave/cli"
@@ -30,7 +32,7 @@ A simple UI interface is available to display network data.`,
 				return err
 			}
 			ledger, _ := e.Ledger()
-			return api.API(c.String("listen"), ledger)
+			return api.API(c.String("listen"), 5*time.Second, 20*time.Second, ledger)
 		},
 	}
 }

+ 2 - 1
cmd/main.go

@@ -4,6 +4,7 @@ import (
 	"encoding/base64"
 	"fmt"
 	"os"
+	"time"
 
 	"github.com/mudler/edgevpn/api"
 	"github.com/mudler/edgevpn/pkg/edgevpn"
@@ -79,7 +80,7 @@ func Main() func(c *cli.Context) error {
 		}
 
 		if c.Bool("api") {
-			go api.API(c.String("api-listen"), ledger)
+			go api.API(c.String("api-listen"), 5*time.Second, 20*time.Second, ledger)
 		}
 
 		if err := e.Start(); err != nil {

+ 9 - 6
pkg/blockchain/ledger.go

@@ -140,8 +140,11 @@ func (l *Ledger) Announce(ctx context.Context, t time.Duration, async func()) {
 }
 
 // AnnounceDeleteBucket Announce a deletion of a bucket. It stops when the bucket is deleted
-func (l *Ledger) AnnounceDeleteBucket(ctx context.Context, interval time.Duration, bucket string) {
-	del, cancel := context.WithCancel(ctx)
+// It takes an interval time and a max timeout.
+// It is best effort, and the timeout is necessary, or we might flood network with requests
+// if more writers are attempting to write to the same resource
+func (l *Ledger) AnnounceDeleteBucket(ctx context.Context, interval, timeout time.Duration, bucket string) {
+	del, cancel := context.WithTimeout(ctx, timeout)
 
 	l.Announce(del, interval, func() {
 		_, exists := l.CurrentData()[bucket]
@@ -154,8 +157,8 @@ func (l *Ledger) AnnounceDeleteBucket(ctx context.Context, interval time.Duratio
 }
 
 // AnnounceDeleteBucketKey Announce a deletion of a key from a bucket. It stops when the key is deleted
-func (l *Ledger) AnnounceDeleteBucketKey(ctx context.Context, interval time.Duration, bucket, key string) {
-	del, cancel := context.WithCancel(ctx)
+func (l *Ledger) AnnounceDeleteBucketKey(ctx context.Context, interval, timeout time.Duration, bucket, key string) {
+	del, cancel := context.WithTimeout(ctx, timeout)
 
 	l.Announce(del, interval, func() {
 		_, exists := l.CurrentData()[bucket][key]
@@ -168,8 +171,8 @@ func (l *Ledger) AnnounceDeleteBucketKey(ctx context.Context, interval time.Dura
 }
 
 // Persist Keeps announcing something into the blockchain until it is reconciled
-func (l *Ledger) Persist(ctx context.Context, interval time.Duration, bucket, key string, value interface{}) {
-	put, cancel := context.WithCancel(ctx)
+func (l *Ledger) Persist(ctx context.Context, interval, timeout time.Duration, bucket, key string, value interface{}) {
+	put, cancel := context.WithTimeout(ctx, timeout)
 
 	l.Announce(put, interval, func() {
 		v, exists := l.CurrentData()[bucket][key]

+ 1 - 0
pkg/edgevpn/interface.go

@@ -1,3 +1,4 @@
+//go:build !windows
 // +build !windows
 
 package edgevpn

+ 1 - 0
pkg/edgevpn/interface_windows.go

@@ -1,3 +1,4 @@
+//go:build windows
 // +build windows
 
 package edgevpn