stream.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. package fx
  2. import (
  3. "sort"
  4. "sync"
  5. "github.com/zeromicro/go-zero/core/collection"
  6. "github.com/zeromicro/go-zero/core/lang"
  7. "github.com/zeromicro/go-zero/core/threading"
  8. )
  9. const (
  10. defaultWorkers = 16
  11. minWorkers = 1
  12. )
  13. type (
  14. rxOptions struct {
  15. unlimitedWorkers bool
  16. workers int
  17. }
  18. // FilterFunc defines the method to filter a Stream.
  19. FilterFunc func(item interface{}) bool
  20. // ForAllFunc defines the method to handle all elements in a Stream.
  21. ForAllFunc func(pipe <-chan interface{})
  22. // ForEachFunc defines the method to handle each element in a Stream.
  23. ForEachFunc func(item interface{})
  24. // GenerateFunc defines the method to send elements into a Stream.
  25. GenerateFunc func(source chan<- interface{})
  26. // KeyFunc defines the method to generate keys for the elements in a Stream.
  27. KeyFunc func(item interface{}) interface{}
  28. // LessFunc defines the method to compare the elements in a Stream.
  29. LessFunc func(a, b interface{}) bool
  30. // MapFunc defines the method to map each element to another object in a Stream.
  31. MapFunc func(item interface{}) interface{}
  32. // Option defines the method to customize a Stream.
  33. Option func(opts *rxOptions)
  34. // ParallelFunc defines the method to handle elements parallelly.
  35. ParallelFunc func(item interface{})
  36. // ReduceFunc defines the method to reduce all the elements in a Stream.
  37. ReduceFunc func(pipe <-chan interface{}) (interface{}, error)
  38. // WalkFunc defines the method to walk through all the elements in a Stream.
  39. WalkFunc func(item interface{}, pipe chan<- interface{})
  40. // A Stream is a stream that can be used to do stream processing.
  41. Stream struct {
  42. source <-chan interface{}
  43. }
  44. )
  45. // Concat returns a concatenated Stream.
  46. func Concat(s Stream, others ...Stream) Stream {
  47. return s.Concat(others...)
  48. }
  49. // From constructs a Stream from the given GenerateFunc.
  50. func From(generate GenerateFunc) Stream {
  51. source := make(chan interface{})
  52. threading.GoSafe(func() {
  53. defer close(source)
  54. generate(source)
  55. })
  56. return Range(source)
  57. }
  58. // Just converts the given arbitrary items to a Stream.
  59. func Just(items ...interface{}) Stream {
  60. source := make(chan interface{}, len(items))
  61. for _, item := range items {
  62. source <- item
  63. }
  64. close(source)
  65. return Range(source)
  66. }
  67. // Range converts the given channel to a Stream.
  68. func Range(source <-chan interface{}) Stream {
  69. return Stream{
  70. source: source,
  71. }
  72. }
  73. // AllMach returns whether all elements of this stream match the provided predicate.
  74. // May not evaluate the predicate on all elements if not necessary for determining the result.
  75. // If the stream is empty then true is returned and the predicate is not evaluated.
  76. func (s Stream) AllMach(predicate func(item interface{}) bool) bool {
  77. for item := range s.source {
  78. if !predicate(item) {
  79. // make sure the former goroutine not block, and current func returns fast.
  80. go drain(s.source)
  81. return false
  82. }
  83. }
  84. return true
  85. }
  86. // AnyMach returns whether any elements of this stream match the provided predicate.
  87. // May not evaluate the predicate on all elements if not necessary for determining the result.
  88. // If the stream is empty then false is returned and the predicate is not evaluated.
  89. func (s Stream) AnyMach(predicate func(item interface{}) bool) bool {
  90. for item := range s.source {
  91. if predicate(item) {
  92. // make sure the former goroutine not block, and current func returns fast.
  93. go drain(s.source)
  94. return true
  95. }
  96. }
  97. return false
  98. }
  99. // Buffer buffers the items into a queue with size n.
  100. // It can balance the producer and the consumer if their processing throughput don't match.
  101. func (s Stream) Buffer(n int) Stream {
  102. if n < 0 {
  103. n = 0
  104. }
  105. source := make(chan interface{}, n)
  106. go func() {
  107. for item := range s.source {
  108. source <- item
  109. }
  110. close(source)
  111. }()
  112. return Range(source)
  113. }
  114. // Concat returns a Stream that concatenated other streams
  115. func (s Stream) Concat(others ...Stream) Stream {
  116. source := make(chan interface{})
  117. go func() {
  118. group := threading.NewRoutineGroup()
  119. group.Run(func() {
  120. for item := range s.source {
  121. source <- item
  122. }
  123. })
  124. for _, each := range others {
  125. each := each
  126. group.Run(func() {
  127. for item := range each.source {
  128. source <- item
  129. }
  130. })
  131. }
  132. group.Wait()
  133. close(source)
  134. }()
  135. return Range(source)
  136. }
  137. // Count counts the number of elements in the result.
  138. func (s Stream) Count() (count int) {
  139. for range s.source {
  140. count++
  141. }
  142. return
  143. }
  144. // Distinct removes the duplicated items base on the given KeyFunc.
  145. func (s Stream) Distinct(fn KeyFunc) Stream {
  146. source := make(chan interface{})
  147. threading.GoSafe(func() {
  148. defer close(source)
  149. keys := make(map[interface{}]lang.PlaceholderType)
  150. for item := range s.source {
  151. key := fn(item)
  152. if _, ok := keys[key]; !ok {
  153. source <- item
  154. keys[key] = lang.Placeholder
  155. }
  156. }
  157. })
  158. return Range(source)
  159. }
  160. // Done waits all upstreaming operations to be done.
  161. func (s Stream) Done() {
  162. drain(s.source)
  163. }
  164. // Filter filters the items by the given FilterFunc.
  165. func (s Stream) Filter(fn FilterFunc, opts ...Option) Stream {
  166. return s.Walk(func(item interface{}, pipe chan<- interface{}) {
  167. if fn(item) {
  168. pipe <- item
  169. }
  170. }, opts...)
  171. }
  172. // First returns the first item, nil if no items.
  173. func (s Stream) First() interface{} {
  174. for item := range s.source {
  175. // make sure the former goroutine not block, and current func returns fast.
  176. go drain(s.source)
  177. return item
  178. }
  179. return nil
  180. }
  181. // ForAll handles the streaming elements from the source and no later streams.
  182. func (s Stream) ForAll(fn ForAllFunc) {
  183. fn(s.source)
  184. // avoid goroutine leak on fn not consuming all items.
  185. go drain(s.source)
  186. }
  187. // ForEach seals the Stream with the ForEachFunc on each item, no successive operations.
  188. func (s Stream) ForEach(fn ForEachFunc) {
  189. for item := range s.source {
  190. fn(item)
  191. }
  192. }
  193. // Group groups the elements into different groups based on their keys.
  194. func (s Stream) Group(fn KeyFunc) Stream {
  195. groups := make(map[interface{}][]interface{})
  196. for item := range s.source {
  197. key := fn(item)
  198. groups[key] = append(groups[key], item)
  199. }
  200. source := make(chan interface{})
  201. go func() {
  202. for _, group := range groups {
  203. source <- group
  204. }
  205. close(source)
  206. }()
  207. return Range(source)
  208. }
  209. // Head returns the first n elements in p.
  210. func (s Stream) Head(n int64) Stream {
  211. if n < 1 {
  212. panic("n must be greater than 0")
  213. }
  214. source := make(chan interface{})
  215. go func() {
  216. for item := range s.source {
  217. n--
  218. if n >= 0 {
  219. source <- item
  220. }
  221. if n == 0 {
  222. // let successive method go ASAP even we have more items to skip
  223. close(source)
  224. // why we don't just break the loop, and drain to consume all items.
  225. // because if breaks, this former goroutine will block forever,
  226. // which will cause goroutine leak.
  227. drain(s.source)
  228. }
  229. }
  230. // not enough items in s.source, but we need to let successive method to go ASAP.
  231. if n > 0 {
  232. close(source)
  233. }
  234. }()
  235. return Range(source)
  236. }
  237. // Last returns the last item, or nil if no items.
  238. func (s Stream) Last() (item interface{}) {
  239. for item = range s.source {
  240. }
  241. return
  242. }
  243. // Map converts each item to another corresponding item, which means it's a 1:1 model.
  244. func (s Stream) Map(fn MapFunc, opts ...Option) Stream {
  245. return s.Walk(func(item interface{}, pipe chan<- interface{}) {
  246. pipe <- fn(item)
  247. }, opts...)
  248. }
  249. // Merge merges all the items into a slice and generates a new stream.
  250. func (s Stream) Merge() Stream {
  251. var items []interface{}
  252. for item := range s.source {
  253. items = append(items, item)
  254. }
  255. source := make(chan interface{}, 1)
  256. source <- items
  257. close(source)
  258. return Range(source)
  259. }
  260. // NoneMatch returns whether all elements of this stream don't match the provided predicate.
  261. // May not evaluate the predicate on all elements if not necessary for determining the result.
  262. // If the stream is empty then true is returned and the predicate is not evaluated.
  263. func (s Stream) NoneMatch(predicate func(item interface{}) bool) bool {
  264. for item := range s.source {
  265. if predicate(item) {
  266. // make sure the former goroutine not block, and current func returns fast.
  267. go drain(s.source)
  268. return false
  269. }
  270. }
  271. return true
  272. }
  273. // Parallel applies the given ParallelFunc to each item concurrently with given number of workers.
  274. func (s Stream) Parallel(fn ParallelFunc, opts ...Option) {
  275. s.Walk(func(item interface{}, pipe chan<- interface{}) {
  276. fn(item)
  277. }, opts...).Done()
  278. }
  279. // Reduce is a utility method to let the caller deal with the underlying channel.
  280. func (s Stream) Reduce(fn ReduceFunc) (interface{}, error) {
  281. return fn(s.source)
  282. }
  283. // Reverse reverses the elements in the stream.
  284. func (s Stream) Reverse() Stream {
  285. var items []interface{}
  286. for item := range s.source {
  287. items = append(items, item)
  288. }
  289. // reverse, official method
  290. for i := len(items)/2 - 1; i >= 0; i-- {
  291. opp := len(items) - 1 - i
  292. items[i], items[opp] = items[opp], items[i]
  293. }
  294. return Just(items...)
  295. }
  296. // Skip returns a Stream that skips size elements.
  297. func (s Stream) Skip(n int64) Stream {
  298. if n < 0 {
  299. panic("n must not be negative")
  300. }
  301. if n == 0 {
  302. return s
  303. }
  304. source := make(chan interface{})
  305. go func() {
  306. for item := range s.source {
  307. n--
  308. if n >= 0 {
  309. continue
  310. } else {
  311. source <- item
  312. }
  313. }
  314. close(source)
  315. }()
  316. return Range(source)
  317. }
  318. // Sort sorts the items from the underlying source.
  319. func (s Stream) Sort(less LessFunc) Stream {
  320. var items []interface{}
  321. for item := range s.source {
  322. items = append(items, item)
  323. }
  324. sort.Slice(items, func(i, j int) bool {
  325. return less(items[i], items[j])
  326. })
  327. return Just(items...)
  328. }
  329. // Split splits the elements into chunk with size up to n,
  330. // might be less than n on tailing elements.
  331. func (s Stream) Split(n int) Stream {
  332. if n < 1 {
  333. panic("n should be greater than 0")
  334. }
  335. source := make(chan interface{})
  336. go func() {
  337. var chunk []interface{}
  338. for item := range s.source {
  339. chunk = append(chunk, item)
  340. if len(chunk) == n {
  341. source <- chunk
  342. chunk = nil
  343. }
  344. }
  345. if chunk != nil {
  346. source <- chunk
  347. }
  348. close(source)
  349. }()
  350. return Range(source)
  351. }
  352. // Tail returns the last n elements in p.
  353. func (s Stream) Tail(n int64) Stream {
  354. if n < 1 {
  355. panic("n should be greater than 0")
  356. }
  357. source := make(chan interface{})
  358. go func() {
  359. ring := collection.NewRing(int(n))
  360. for item := range s.source {
  361. ring.Add(item)
  362. }
  363. for _, item := range ring.Take() {
  364. source <- item
  365. }
  366. close(source)
  367. }()
  368. return Range(source)
  369. }
  370. // Walk lets the callers handle each item, the caller may write zero, one or more items base on the given item.
  371. func (s Stream) Walk(fn WalkFunc, opts ...Option) Stream {
  372. option := buildOptions(opts...)
  373. if option.unlimitedWorkers {
  374. return s.walkUnlimited(fn, option)
  375. }
  376. return s.walkLimited(fn, option)
  377. }
  378. func (s Stream) walkLimited(fn WalkFunc, option *rxOptions) Stream {
  379. pipe := make(chan interface{}, option.workers)
  380. go func() {
  381. var wg sync.WaitGroup
  382. pool := make(chan lang.PlaceholderType, option.workers)
  383. for item := range s.source {
  384. // important, used in another goroutine
  385. val := item
  386. pool <- lang.Placeholder
  387. wg.Add(1)
  388. // better to safely run caller defined method
  389. threading.GoSafe(func() {
  390. defer func() {
  391. wg.Done()
  392. <-pool
  393. }()
  394. fn(val, pipe)
  395. })
  396. }
  397. wg.Wait()
  398. close(pipe)
  399. }()
  400. return Range(pipe)
  401. }
  402. func (s Stream) walkUnlimited(fn WalkFunc, option *rxOptions) Stream {
  403. pipe := make(chan interface{}, option.workers)
  404. go func() {
  405. var wg sync.WaitGroup
  406. for item := range s.source {
  407. // important, used in another goroutine
  408. val := item
  409. wg.Add(1)
  410. // better to safely run caller defined method
  411. threading.GoSafe(func() {
  412. defer wg.Done()
  413. fn(val, pipe)
  414. })
  415. }
  416. wg.Wait()
  417. close(pipe)
  418. }()
  419. return Range(pipe)
  420. }
  421. // UnlimitedWorkers lets the caller use as many workers as the tasks.
  422. func UnlimitedWorkers() Option {
  423. return func(opts *rxOptions) {
  424. opts.unlimitedWorkers = true
  425. }
  426. }
  427. // WithWorkers lets the caller customize the concurrent workers.
  428. func WithWorkers(workers int) Option {
  429. return func(opts *rxOptions) {
  430. if workers < minWorkers {
  431. opts.workers = minWorkers
  432. } else {
  433. opts.workers = workers
  434. }
  435. }
  436. }
  437. // buildOptions returns a rxOptions with given customizations.
  438. func buildOptions(opts ...Option) *rxOptions {
  439. options := newOptions()
  440. for _, opt := range opts {
  441. opt(options)
  442. }
  443. return options
  444. }
  445. // drain drains the given channel.
  446. func drain(channel <-chan interface{}) {
  447. for range channel {
  448. }
  449. }
  450. // newOptions returns a default rxOptions.
  451. func newOptions() *rxOptions {
  452. return &rxOptions{
  453. workers: defaultWorkers,
  454. }
  455. }