diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-01-16 13:07:20 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:21 +0800 |
commit | 4095f8c34043a2418dece4d5affbceffe0ca55da (patch) | |
tree | 458337ca2bd260006f6e22a37db88933da63e06f /vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go | |
parent | ea0c0a11ccbefffc2dbb5e323f23997b91239c56 (diff) | |
download | go-tangerine-4095f8c34043a2418dece4d5affbceffe0ca55da.tar go-tangerine-4095f8c34043a2418dece4d5affbceffe0ca55da.tar.gz go-tangerine-4095f8c34043a2418dece4d5affbceffe0ca55da.tar.bz2 go-tangerine-4095f8c34043a2418dece4d5affbceffe0ca55da.tar.lz go-tangerine-4095f8c34043a2418dece4d5affbceffe0ca55da.tar.xz go-tangerine-4095f8c34043a2418dece4d5affbceffe0ca55da.tar.zst go-tangerine-4095f8c34043a2418dece4d5affbceffe0ca55da.zip |
vendor: sync to latest core (#154)
* vendor: sync to latest core with BA3.0
* params: Update dmoment
Diffstat (limited to 'vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go')
-rw-r--r-- | vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go index 8c9f77a69..8486d2854 100644 --- a/vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go +++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/utils/utils.go @@ -18,6 +18,7 @@ package utils import ( + "context" "fmt" "github.com/dexon-foundation/dexon-consensus/common" @@ -92,3 +93,36 @@ func VerifyDKGComplaint( } return ok, nil } + +// LaunchDummyReceiver launches a go routine to receive from the receive +// channel of a network module. An context is required to stop the go routine +// automatically. An optinal message handler could be provided. +func LaunchDummyReceiver( + ctx context.Context, recv <-chan interface{}, handler func(interface{})) ( + context.CancelFunc, <-chan struct{}) { + var ( + dummyCtx, dummyCancel = context.WithCancel(ctx) + finishedChan = make(chan struct{}, 1) + ) + go func() { + defer func() { + finishedChan <- struct{}{} + }() + loop: + for { + select { + case <-dummyCtx.Done(): + break loop + case v, ok := <-recv: + if !ok { + panic(fmt.Errorf( + "receive channel is closed before dummy receiver")) + } + if handler != nil { + handler(v) + } + } + } + }() + return dummyCancel, finishedChan +} |