The Barrier post used condvar plus mutex under the hood; here the condvar is the main character.
上次提到的Barrier用到了Rust的condvar和mutex,今天来看下condvar的用法。
A lot of Rust’s thread safety is two auto traits: Send (OK to move to another thread) and Sync (OK to share as &T). The compiler checks them at thread::spawn and when references cross threads.
不知道你有没有好奇过,Rust是怎么控制并发安全的。为什么编译器在编译时就能发现一些并发安全的问题。
今天拿例子聊聊这背后Rust的两个并发约束trait:Sync和Send,看看它们是怎么控制并发安全的。
Interior mutability means a value looks immutable from the outside but can still be changed on the inside. It shows up when you hold an immutable reference and still need to update the data.
Cell and RefCell are Rust’s two main options. Cell targets Copy types; RefCell handles non-Copy types.
When do you actually reach for them, and why two types? Below are a few concrete situations.
内部可变性(interior mutability)是Rust用来表示在一个值的外部看起来是不可变的,但是在内部是可变的。这种模式通常用于在拥有不可变引用的同时修改目标数据。
Cell和RefCell是Rust提供的两种内部可变性的实现。Cell是用于Copy类型的,而RefCell是用于非Copy类型的。
不知道你有没有好奇过具体内部可变性应用在什么场景,为啥要分两种实现。
今天我们针对一些场景来聊聊这两个类型的应用。
Ownership is one of Rust’s most distinctive features — and one of the hardest for newcomers.
Ownership trips up most Rust beginners. The examples below show what it is, why the compiler cares, and how move, borrow, and lifetimes fit together. Skim once for the map; come back for the line-by-line bits.
Tip: The code has comments. You don’t need every detail on the first pass — follow the flow, then dig into specific snippets later.
所有权是 Rust 很有意思的一个语言特性,但对于初学者却是一个比较有挑战的内容。
今天尝试用代码示例来聊聊 Rust 的所有权是什么,以及为什么要有所有权。希望能给初学的朋友一点帮助。
Tips:文中代码有相应注释,建议可以先不用纠结细节,关注整体。后边可以再挨个去研究具体代码细节
I’ve been reading Software Engineering at Google, and one line stuck with me:
Code is a liability, not an asset.
If you’re a programmer, that probably sounds wrong. Your code feels like something you built — proof of skill. How could it be a liability?
最近在读《Google软件工程》,其中有一句话让我印象深刻:
代码是负债,而不是资产。
然而作为程序员,相信很多人都认为自己的代码就是资产,是自己智慧的结晶,怎么会是负债呢?
Most of us have at least heard of OAuth2.
New to the vocabulary? Skim Authentication vs authorization — OAuth2, OIDC, JWT, and Keycloak for the basics and flows.
With Google as the IdP, the usual story: user hits the consent screen, comes back with an authorization code, you exchange it for tokens, then call Google APIs on the user’s behalf.
Rolling that by hand isn’t huge — but add Facebook, Instagram, and every other IdP and you don’t want N custom integrations. A central auth layer is the point. Here we use Keycloak as that layer.
If you can’t reproduce it locally, it’s hard to learn or debate the design. This walkthrough uses Rust/Axum + Keycloak, with docker-compose, Terraform, and shell scripts so you can replay the whole stack on a Mac. Repo: https://github.com/NewbMiao/axum-koans
提到OAuth2,大家多少都有些了解。
不了解的话可以先看下之前的简单聊聊鉴权背后的那些技术先回顾一下基本概念和流程。
简单来说,以google授权为例,一般就是通过用户授权页面登录google账号,再跳转用code换取到相应权限的token,就可以代表用户去发起一些google api的请求。
直接代码实现这套授权逻辑并不复杂,不过如果还需要接入facebook授权,instagram授权呢,总不能挨个去实现一遍吧。
最好能有一套通用的解决方案来解放双手, 今天我们就聊聊如何用keycloak实现一套通用的身份验证和授权管理方案。
提前说明,无法本地复刻的技术方案不利于理解,也不利于方案探讨。虽然本文章所用代码是使用了
rust的axum框架(为啥?因为rustis future!)+keycloak,但从服务启动到keycloak服务及相关配置,都用docker-compose+terraform+shell脚本化管理,可100%本地复刻,欢迎本地尝试。(当然我说的是Mac下)代码地址: https://github.com/NewbMiao/axum-koans
I was working through Chen Tian’s Programming Rust course and building the sample code in Docker. Every run spent time refreshing the crates.io index—even when Cargo.toml had not changed.
There has to be a way to cache that. This is the path I took (including what did not work).
最近开始学习《陈天 · Rust 编程第一课》,在用docker镜像打包构建代码示例时,每次都会在更新crate.io索引上等上一会,即便在没有修改Cargo.tomal依赖的前提下。
想着难道没有办法缓存下更新么,于是就有了这篇新手踩坑指南。
I put together a small Keycloak auth demo so folks who’ve never touched Keycloak can see the moving parts without drowning in docs.
Goes with the Keycloak demo above: authentication vs authorization, OAuth2, OpenID Connect, JWT, JWKS—and where each shows up in that code.
最近弄了一个keycloak鉴权相关demo(地址附文末), 可以让不熟悉的keycloak的同学有一个快速、直观的了解。
今天结合背后的技术概念,简单聊聊:鉴权(authentication)和授权(authorization)以及Oauth2、OpenID Connect、JWT 和 JWKS。