TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Writing a Microservice in Rust

265 pointsby blacksmytheabout 7 years ago

11 comments

zbentleyabout 7 years ago
Minor nitpick:<p>&gt; Immutability (const) by default<p>The author clearly knows this, but others may not, so: immutability and &quot;const&quot; aren&#x27;t the same thing. Const typically prevents name rebinding (as the type system permits). Immutability is the presumption that the whole data structure can&#x27;t be mutated by default (though there are usually ways to mutate cheaply--without bulk copying, that is--provided by most immutable data APIs).<p>Const works like this:<p><pre><code> not_const int[] foo = [123, 456] const int[] bar = [456,789] foo = [1,2,3] &#x2F;&#x2F; no problem; rebinding is allowed if it meets the &#x27;int&#x27; type constraint. bar = [1,2,3] &#x2F;&#x2F; compile error! </code></pre> Immutability, <i>as typically defined</i> (there&#x27;s a bit of a semantic debate here sometimes) prevents changes to the contents of complex data structures:<p><pre><code> mut int[] foo = [123, 456] not_mut int[] bar = [456,789] foo[0] = 789 &#x2F;&#x2F; no problem; mutation is allowed so long as it&#x27;s with valid types. bar[0] = 123 &#x2F;&#x2F; compile error! </code></pre> Edit: missed a space.
评论 #16546989 未加载
评论 #16548470 未加载
评论 #16549501 未加载
评论 #16548664 未加载
评论 #16549902 未加载
Animatsabout 7 years ago
<i>With microservice, I mean an application that speaks HTTP, accepts requests, speaks to a database, returns a response (possibly serving HTML), packaged up in a Docker container and ready to be dropped somewhere in the cloud.</i><p>Huh? That&#x27;s a CRUD app. A &quot;microservice&quot; is a service which is a component of a larger service. Usually a service run by the same organization. Many microservices don&#x27;t speak HTTP; they use Google protocol buffers or something similarly efficient. Serving a Facebook page involves about a hundred microservices, and they don&#x27;t talk HTTP to each other.
评论 #16550546 未加载
评论 #16550459 未加载
评论 #16550869 未加载
allan_sabout 7 years ago
I&#x27;m waiting for the service to be a bit more consistent to blog about it, but we&#x27;ve started with a friend to write a webservice in rocket and it&#x27;s been a total pleasure<p>It may be useful to some people, as we&#x27;ve most of the feature you find in a &quot;real life&quot; service<p><pre><code> * database connection * schema creation * json handling * pagination * some endpoints are non-json * support for CORS * automated isolation test (i.e we test the service as a blackbox) * generation of a minimal bin-only docker image that with travis-ci * self-contained dev environment (vagrant up and you&#x27;re good to go) </code></pre> <a href="https:&#x2F;&#x2F;github.com&#x2F;allan-simon&#x2F;sentence-aligner-rust" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;allan-simon&#x2F;sentence-aligner-rust</a>
评论 #16549005 未加载
grumpydbaabout 7 years ago
Strikes me as having quite a big cognitive overhead. I wouldn&#x27;t try using this in unless being cornered by performance considerations or wanting a pure rust software architecture.
评论 #16547534 未加载
评论 #16544628 未加载
ilurkedhereabout 7 years ago
That does seem a bit painful to me. Maybe it will get better with some Rust macro DSL though...
评论 #16547190 未加载
评论 #16545260 未加载
评论 #16548032 未加载
MrBuddyCasinoabout 7 years ago
I made a very simple web service with Rocket (see <a href="https:&#x2F;&#x2F;github.com&#x2F;MrBuddyCasino&#x2F;alexa-auth" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;MrBuddyCasino&#x2F;alexa-auth</a>), which is better but still - I‘d go for Kotlin or Go instead if performance requirements allow it.
评论 #16552157 未加载
评论 #16547062 未加载
Lercabout 7 years ago
I haver been looking at Rust as a replacement for a c microserver I made,<p>I have a thing that listens as root and serves as a user dictated by their id.<p>so it&#x27;s<p><pre><code> as root: on connect fork: forked process: read request to determine ID drop privileges to UID and GID of ID process request </code></pre> Because of the root component I want it to do as little as possible while being root, which c is good for. but it&#x27;s also probably riddled with holes, only some of which I am aware.
评论 #16548619 未加载
Promargedabout 7 years ago
Shouldn&#x27;t RUST_LOG use &quot;microservice_rs&quot; instead of &quot;microservice&quot;? the &quot;_rs&quot; is a crate name.<p>Another thing, in a make_post_response stub function I think it&#x27;s better to use StatusCode::NotImplemented instead of NotFound (when trying this code I thought the match did not work correctly as everything returned NotFound).
hinkleyabout 7 years ago
The amount of ad network traffic rolling off of this page is staggering.<p>Made more amazing by the fact that only one ad shows on the entire page.
Shootheabout 7 years ago
Excellent guide, something that I&#x27;ll need in near future.<p>I wonder why the author chose hyper instead of iron though...
评论 #16544306 未加载
评论 #16543914 未加载
评论 #16547805 未加载
评论 #16548155 未加载
always_goodabout 7 years ago
Diesel is a synchronous library, so the post&#x27;s database functions make a blocking call and then return a future which, of course, doesn&#x27;t make them asynchronous. They&#x27;d still block the event loop.<p>You could fix this by passing a `&amp;futures_cpupool::CpuPool` into your database functions and wrapping the post&#x27;s function bodies in `pool.spawn_fn(|| { ... })` so that they execute on a different thread and return a future of their result.<p>To have a global IO pool that you pass into fns, you can store it in the Service struct:<p><pre><code> struct Microservice { io_pool: CpuPool } </code></pre> Now you can access it in your service&#x27;s handler (the call() fn) with `self.io_pool`.
评论 #16548141 未加载
评论 #16551483 未加载