定制 digidip/url-rewriter 二次开发

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

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

digidip/url-rewriter

Composer 安装命令:

composer require digidip/url-rewriter

包简介

digidip URL Rewriter with a circuit breaker.

README 文档

README

This library was created to allow digidip customers to rewrite their affiliate URLs into digidip affiliate URLs for tracking purchases etc. The library takes advantage of a circuit breaker methodology to help reduce the number of users being redirected via digidip's servers when a server or network issue exists. When a server outage is detected, the circuit will become open and all rewritten URLs will be returned as the original URL provided to the library.

If the circuit is open, it means that the transaction can not be tracked by digidip, although a majority of users will experience a positive redirection to the merchant's page instead of experiencing a error page.

Installation

You may install UrlRewriter via Composer:

composer require digidip/url-rewriter

Dependancies

If you wish to use the Memcached or Redis adapter you must install either PECL extensions:

  • Redis (pecl install redis)
  • Memcached (pecl install memcached)

Examples

// Basic example of the implementation.

$redis = new \Redis();
$redis->connect('....');

$adapter = new RedisCircuitBreakerAdapter($redis);
$circuit = new CircuitBreaker($adapter);
$rewriter = new UrlRewriter($circuit, new DigidipSubdomainRewriterStrategy('mydomain'));

$url = $rewriter->getUrl('http://www.merchant.com');

// ... $url is used elsewhere as part of your implementation...

For more check the examples directory.

Key Concepts

  • An explanation of the circuit breaker pattern.
  • When the failure count is zero, then the circuit is considered Closed.
  • When the failure count is greater than zero, but less than the failure threshold, the circuit is considered Half-Open.
  • When the failure count is greater than zero and equal to the failure threshold, the circuit is considered Open.
  • When the circuit is Closed or Half-Open, the UrlRewriter theoretically will convert https://mymerchant.com to http://visit.digidip.net/visit?url=http%3A%2F%2Fmymerchant.com.
  • When the circuit is Open, the UrlRewriter will simply return https://mymerchant.com with no rewriter strategy being used.

Url Writer

This class is used to bind the Circuit Breaker and URL Writer strageies together, ideally you should only need to interact with this class in your existing code base. Reference Url Writer Strategies below for available strategies.

use digidip\UrlRewriter;

$urlRewriter = new UrlRewriter(
    // ... CircuitBreaker $circuitBreaker,
    // ... RewriterStrategy $strategy,
    // ... LoggerInterface $logger = null       (PSR Logger Instance)
);

$url = $urlRewriter->getUrl('https://www.a-merchant.com/'); // Return a URL whiuch is defined by the <Method>RewriterStrategy.

Circuit Breaker

The circuit breaker class is the responsible on whether the URL Rewriter should perform a URL rewrite or not. It minimal requirements are an adapter which provides the Circuit Breaker a mechanism to communicate with an external storage resource, such as a file, Redis, Memcache or any bespoke method which can be implemented with the CircuitBreakerAdapter interface. Reference Circuit Breaker Adapters below for available adapters.

use digidip\CircuitBreaker;
use digidip\Adapters\FilePathCircuitBreakerAdapter;

$adapter = new FilePathCircuitBreakerAdapter('/path/to/file.json');
$circuit = new CircuitBreaker(
    $adapter,
    // ... ?array $options = [],
    // ... ?string $url = null,                 For testing purposes
    // ... ?Client $client = null,              For testing purposes
    // ... ?LoggerInterface $logger = null      (PSR Logger Instance)
);

Circuit Breaker Options (?array $options = [])

  • CircuitBreaker::OPTION_TIMEOUT - Number of milliseconds before the HTTP request will timeout and be considered a service failure. Default: 1000
  • CircuitBreaker::OPTION_FAILURE_THRESHOLD - The amount of failures which must occur before opening the circuit. Default: 5
  • CircuitBreaker::OPTION_OPENED_SAMPLE_RATE - The number of seconds between polling the service's availablity, once a single failure has been detected, this sample rate will be used. Default: 1
  • CircuitBreaker::OPTION_CLOSED_SAMPLE_RATE - The number of seconds between polling the service's availablity while no failures are detected. Default: 5

Url Writer Strategies

Currently available stategies include:

  • Standard Digidip Rewriter Strategy - DigidipRewriterStrategy(pid)
  • Subdomain based Digidip Rewriter Strategy - DigidipSubdomainRewriterStrategy(subdomain)
  • Template Rewriter - TemplateRewriterStrategy(url template)

Circuit Breaker Adapters

Currently available adapters include:

  • File Path - FilePathCircuitBreakerAdapter(filepath)
  • File reader and writer interfaces - FileCircuitBreakerAdapter(Reader, Writer), reference src/Modules/Filesystem for available Readers and Writers.
  • Redis - RedisCircuitBreakerAdapter(\Redis instance)
  • Memcached - MemcachedCircuitBreakerAdapter(\Memcached instance)

digidip/url-rewriter 适用场景与选型建议

digidip/url-rewriter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 digidip/url-rewriter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-23