-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkinesis.rs
More file actions
125 lines (115 loc) · 3.7 KB
/
kinesis.rs
File metadata and controls
125 lines (115 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::error::{Error, ErrorKind};
use futures::{Async, Future, Poll, Stream};
use rusoto_core::Region;
use rusoto_kinesis::{
GetRecordsInput, GetRecordsOutput, GetShardIteratorInput, Kinesis, KinesisClient,
ListShardsInput, Shard,
};
pub struct KinesisShardIterator {
client: KinesisClient,
input: GetShardIteratorInput,
token: Option<String>,
}
impl KinesisShardIterator {
pub fn get_shard_ids(name: &str, region: &Region) -> Result<Vec<Shard>, Error> {
let c = KinesisClient::new(region.clone());
c.list_shards(ListShardsInput {
stream_name: Some(name.to_string()),
..Default::default()
})
.sync()
.map(|xs| xs.shards.unwrap())
.map_err(Into::into)
}
fn new_self(input: GetShardIteratorInput, region: Region) -> Self {
let c = KinesisClient::new(region);
KinesisShardIterator {
client: c,
input,
token: None,
}
}
pub fn new(
stream_name: &str,
shard_id: &str,
shard_iterator_type: &str,
region: &Region,
) -> Self {
let input = GetShardIteratorInput {
shard_id: shard_id.to_string(),
shard_iterator_type: shard_iterator_type.to_string(),
stream_name: stream_name.to_string(),
..Default::default()
};
KinesisShardIterator::new_self(input, region.clone())
}
pub fn new_with_sequence_number(
stream_name: &str,
shard_id: &str,
shard_iterator_type: &str,
sequence_number: &str,
region: &Region,
) -> Self {
let input = GetShardIteratorInput {
shard_id: shard_id.to_string(),
shard_iterator_type: shard_iterator_type.to_string(),
stream_name: stream_name.to_string(),
starting_sequence_number: Some(sequence_number.to_string()),
..Default::default()
};
KinesisShardIterator::new_self(input, region.clone())
}
pub fn new_with_timestamp(
stream_name: &str,
shard_id: &str,
shard_iterator_type: &str,
timestamp: f64,
region: &Region,
) -> Self {
let input = GetShardIteratorInput {
shard_id: shard_id.to_string(),
shard_iterator_type: shard_iterator_type.to_string(),
stream_name: stream_name.to_string(),
timestamp: Some(timestamp),
..Default::default()
};
KinesisShardIterator::new_self(input, region.clone())
}
pub fn get_iterator_token(&self) -> Result<String, Error> {
self.client
.get_shard_iterator(self.input.clone())
.sync()
.map_err(Into::into)
.and_then(|x| {
x.shard_iterator
.map_or_else(|| Err(Error::from(ErrorKind::Rusoto)), Ok)
})
}
}
impl Stream for KinesisShardIterator {
type Item = GetRecordsOutput;
type Error = Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
if let Some(current) = &self.token {
let r = GetRecordsInput {
shard_iterator: current.clone(),
..Default::default()
};
self.client
.get_records(r)
.map(|r| {
self.token = r.next_shard_iterator.clone();
Async::Ready(Some(r))
})
.map_err(Into::into)
.wait()
} else {
self.get_iterator_token()
.map(|next| {
self.token = Some(next);
Async::NotReady
})
.map_err(Into::into)
}
}
}