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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Publish events to Nakadi
use std::sync::Arc;
use std::time::Duration;
use std::io::Read;

use serde::Serialize;
use serde_json;
use reqwest::{Client as HttpClient, Response};
use reqwest::StatusCode;
use reqwest::header::{Authorization, Bearer};
use backoff::{Error as BackoffError, ExponentialBackoff, Operation};

use auth::{AccessToken, ProvidesAccessToken};
use nakadi::model::FlowId;

header! { (XFlowId, "X-Flow-Id") => [String] }

/// Publishes events to `Nakadi`
///
/// The publisher is just a convinience struct
/// and is not used for consuming a `Nakadi` stream.
/// It is simply a helper for publishing to a `Nakadi`
/// stream
pub struct NakadiPublisher {
    nakadi_base_url: String,
    http_client: HttpClient,
    token_provider: Arc<ProvidesAccessToken>,
}

impl NakadiPublisher {
    /// Create a new `NakadiPublisher`
    pub fn new<U: Into<String>, T: ProvidesAccessToken + 'static>(
        nakadi_base_url: U,
        token_provider: T,
    ) -> NakadiPublisher {
        NakadiPublisher {
            nakadi_base_url: nakadi_base_url.into(),
            http_client: HttpClient::new(),
            token_provider: Arc::new(token_provider),
        }
    }

    /// Create a new `NakadiPublisher`
    pub fn with_shared_access_token_provider<U: Into<String>>(
        nakadi_base_url: U,
        token_provider: Arc<ProvidesAccessToken>,
    ) -> NakadiPublisher {
        NakadiPublisher {
            nakadi_base_url: nakadi_base_url.into(),
            http_client: HttpClient::new(),
            token_provider: token_provider,
        }
    }

    /// Publish events packed into a vector of bytes.
    ///
    /// The events must be encoded in a way that `Nakadi`
    /// can understand.
    pub fn publish_raw(
        &self,
        event_type: &str,
        bytes: Vec<u8>,
        flow_id: Option<FlowId>,
        budget: Duration,
    ) -> Result<PublishStatus, PublishError> {
        let url = format!("{}/event-types/{}/events", self.nakadi_base_url, event_type);

        let flow_id = flow_id.unwrap_or_else(|| FlowId::default());

        let mut op = || match publish_events(
            &self.http_client,
            &url,
            &*self.token_provider,
            bytes.clone(),
            &flow_id,
        ) {
            Ok(publish_status) => Ok(publish_status),
            Err(err) => {
                if err.is_retry_suggested() {
                    Err(BackoffError::Transient(err))
                } else {
                    Err(BackoffError::Permanent(err))
                }
            }
        };

        let notify = |err, dur| {
            warn!("Publish error happened {:?}: {}", dur, err);
        };

        let mut backoff = ExponentialBackoff::default();
        backoff.max_elapsed_time = Some(budget);
        backoff.initial_interval = Duration::from_millis(50);
        backoff.multiplier = 1.5;

        match op.retry_notify(&mut backoff, notify) {
            Ok(publish_status) => Ok(publish_status),
            Err(BackoffError::Transient(err)) => Err(err),
            Err(BackoffError::Permanent(err)) => Err(err),
        }
    }

    /// Publish the given events to `Nakadi`
    pub fn publish_events<T: Serialize>(
        &self,
        event_type: &str,
        events: &[T],
        flow_id: Option<FlowId>,
        budget: Duration,
    ) -> Result<PublishStatus, PublishError> {
        let bytes = match serde_json::to_vec(events) {
            Ok(bytes) => bytes,
            Err(err) => return Err(PublishError::Serialization(err.to_string())),
        };
        self.publish_raw(event_type, bytes, flow_id, budget)
    }
}

fn publish_events(
    client: &HttpClient,
    url: &str,
    token_provider: &ProvidesAccessToken,
    bytes: Vec<u8>,
    flow_id: &FlowId,
) -> Result<PublishStatus, PublishError> {
    let mut request_builder = client.post(url);

    match token_provider.get_token() {
        Ok(Some(AccessToken(token))) => {
            request_builder.header(Authorization(Bearer { token }));
        }
        Ok(None) => (),
        Err(err) => return Err(PublishError::Token(err.to_string())),
    };

    request_builder.header(XFlowId(flow_id.0.clone()));

    match request_builder.body(bytes).send() {
        Ok(ref mut response) => match response.status() {
            StatusCode::Ok => Ok(PublishStatus::AllEventsPublished),
            StatusCode::MultiStatus => Ok(PublishStatus::NotAllEventsPublished),
            StatusCode::Unauthorized => {
                let msg = read_response_body(response);
                Err(PublishError::Unauthorized(msg, flow_id.clone()))
            }
            StatusCode::Forbidden => {
                let msg = read_response_body(response);
                Err(PublishError::Forbidden(msg, flow_id.clone()))
            }
            StatusCode::UnprocessableEntity => {
                let msg = read_response_body(response);
                Err(PublishError::UnprocessableEntity(msg, flow_id.clone()))
            }
            _ => {
                let msg = read_response_body(response);
                Err(PublishError::Other(msg, flow_id.clone()))
            }
        },
        Err(err) => Err(PublishError::Other(format!("{}", err), flow_id.clone())),
    }
}

fn read_response_body(response: &mut Response) -> String {
    let mut buf = String::new();
    response
        .read_to_string(&mut buf)
        .map(|_| buf)
        .unwrap_or("<Could not read body.>".to_string())
}

/// A status for (almos) successful publishing
#[derive(Debug)]
pub enum PublishStatus {
    /// All events were written send and accepted by `Nakadi`
    AllEventsPublished,
    /// Not all events were accepted by `Nakadi`
    NotAllEventsPublished,
}

/// Errors that can happen when publishing to `Nakadi`.
#[derive(Fail, Debug)]
pub enum PublishError {
    #[fail(display = "Unauthorized(FlowId: {}): {}", _1, _0)]
    Unauthorized(String, FlowId),
    /// Already exists
    #[fail(display = "Forbidden(FlowId: {}): {}", _1, _0)]
    Forbidden(String, FlowId),
    #[fail(display = "Unprocessable Entity(FlowId: {}): {}", _1, _0)]
    UnprocessableEntity(String, FlowId),
    #[fail(display = "Could not serialize events: {}", _0)]
    Serialization(String),
    #[fail(display = "An error occured: {}", _0)]
    Token(String),
    #[fail(display = "An error occured(FlowId: {}): {}", _1, _0)]
    Other(String, FlowId),
}

impl PublishError {
    pub fn is_retry_suggested(&self) -> bool {
        match *self {
            PublishError::Unauthorized(_, _) => true,
            PublishError::Forbidden(_, _) => false,
            PublishError::UnprocessableEntity(_, _) => false,
            PublishError::Serialization(_) => false,
            PublishError::Token(_) => true,
            PublishError::Other(_, _) => true,
        }
    }
}