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 210 211 212 213 214 215 216 217 218 219 220 221
//! This module contains various simplifications aimed to ease working with services: builders and //! façades. use std::borrow::Cow; use std::fmt::{self, Debug, Formatter}; use std::iter::IntoIterator; use std::net::SocketAddr; use std::sync::{Arc, Mutex}; use tokio_core::reactor::Handle; use {Resolve, Service}; use super::{FixedResolver, Resolver, Supervisor}; pub mod app; pub mod locator; pub mod storage; pub mod tvm; pub mod unicorn; pub use self::app::App; pub use self::locator::Locator; pub use self::storage::Storage; pub use self::tvm::Tvm; pub use self::unicorn::Unicorn; const LOCATOR_NAME: &str = "locator"; /// Resolve configuration. pub trait ResolveBuilder { /// Resolver type. type Item: Resolve; /// Consumes this builder, yielding a new resolver. fn build(self, handle: &Handle) -> Self::Item; } /// A resolve builder, that builds Locator resolver. #[derive(Debug)] pub struct ResolverBuilder { name: Cow<'static, str>, resolver: FixedResolver, } impl ResolveBuilder for ResolverBuilder { type Item = Resolver; fn build(self, handle: &Handle) -> Self::Item { let shared = Arc::new(Mutex::new(Default::default())); let locator = Service { name: self.name.clone(), shared: shared.clone(), tx: Supervisor::spawn(self.name, shared, self.resolver, handle), }; Resolver::new(Locator::new(locator)) } } /// A resolver builder that returns already preconfigured `Resolve`. #[derive(Debug)] pub struct PreparedResolver<R> { resolver: R, } impl<R: Resolve> ResolveBuilder for PreparedResolver<R> { type Item = R; fn build(self, _handle: &Handle) -> Self::Item { self.resolver } } /// Service configuration. Provides detailed control over the properties and behavior of new /// services. /// /// Uses `Locator` as a service name resolver by default. /// /// # Examples /// /// ``` /// use cocaine::{Core, ServiceBuilder}; /// /// let core = Core::new().unwrap(); /// /// let service = ServiceBuilder::new("storage") /// .build(&core.handle()); /// /// assert_eq!("storage", service.name()); /// ``` pub struct ServiceBuilder<T> { name: Cow<'static, str>, resolve_builder: T, } impl ServiceBuilder<ResolverBuilder> { /// Constructs a new service builder, which will build a `Service` with the given name. pub fn new<N: Into<Cow<'static, str>>>(name: N) -> Self { let resolver = ResolverBuilder { name: LOCATOR_NAME.into(), resolver: FixedResolver::default(), }; Self { name: name.into(), resolve_builder: resolver, } } /// Sets the `Locator` resolver endpoints. /// /// By default `[::]:10053` address is used to resolve the `Locator` service and it can be /// changed using this method. If multiple endpoints are specified the resolver will try to /// connect to each of them in a loop, breaking after the connection establishment. /// /// # Note /// /// These endpoints are used only for `Locator` resolving. The `Service` resolution is done /// through the `Locator` unless `FixedResolver` specified using [`resolver`][resolver] method. /// /// # Examples /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; /// /// use cocaine::{Core, ServiceBuilder}; /// /// let core = Core::new().unwrap(); /// /// let service = ServiceBuilder::new("storage") /// .locator_addrs(vec![SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 10071)]) /// .build(&core.handle()); /// ``` /// /// [resolver]: #method.resolver pub fn locator_addrs<E>(mut self, addrs: E) -> Self where E: IntoIterator<Item = SocketAddr>, { self.resolve_builder.resolver = FixedResolver::new(addrs.into_iter().collect()); self } /// Sets the memory limit in bytes for internal buffers. /// /// Normally cocaine-runtime must read all incoming events as fast as possible no matter what. /// However, especially for logging service, sometimes the client can overflow the TCP window, /// which leads to readable stream overload. As a result - we start to buffering incoming /// events more and more, and it may potentially lead to OOM killer coming. /// /// By specifying this option we set the internal memory limit so every new either invocation /// or push event, that will overflow the specified limit, will be rejected /// with `MemoryOverflow` error code and this is guaranteed that those bytes won't be written /// into the socket until retried. pub fn memory_limit(self, _nbytes: usize) -> Self { // TODO: To allow this we must return a future from `Sender::send`. unimplemented!(); } // TODO: Receiver memory_limit. // TODO: Resolve timeout. } impl<T> ServiceBuilder<T> { /// Sets the resolver, that is used for name resolution. /// /// By default name resolution via the `Locator` is used, but sometimes more detailed control /// is required. /// /// # Examples /// /// This example demonstrates how to build a `Service`, which will always try to connect to a /// fixed endpoint at `127.0.0.1:32768`. /// /// ``` /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; /// /// use cocaine::{ServiceBuilder, Core, FixedResolver}; /// /// let core = Core::new().unwrap(); /// let endpoints = vec![SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 32768)]; /// /// let service = ServiceBuilder::new("storage") /// .resolver(FixedResolver::new(endpoints)) /// .build(&core.handle()); /// ``` pub fn resolver<R: Resolve>(self, resolver: R) -> ServiceBuilder<PreparedResolver<R>> { ServiceBuilder { name: self.name, resolve_builder: PreparedResolver { resolver: resolver }, } } } impl<T: ResolveBuilder + 'static> ServiceBuilder<T> { /// Consumes this `ServiceBuilder` yielding a `Service`. /// /// This will not perform a connection attempt until required - both name resolution and /// connection will be performed on demand. You can call [`Service::connect()`][connect] method /// for fine-grained control. /// /// [connect]: ../struct.Service.html#method.connect pub fn build(self, handle: &Handle) -> Service { let shared = Arc::new(Mutex::new(Default::default())); Service { name: self.name.clone(), shared: shared.clone(), tx: Supervisor::spawn(self.name, shared, self.resolve_builder.build(handle), handle), } } } impl<T> Debug for ServiceBuilder<T> { fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> { fmt.debug_struct("ServiceBuilder") .field("name", &self.name) .finish() } }