-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikita Neznaemov
committed
Oct 2, 2023
1 parent
9c512bc
commit c7ea95d
Showing
12 changed files
with
150 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package observer | ||
|
||
import ( | ||
hlfproto "github.com/s7techlab/hlf-sdk-go/proto" | ||
) | ||
|
||
type ( | ||
ParsedBlock struct { | ||
Block *hlfproto.Block // parsed block | ||
BlockOriginal *hlfproto.Block // here is original block before transformation if it is, otherwise it's nil | ||
Channel string | ||
Error error | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package observer | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"sync" | ||
) | ||
|
||
type StreamParsed interface { | ||
Subscribe() (ch chan *ParsedBlock, closer func()) | ||
} | ||
|
||
type ParsedBlocksStream struct { | ||
connectionsParsed map[string]chan *ParsedBlock | ||
mu *sync.RWMutex | ||
|
||
isWork bool | ||
cancelObserve context.CancelFunc | ||
} | ||
|
||
func NewParsedBlocksStream() *ParsedBlocksStream { | ||
return &ParsedBlocksStream{ | ||
connectionsParsed: make(map[string]chan *ParsedBlock), | ||
mu: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (b *ParsedBlocksStream) Observe(ctx context.Context, blocks <-chan *ParsedBlock) { | ||
if b.isWork { | ||
return | ||
} | ||
|
||
// ctxObserve using for nested control process without stopped primary context | ||
ctxObserve, cancel := context.WithCancel(ctx) | ||
b.cancelObserve = cancel | ||
|
||
go func() { | ||
defer func() { | ||
for connName := range b.connectionsParsed { | ||
b.closeChannel(connName) | ||
} | ||
}() | ||
|
||
b.isWork = true | ||
|
||
for { | ||
select { | ||
case <-ctxObserve.Done(): | ||
// If primary context is done then cancel ctxObserver | ||
b.cancelObserve() | ||
return | ||
|
||
case block, ok := <-blocks: | ||
if !ok { | ||
return | ||
} | ||
|
||
b.mu.RLock() | ||
for _, connection := range b.connectionsParsed { | ||
connection <- block | ||
} | ||
b.mu.RUnlock() | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (b *ParsedBlocksStream) Subscribe() (chan *ParsedBlock, func()) { | ||
b.mu.Lock() | ||
newConnection := make(chan *ParsedBlock) | ||
name := "channel-" + strconv.Itoa(len(b.connectionsParsed)) | ||
b.connectionsParsed[name] = newConnection | ||
b.mu.Unlock() | ||
|
||
closer := func() { b.closeChannel(name) } | ||
|
||
return newConnection, closer | ||
} | ||
|
||
func (b *ParsedBlocksStream) SubscribeParsed() (chan *ParsedBlock, func()) { | ||
b.mu.Lock() | ||
newConnection := make(chan *ParsedBlock) | ||
name := "channel-" + strconv.Itoa(len(b.connectionsParsed)) | ||
b.connectionsParsed[name] = newConnection | ||
b.mu.Unlock() | ||
|
||
closer := func() { b.closeChannel(name) } | ||
|
||
return newConnection, closer | ||
} | ||
|
||
func (b *ParsedBlocksStream) closeChannel(name string) { | ||
b.mu.Lock() | ||
close(b.connectionsParsed[name]) | ||
delete(b.connectionsParsed, name) | ||
b.mu.Unlock() | ||
} | ||
|
||
func (b *ParsedBlocksStream) Stop() { | ||
if b.cancelObserve != nil { | ||
b.cancelObserve() | ||
} | ||
b.isWork = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters