项目作者: iboard

项目描述 :
Elixir Datasource and DataStage
高级语言: Elixir
项目地址: git://github.com/iboard/data_source.git
创建时间: 2018-09-02T19:10:55Z
项目社区:https://github.com/iboard/data_source

开源协议:MIT License

下载


DataSource

Documentation
Documentation

Part of the [PocketData][] project. A DataSource is a “Producer” (of data)
for the system. It can be a simple counter, or a complex “collector”, reading
sensors from embedded systems or collecting data from foreign (web-)services.

Installation

The package is available in Hex, the package can be installed
by adding data_source to your list of dependencies in mix.exs:

  1. def deps do
  2. [
  3. {:data_source, "~> 0.1"}
  4. ]
  5. end

Example

The best way to figure out how you can use this library is by having a look at
this Test suite.

  1. # Remember consumed events in state
  2. defmodule ConsumerSpy do
  3. use GenStage
  4. def start_link(), do: GenStage.start_link(ConsumerSpy, [])
  5. def init(state), do: {:consumer, state}
  6. def handle_events(events, _from, state) do
  7. # Simulate load
  8. Process.sleep(10)
  9. {:noreply, [], [events | state]}
  10. end
  11. def handle_call(:get, _from, state) do
  12. {:reply, Enum.flat_map(state, & &1), [], state}
  13. end
  14. end
  15. {:ok, datasource} = Datasource.start_link(0, fn state -> {state, state + 1} end)
  16. {:ok, producer} = Datasource.DataStage.start_link(datasource)
  17. {:ok, consumer} = ConsumerSpy.start_link()
  18. GenStage.sync_subscribe(consumer, to: producer, max_demand: 1)
  19. Process.sleep(100)
  20. ConsumerSpy.call(consumer, :get)
  21. # => [0,1,2,3,...10]

Because the consumer delays for 10ms and we have 1 consumer only,
in 100ms we can expect about 10 events. To process more than 10
events you can increase the number of consumers.

Datasources

Some Datasources are defined in lib/data_source. Such as
Datasource.Counter, Datasource.File, and more.