8000 chore(xstream): optimize stream by goroutine pools by chenquan · Pull Request #42 · chenquan/go-pkg · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore(xstream): optimize stream by goroutine pools #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions xstream/xstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ import (
"sync"
)

// empty an empty Stream.
var (
empty *Stream
pool, _ = ants.NewPool(-1)
)

func init() {
source := make(chan interface{})
close(source)
empty = &Stream{source: source}
}

type (
// FilterFunc defines the method to filter a Stream.
FilterFunc func(item interface{}) bool
Expand Down Expand Up @@ -53,25 +65,24 @@ type (
CollectorFunc func(c <-chan interface{})
)

// Input implements Collector.
func (cf CollectorFunc) Input(c <-chan interface{}) {
cf(c)
}

// Stream Represents a stream.
type Stream struct {
source <-chan interface{}
}

// empty an empty Stream.
var empty *Stream
// Input implements Collector.
func (cf CollectorFunc) Input(c <-chan interface{}) {
cf(c)
}

func init() {
source := make(chan interface{})
close(source)
empty = &Stream{source}
// -------------

func startGoroutine(f func()) {
_ = pool.Submit(f)
}

// -------------

// Empty Returns an empty stream.
func Empty() *Stream {
return empty
Expand Down Expand Up @@ -622,7 +633,3 @@ func drain(channel <-chan interface{}) {
func (s *Stream) Collection(collector Collector) {
collector.Input(s.source)
}

func startGoroutine(f func()) {
_ = ants.Submit(f)
}
0