Switch is a distributed real-time feature management platform that provides secure, efficient dynamic configuration capabilities for enterprise applications. Through advanced WebSocket persistent connection architecture and multi-driver communication mechanisms, it achieves millisecond-level configuration delivery, enabling development teams to precisely control feature releases, user experiences, and system behavior without restarting applications.
1. Enterprise-Grade Communication Architecture
- WebSocket Persistent Connection Framework - Based on
switch-components/pcfor persistent connection management - Multi-Driver Support - Three communication modes: Webhook, Kafka, and Long Polling
- Intelligent Network Discovery - Automatic adaptation to NAT environments, solving complex network scenarios
- Layered Confirmation Mechanism - Ensuring reliability and consistency of configuration delivery
2. Flexible Factor System
// Not just simple true/false, but intelligent decision-making based on complex rules
if _switch.IsOpen(ctx, "feature_enabled") {
// The system calculates in real-time based on configured multi-dimensional factors
// (e.g., user attributes, geographic location, time windows, etc.)
// whether to enable this feature
}3. Multi-Tenant Management System
- Tenant Isolation - Complete data and permission isolation
- Environment Management - Independent configurations for development, testing, and production environments with strict configuration promotion policies
- Approval Workflows - Multi-level approval mechanism for sensitive changes
The Switch ecosystem consists of the following core components:
- switch-admin: Backend service responsible for managing configurations and client communication
- switch-frontend: Web interface for configuration management
- switch-sdk-go: Go SDK for integrating switches into business applications to form clients
- switch-sdk-core: Core definitions and interfaces
- switch-components: Implementation of communication and core logic
- switch-client-demo: Example application demonstrations
switch-sdk-go is the Go language business integration layer of the Switch ecosystem, providing out-of-the-box feature flag capabilities for Go applications. It encapsulates complex communication protocols and data synchronization logic, enabling developers to easily implement intelligent feature control based on multi-dimensional factors through a concise API, supporting real-time configuration updates and multiple communication modes.
- Simple and Easy-to-Use API: Provides two switch checking methods,
IsOpen()andIsSwitchOpen(), to meet different scenario requirements - Real-Time Configuration Synchronization: WebSocket-based real-time configuration push with millisecond-level effectiveness, no application restart required
- Multi-Communication Mode Support:
- Kafka Mode: Message queue distribution, suitable for large-scale distributed scenarios
- Webhook Mode: HTTP callback push, flexibly adapting to various network environments
- Long Polling Mode: Active configuration pulling, compatible with restricted network environments
- Intelligent Caching Mechanism:
- Factor-level caching to avoid redundant calculations
- Singleflight pattern to prevent cache stampede
- Thread-safe rule storage
- Complete Context Support: Native support for Go Context, enabling request-level tracing and control
- High Availability Guarantee:
- Automatic reconnection mechanism
- Graceful degradation strategy
- Local cache fallback
- Enterprise-Grade Monitoring: Built-in statistical metrics and performance monitoring, supporting full-chain tracing of switch execution
- Flexible Middleware Architecture: Pluggable middleware system supporting custom extensions
go get gitee.com/fatzeng/switch-sdk-gopackage main
import (
"context"
"log"
switchsdk "gitee.com/fatzeng/switch-sdk-go"
_switch "gitee.com/fatzeng/switch-sdk-go/core/switch"
"gitee.com/fatzeng/switch-sdk-core/model"
)
func main() {
// Create context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Initialize Switch SDK
err := switchsdk.Start(ctx,
_switch.WithDomain("ws://localhost:8081"), // Switch Admin service address
_switch.WithNamespaceTag("your-namespace"), // Namespace identifier
_switch.WithEnvTag("production"), // Environment identifier
_switch.WithServiceName("your-service"), // Service name
_switch.WithVersion("1.0.0"), // Service version
)
if err != nil {
log.Fatalf("Failed to initialize Switch SDK: %v", err)
}
defer switchsdk.Shutdown()
// Method 1: Simple switch check (by switch name)
if _switch.IsOpen(ctx, "new-feature") {
// New feature is enabled
log.Println("Using new feature")
} else {
// New feature is disabled
log.Println("Using old feature")
}
// Method 2: Switch model-based check (supports more complex scenarios)
switchModel := &model.SwitchModel{
Name: "advanced-feature",
// Can set more attributes for factor calculation
}
if _switch.IsSwitchOpen(ctx, switchModel) {
log.Println("Advanced feature is enabled")
}
}import "gitee.com/fatzeng/switch-sdk-go/core/cache"
// Enable factor caching to avoid redundant calculations
ctx = cache.UseCache(ctx)
// Subsequent switch checks will automatically use cache
if _switch.IsOpen(ctx, "cached-feature") {
// When cache hits, return result directly without recalculation
}err := switchsdk.Start(ctx,
// Basic configuration
_switch.WithDomain("ws://switch-admin.example.com"),
_switch.WithNamespaceTag("production-ns"),
_switch.WithEnvTag("prod"),
_switch.WithServiceName("order-service"),
_switch.WithVersion("2.1.0"),
)switch-sdk-go/
βββ start.go # SDK main entry, providing initialization and lifecycle management
βββ core/ # Core functional modules
β βββ switch/ # Switch core engine
β βββ cache/ # Intelligent caching system
β βββ filter/ # Filter system
β βββ factor/ # Factor processing module
β βββ factor_statistics/ # Statistics and monitoring
β βββ middleware/ # Middleware framework
βββ internal/ # Internal implementation (not exposed)
β βββ datasync/ # Data synchronization mechanism
βββ go.mod # Go module dependencies
start.go: Unified SDK entry point, responsible for initialization, configuration management, and lifecycle controlcore/switch/: Core engine for switch evaluation, implementing intelligent decision-making based on rules and factorscore/cache/: High-performance caching system using singleflight pattern to prevent cache stampedecore/filter/: Flow control for switch execution, supporting custom filtering logiccore/factor/: Factor processing and calculation logiccore/factor_statistics/: Statistics and performance monitoring for switch executioncore/middleware/: Extensible middleware architecture supporting chain processinginternal/datasync/: Data synchronization core, handling real-time configuration updates and persistence
switch-sdk-go (Business Integration Layer)
β depends on
switch-sdk-core (Core Definition Layer)
- Provides data models (SwitchModel, RuleNode)
- Defines unified interfaces and protocols
β depends on
switch-components (Communication Component Layer)
- Provides WebSocket client
- Implements multiple drivers (Kafka, Webhook, Polling)
- Provides network communication infrastructure
Switch Evaluation Flow:
Business Application β switch-sdk-go.IsOpen() β Rule Engine β Factor Calculation β Cache β Return Result
A switch is the basic unit of feature control. Each switch contains:
- Name: Unique identifier
- Rules: Decision logic (AND/OR combinations)
- Factors: Multi-dimensional judgment conditions (user attributes, geographic location, time, etc.)
Factors are the judgment dimensions for switch decisions, supporting:
- User attribute factors (user ID, user group, VIP level, etc.)
- Geographic location factors (country, city, IP range, etc.)
- Time factors (time windows, date ranges, etc.)
- Custom factors (business-specific judgment logic)
Rules define how factors are combined:
- AND Rule: Switch opens only when all factors are satisfied
- OR Rule: Switch opens when any factor is satisfied
- Nested Rules: Support for complex logical combinations
We welcome and appreciate all forms of contributions! Whether it's reporting issues, suggesting features, improving documentation, or submitting code, your participation will help make Switch better.
- Fork this repository and create your feature branch
- Write code and ensure it follows the project's coding standards
- Add tests to cover your changes
- Submit a Pull Request with a detailed description of your changes and motivation
- π Bug Fixes: Discover and fix issues
- β¨ New Features: Propose and implement new capabilities
- π Documentation Improvements: Enhance documentation and examples
- π¨ Code Optimization: Improve code quality and performance
- π§ͺ Test Enhancement: Increase test coverage
For more details, please refer to our contribution guidelines documentation.
This project is licensed under the MIT License.