โ† Back to list
rustfs

m12-lifecycle

by rustfs

๐Ÿš€2.3x faster than MinIO for 4KB object payloads. RustFS is an open-source, S3-compatible high-performance object storage system supporting migration and coexistence with other S3-compatible platforms such as MinIO and Ceph.

โญ 20,125๐Ÿด 866๐Ÿ“… Jan 23, 2026

SKILL.md


name: m12-lifecycle description: "Use when designing resource lifecycles. Keywords: RAII, Drop, resource lifecycle, connection pool, lazy initialization, connection pool design, resource cleanup patterns, cleanup, scope, OnceCell, Lazy, once_cell, OnceLock, transaction, session management, when is Drop called, cleanup on error, guard pattern, scope guard, ่ต„ๆบ็”Ÿๅ‘ฝๅ‘จๆœŸ, ่ฟžๆŽฅๆฑ , ๆƒฐๆ€งๅˆๅง‹ๅŒ–, ่ต„ๆบๆธ…็†, RAII ๆจกๅผ"

Resource Lifecycle

Layer 2: Design Choices

Core Question

When should this resource be created, used, and cleaned up?

Before implementing lifecycle:

  • What's the resource's scope?
  • Who owns the cleanup responsibility?
  • What happens on error?

Lifecycle Pattern โ†’ Implementation

PatternWhenImplementation
RAIIAuto cleanupDrop trait
Lazy initDeferred creationOnceLock, LazyLock
PoolReuse expensive resourcesr2d2, deadpool
GuardScoped accessMutexGuard pattern
ScopeTransaction boundaryCustom struct + Drop

Thinking Prompt

Before designing lifecycle:

  1. What's the resource cost?

    • Cheap โ†’ create per use
    • Expensive โ†’ pool or cache
    • Global โ†’ lazy singleton
  2. What's the scope?

    • Function-local โ†’ stack allocation
    • Request-scoped โ†’ passed or extracted
    • Application-wide โ†’ static or Arc
  3. What about errors?

    • Cleanup must happen โ†’ Drop
    • Cleanup is optional โ†’ explicit close
    • Cleanup can fail โ†’ Result from close

Trace Up โ†‘

To domain constraints (Layer 3):

"How should I manage database connections?"
    โ†‘ Ask: What's the connection cost?
    โ†‘ Check: domain-* (latency requirements)
    โ†‘ Check: Infrastructure (connection limits)
QuestionTrace ToAsk
Connection poolingdomain-*What's acceptable latency?
Resource limitsdomain-*What are infra constraints?
Transaction scopedomain-*What must be atomic?

Trace Down โ†“

To implementation (Layer 1):

"Need automatic cleanup"
    โ†“ m02-resource: Implement Drop
    โ†“ m01-ownership: Clear owner for cleanup

"Need lazy initialization"
    โ†“ m03-mutability: OnceLock for thread-safe
    โ†“ m07-concurrency: LazyLock for sync

"Need connection pool"
    โ†“ m07-concurrency: Thread-safe pool
    โ†“ m02-resource: Arc for sharing

Quick Reference

PatternTypeUse Case
RAIIDrop traitAuto cleanup on scope exit
Lazy InitOnceLock, LazyLockDeferred initialization
Poolr2d2, deadpoolConnection reuse
GuardMutexGuardScoped lock release
ScopeCustom structTransaction boundaries

Lifecycle Events

EventRust Mechanism
Creationnew(), Default
Lazy InitOnceLock::get_or_init
Usage&self, &mut self
CleanupDrop::drop()

Pattern Templates

RAII Guard

struct FileGuard {
    path: PathBuf,
    _handle: File,
}

impl Drop for FileGuard {
    fn drop(&mut self) {
        // Cleanup: remove temp file
        let _ = std::fs::remove_file(&self.path);
    }
}

Lazy Singleton

use std::sync::OnceLock;

static CONFIG: OnceLock<Config> = OnceLock::new();

fn get_config() -> &'static Config {
    CONFIG.get_or_init(|| {
        Config::load().expect("config required")
    })
}

Common Errors

ErrorCauseFix
Resource leakForgot DropImplement Drop or RAII wrapper
Double freeManual memoryLet Rust handle
Use after dropDangling referenceCheck lifetimes
E0509 move out of DropMoving owned fieldOption::take()
Pool exhaustionNot returnedEnsure Drop returns

Anti-Patterns

Anti-PatternWhy BadBetter
Manual cleanupEasy to forgetRAII/Drop
lazy_static!External depstd::sync::OnceLock
Global mutable stateThread unsafetyOnceLock or proper sync
Forget to closeResource leakDrop impl

WhenSee
Smart pointersm02-resource
Thread-safe initm07-concurrency
Domain scopesm09-domain
Error in cleanupm06-error-handling

Score

Total Score

90/100

Based on repository quality metrics

โœ“SKILL.md

SKILL.mdใƒ•ใ‚กใ‚คใƒซใŒๅซใพใ‚Œใฆใ„ใ‚‹

+20
โœ“LICENSE

ใƒฉใ‚คใ‚ปใƒณใ‚นใŒ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹

+10
โœ“่ชฌๆ˜Žๆ–‡

100ๆ–‡ๅญ—ไปฅไธŠใฎ่ชฌๆ˜ŽใŒใ‚ใ‚‹

+10
โœ“ไบบๆฐ—

GitHub Stars 1000ไปฅไธŠ

+15
โœ“ๆœ€่ฟ‘ใฎๆดปๅ‹•

1ใƒถๆœˆไปฅๅ†…ใซๆ›ดๆ–ฐ

+10
โœ“ใƒ•ใ‚ฉใƒผใ‚ฏ

10ๅ›žไปฅไธŠใƒ•ใ‚ฉใƒผใ‚ฏใ•ใ‚Œใฆใ„ใ‚‹

+5
โ—‹Issue็ฎก็†

ใ‚ชใƒผใƒ—ใƒณIssueใŒ50ๆœชๆบ€

0/5
โœ“่จ€่ชž

ใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใŒ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹

+5
โœ“ใ‚ฟใ‚ฐ

1ใคไปฅไธŠใฎใ‚ฟใ‚ฐใŒ่จญๅฎšใ•ใ‚Œใฆใ„ใ‚‹

+5

Reviews

๐Ÿ’ฌ

Reviews coming soon