Skip to content

Commit

Permalink
🔧 Add default timeout for ledger persist calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mudler committed Dec 7, 2021
1 parent c65e401 commit d3f0ffb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
8 changes: 4 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -111,23 +111,23 @@ 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)
})

// Delete data from ledger
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)
})

ec.DELETE("/api/ledger/:bucket/:key", func(c echo.Context) 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)
})

Expand Down
4 changes: 3 additions & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"time"

"github.com/mudler/edgevpn/api"
"github.com/mudler/edgevpn/pkg/edgevpn"
"github.com/urfave/cli"
Expand Down Expand Up @@ -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)
},
}
}
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"os"
"time"

"github.com/mudler/edgevpn/api"
"github.com/mudler/edgevpn/pkg/edgevpn"
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 9 additions & 6 deletions pkg/blockchain/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions pkg/edgevpn/interface.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package edgevpn
Expand Down
1 change: 1 addition & 0 deletions pkg/edgevpn/interface_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package edgevpn
Expand Down

0 comments on commit d3f0ffb

Please sign in to comment.