graymatterlabs/pingtree
Composer 安装命令:
composer require graymatterlabs/pingtree
包简介
A Ping Tree is a process in which a single lead is offered to multiple buyers in real-time and sold to the highest bidder
关键字:
README 文档
README
A Ping Tree is a process in which a single lead is offered to multiple buyers in real-time and sold to the highest bidder.
Installation
You can install the package via composer:
composer require graymatterlabs/pingtree:^0.3.0
Usage
$tree = new Tree($strategy, $offers); $response = $tree->ping($lead);
Responses
The ping tree will return the first successful response provided by an offer. Responses must implement the GrayMatterLabs\PingTree\Contracts\Response interface but should be customized beyond that.
$tree = new Tree($strategy, $offers); $response = $tree->ping($lead); if ($response instanceof RedirectResponse) { return redirect($response->url()); }
Offers
Offer instances are responsible for communicating with and managing state of the offer such as health and eligibility rules.
<?php namespace App\Offers; use App\Http; use App\Leads\AutoLead; use GrayMatterLabs\PingTree\Contracts\Offer; use GrayMatterLabs\PingTree\Contracts\Lead; use GrayMatterLabs\PingTree\Contracts\Response; class Example implements Offer { private string $url = 'https://example.com'; public function __construct(private Http $http, private string $key) { } public function getIdentifier(): string|int { return 'example'; } public function ping(AutoLead $lead): int { $response = $this->http ->withHeader('Authorization', $this->key) ->post($this->url . '/value', [ // ... ]); return (int) $response->json('value'); } public function send(AutoLead $lead): Response { $response = $this->http ->withHeader('Authorization', $this->key) ->post($this->url . '/send', [ // ... ]); return new RedirectResponse(!$response->ok(), $response->json('accepted'), $response->json('url')); } public function isEligible(AutoLead $lead): bool { return $lead->validate(); } public function isHealthy(): bool { return $this->http->get($this->url . '/health')->ok(); } }
Events
This package fires events and provides you the ability to register listeners for each. Listeners can be used for performing any custom logic. Listeners are executed synchronously to be sure to handle any potential exceptions.
Listeners can be registered to the Tree class, which handles all events, using the listen method, like so:
$tree = new Tree($strategy, $offers); // listen for any events $tree->listen($event, $callable); $tree->listen($event, $other_callable); $response = $tree->ping($lead);
Below is a list of all events fired, their descriptions, and the parameters passed to any registered listeners.
| Name | Description | Parameters |
|---|---|---|
selecting |
An offer is being selected | Strategy $strategy, Lead $lead, array $offers |
selected |
An offer has been selected | Strategy $strategy, Lead $lead, array $offers, Offer $offer |
sending |
The lead is being sent to the offer | Lead $lead, Offer $offer |
sent |
The lead has been send to the offer | Lead $lead, Offer $offer, Response $response, int $attempts |
Strategies
This package provides a concept of "strategies" to decide which offer to send the lead to. A default set of strategies are provided out-of-the-box. The only requirement to providing your own strategies is that they implement the GrayMatterLabs\PingTree\Contracts\Strategy interface.
| Strategy | Description |
|---|---|
| HighestPing | Gets the offer with the highest ping() value |
| RoundRobin | A decorator to ensure each offer is attempted across multiple executions for a given lead |
| Shuffled | A decorator to ensure random order before executing the given strategy |
| Ordered | Gets the first offer in the list of provided offers |
Testing
composer test
Changelog
Please see the Release Notes for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
graymatterlabs/pingtree 适用场景与选型建议
graymatterlabs/pingtree 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 351 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「graymatterlabs」 「pingtree」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 graymatterlabs/pingtree 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 graymatterlabs/pingtree 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 graymatterlabs/pingtree 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 351
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-08