定制 pablojoan/feature 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pablojoan/feature

Composer 安装命令:

composer require pablojoan/feature

包简介

Feature flagging API used for operational rampups and A/B testing

README 文档

README

GitHub license

Requires PHP 8.3 and above.

Installation

composer require pablojoan/feature

Basic Usage

use PabloJoan\Feature\Features; // Import the namespace.

$featureConfigs = [
    'foo' => [
        'variants' => [
            'variant1' => 100, //100% chance this variable will be chosen
            'variant2' => 0    //0% chance this variable will be chosen
        ]
    ],
    'bar' => [
        'variants' => [
            'variant1' => 25, //25% chance this variable will be chosen
            'variant2' => 25, //25% chance this variable will be chosen
            'variant3' => 50 //50% chance this variable will be chosen
        ],
        'bucketing' => 'id' //same id string will always return the same variant
    ]
];

$features = new Features($featureConfigs);

$features->isEnabled(featureName: 'foo');         // true
$features->getEnabledVariant(featureName: 'foo'); // 'variant1'

For a quick summary and common use cases, please read the rest of this README.

Feature API

Feature flagging API used for operational rampups and A/B testing.

The Feature API is how we selectively enable and disable features at a very fine grain as well as enabling features for a percentage of users for operational ramp-ups and for A/B tests. A feature can be completely enabled, completely disabled, or something in between and can comprise a number of related variants.

The two main API entry points are:

    $features->isEnabled(featureName: 'my_feature')

which returns true when my_feature is enabled and, for multi-variant features:

    $features->getEnabledVariant(featureName: 'my_feature')

which returns the name of the particular variant which should be used.

The single argument to each of these methods is the name of the feature to test.

A typical use of $features->isEnabled for a single-variant feature would look something like this:

    if ($features->isEnabled(featureName: 'my_feature')) {
        // do stuff
    }

For a multi-variant feature, we can determine the appropriate code to run for each variant with something like this:

    switch ($features->getEnabledVariant(featureName: 'my_feature')) {
      case 'foo':
          // do stuff appropriate for the 'foo' variant
          break;
      case 'bar':
          // do stuff appropriate for the 'bar' variant
          break;
    }

If a feature is bucketed by id, then we pass the id string to $features->isEnabled and $features->getEnabledVariant as a second parameter

    $isMyFeatureEnabled = $features->isEnabled(
        featureName: 'my_feature',
        id: 'unique_id_string'
    );

    $variant = $features->getEnabledVariant(
        featureName: 'my_feature',
        id: 'unique_id_string'
    );

Configuration cookbook

There are a number of common configurations so before I explain the complete syntax of the feature configuration stanzas, here are some of the more common cases along with the most concise way to write the configuration.

A totally enabled feature:

    $server_config['foo'] = ['variants' => ['enabled' => 100]];

A totally disabled feature:

    $server_config['foo'] = ['variants' => ['enabled' => 0]];

Feature with winning variant turned on for everyone

    $server_config['foo'] = ['variants' => ['blue_background' => 100]];

Single-variant feature ramped up to 1% of users.

    $server_config['foo'] = ['variants' => ['enabled' => 1]];

Multi-variant feature ramped up to 1% of users for each variant.

    $server_config['foo'] = [
       'variants' => [
           'blue_background'   => 1,
           'orange_background' => 1,
           'pink_background'   => 1,
       ],
    ];

Enabled for 10% of regular users.

    $server_config['foo'] = [
       'variants' => ['enabled' => 10]
    ];

Feature ramped up to 1% of requests, bucketing at random rather than by id

    $server_config['foo'] = [
       'variants' => ['enabled' => 1],
       'bucketing' => 'random'
    ];

Feature ramped up to 40% of requests, bucketing by id rather than at random

    $server_config['foo'] = [
       'variants' => ['enabled' => 40],
       'bucketing' => 'id'
    ];

Single-variant feature in 50/50 A/B test

    $server_config['foo'] = ['variants' => ['enabled' => 50]];

Multi-variant feature in A/B test with 20% of users seeing each variant (and 40% left in control group).

    $server_config['foo'] = [
       'variants' => [
           'blue_background'   => 20,
           'orange_background' => 20,
           'pink_background'   => 20
       ],
    ];

Configuration details

Each feature’s config stanza controls when the feature is enabled and what variant should be used when it is.

The value of a feature config stanza is an array with a number of special keys, the most important of which is 'variants'.

The value of the 'variants' property an array whose keys are names of variants and whose values are the percentage of requests that should see each variant.

The remaining feature config property is 'bucketing'. Bucketing specifies how users are bucketed when a feature is enabled for only a percentage of users. The default value, 'random', causes each request to be bucketed independently, meaning that the same user will be in different buckets on different requests. This is typically used for features that should have no user-visible effects but where we want to ramp up something like the switch from master to shards or a new version of JS code.

The bucketing value 'id', causes bucketing to be based on the given id.

Errors

There are a few ways to misuse the Feature API or misconfigure a feature that may be detected. (Some of these are not currently detected but may be in the future.)

  1. Setting the percentage value of a variant in 'variants' to a value less than 0 or greater than 100.

  2. Setting 'variants' such that the sum of the variant percentages is greater than 100.

  3. Setting 'variants' to a non-array value.

  4. Setting 'bucketing' to any value that is not 'id' or 'random'.

pablojoan/feature 适用场景与选型建议

pablojoan/feature 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 27, 最近一次更新时间为 2017 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「feature」 「toggle」 「ab-test」 「feature-flags」 「feature-toggles」 「feature-toggle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pablojoan/feature 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pablojoan/feature 我们能提供哪些服务?
定制开发 / 二次开发

基于 pablojoan/feature 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 1
  • Forks: 65
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2017-12-28