Code Monkey home page Code Monkey logo

appmsgsrv's People

Contributors

88250 avatar sf100 avatar soa4java avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

appmsgsrv's Issues

会话推送

需求

需要精确到客户端会话(例如:设备 1、设备 2、浏览器 tab1、浏览器 tab2)进行推送。

比如一个用户可以同时登录多个客户端,有的应用场景只需要推送给其中某个客户端。

设计

session_id + @user 为 Name(key) 的方式:

  • session_iduser_id + _ + device_id(设备)/ tab_id(浏览器)
    • device_iddevice_type (iOS/Android) + - + real_device_id
    • tab_idbrowser_type + - + browser_version + - + random_num
  • 端订阅 key 改为 session_id + @user
  • 会话状态管理
  • 推送时在参数中加入会话(sessions):
    1. 给全部会话(["all"]
    2. 给指定会话(["xxx1", "xxx2"]
    3. 给激活的会话(["active"]
    4. 给非激活的会话(["inactive"]

配额模型

配额项

  • API 调用计数 (type: api_cnt)
  • 推送计数 (type: push_cnt)
  • 到期时间 (type: expire)

配额

自动分配给用户客户端 / 应用配额,使用超限后结束调用并给出受限返回值。

  • 用户客户端 API:限制到期时间
  • 应用 API:限制调用次数、推送个数、到期时间

实现

  • 配额同步接口 #47
  • 每 5 分钟重新从数据库中加载一次配额记录到内存

群 API 文档

  • 创建群
  • 获取群用户
  • 更新群主题
  • 添加群用户
  • 删除群用户

添加/删除群成员需要发消息

添加群成员:/app/client/device/addQunMember
删除群成员:/app/client/device/delQunMember

消息文案

操作者(创建者)

  • 您邀请xxx、xxx、xxx等N人加入了群聊
  • 您将xxx、xxx、xxx等N人移出了群聊

已有成员

  • xxx邀请xxx、xxx、xxx等N人加入了群聊
  • xxx将xxx、xxx、xxx等N人移出了群聊

被添加者

  • 您被xxx邀请加入群聊

被删除者

  • 您被xxx移出了群聊

主动退出

  • 您退出了群聊

JS 客户端

参考 会话推送

  • 在浏览器中生成会话 id
  • 订阅 key 使用 uid + _ + session_id + @user 拼接,使用者应该只用传入 uid 作为实参

MySQL 消息持久化

  • 使用 gopush-cluster 原生机制
  • 测试该机制是否工作正常
    • 持久化
    • 消息过期扫描
    • 离线消息获取

节点集群

在 63 搭建节点集群(每个服务进程至少两个节点)并进行测试。

连接压力测试

场景:

  • 在线会话
  • 并发会话新建、删除

用 WebSocket 协议,用 Go 开发压测工具。

用户同步开发

  • 存在则更新
  • 不存在则添加
  • 需要验证 app token

相关数据库表:

  • user
  • org_user (如果参数里有 orgId 则关联插入)

会话状态管理

设计

  • comet 建立连接时在 session 表中插入记录;断开连接时删除该记录 (会话初始化状态为 init)
    • websocket
    • tcp
  • 用户登录后进行推送会话关联(使用 baseRequest.deviceID 作为会话 id)
  • 设置最大会话时间,定时回收无效会话

接口

需要对外提供 HTTP 接口:

  • 激活/反激活会话:在 session 表中标识出该 user_id 的某会话状态是 active / inactive (应用接口 /app/client/app/setSessionState & 设备接口 /app/client/device/setSessionState
  • 获取会话集:获取指定 user_id 的会话集(应用接口 /app/client/app/getSessions

删除无效 apns token

iOS 通知时如果 APNS 返回 INVALID_TOKEN,则删除该 apns token 记录。

push.goclient.go 中实现。

租户化改造

在表中加入 tenant_idcustomer_id,用于区分租户、客户。

  • application 加入 tenant_id
  • tenant 加入 customer_id
  • qun 加入 tenant_id

用户设备记录

调用登录接口后需要记录下用户的设备信息,数据库表:client

baseRequest 新增参数 deviceType

baseRequest: {
    "uid": "",
    "deviceID": "",
    "deviceType": "", // iOS / Android
    "token": ""
}

调用计数

  • API 调用计数
  • 推送计数

两个层面进行统计。


  • 统一进行 Handler 包装
  • 需要进行调用统计的 API 如下:
    • app.Device.Push
    • app.App.UserPush
    • app.Device.Login

用户-组织关系管理接口

提供 用户-组织关联关系管理接口:

  • get by org_id,返回组织下的用户 id 集
  • get by user_id,返回该用户所属的组织 id 集
  • add org_id-user_id,添加用户-组织关联关系
  • remove org_id-user_id,移除用户-组织关联关系

····

数据库表:

  • org_user

需要验证 app token。

文件推送

支持客户端的

  • 语音
  • 图片
  • 链接

推送,同客户端部分一起进行实现设计。

代码整理

  • msg-js.js,里面冗余的代码要有明确注释,不确定的代码要明确用途
  • golang 部分,补充一下必要的注释,比如函数注释

文件链接管理

  • 客户端的消息中会包含_文件链接_
  • 将_文件链接_持久化到数据库
  • 文件默认 1 周过期:调用_文件存储 API_ 进行删除

组织/租户同步开发

  • syncOrg
  • syncTenant(加入 customer_id 字段)
  • postman 测试用例
  • 接口文档整理

相关数据库表:

  • customer
  • org
  • tenant

统计 SQL

方便出统计报告。


-- API 调用计数

-- 按 API
select api_name, type, sum(count) as 'sum', customer.name as 'customer_name' from api_call
inner join customer on api_call.customer_id = customer.id
where 1=1 group by type, api_name, customer_id order by api_name, type;

-- 总计
select sum(count), customer.name as 'customer_name' from api_call
inner join customer on api_call.customer_id = customer.id
where 1=1 group by customer_id;

-- 客户 TOP10 租户
select sum(count) as sum, tenant.name as 'tentant_name', customer.name as 'customer_name'
from api_call
inner join customer on api_call.customer_id = customer.id
inner join tenant on api_call.tenant_id = tenant.id
where 1=1 group by api_call.customer_id, api_call.tenant_id
order by sum desc limit 10;

----

-- 实际推送计数

-- 按类型
select push_type, type, sum(count) as 'sum', customer.name as 'customer_name' from push_cnt
inner join customer on push_cnt.customer_id = customer.id
where 1=1 group by type, push_type, customer_id order by push_type, type;

-- 总计
select sum(count) as 'sum', customer.name as 'customer_name' from push_cnt
inner join customer on push_cnt.customer_id = customer.id
where 1=1 group by customer_id;

-- 客户 TOP10 租户
select sum(count) as sum, tenant.name as 'tentant_name', customer.name as 'customer_name'
from push_cnt
inner join customer on push_cnt.customer_id = customer.id
inner join tenant on push_cnt.tenant_id = tenant.id
where 1=1 group by push_cnt.customer_id, push_cnt.tenant_id
order by sum desc limit 10;

性能压测

  • 应用推送
  • 设备推送
  • 在线
  • 重连

记录内存、CPU、连接数;


压测结果

压测前:
web 内存:12m cpu:0.1 %
message 内存:9516 cpu:0.0 %
comet 内存:11m cpu:0.0 %
mysql 内存 :2.1g cpu: 13 %
redis 内存 :12m cpu: 0.0 %
网络连接数:224

*/app/client/app/user/push 并发数:5000

web 内存:1.1g cpu:42 %
message 内存:15m cpu:16 %
comet 内存:22m cpu:19 %
mysql 内存 :2.1g cpu:53.6 %
网络连接数:5633
10分钟发送消息:119399次

*/app/client/device/push并发数:5000

web 内存:887m cpu:49.6 %
message 内存:15m cpu:2.0 %
comet 内存:15m cpu:6.7 %
mysql 内存 :2.3g cpu:64.3 %
redis 内存 :12m cpu:7%
网络连接数:10592
10分钟发送消息:68497次

*/app/client/device/login 并发数:5000

web 内存:968m cpu:59.6 %
message 内存:13m cpu:0.0 %
comet 内存:15m cpu:0.0 %
mysql 内存 :2.4g cpu:71.3 %
redis 内存 :27m cpu:4.0%
网络连接数:7616
10分钟发送消息:111748次

YOP 消息推送支持

  • 帮助应用开发进行接入
  • 编写针对《YOP 的有信使用文档》
  • 验证接口开发
  • 缺陷修复

Web 消息收发

20141110:网页版暂时不做,但需要完成使用 JS 客户端进行聊天,该任务作为 #15 的验证任务。

推送压力测试

场景:

  • 用户-用户:/app/client/device/push
  • 用户-群:/app/client/device/push
  • 应用-用户:/app/client/app/user/push

注意关注数据库和 Redis。

apns token 管理

  • 客户端调用 apns token 添加接口持久化该客户端 apns token
  • 提供内部函数(根据 user_id 查询 apns_token)
  • 数据库表:appns_token

网页版

参考 马上云

  • 登录
  • 通讯录
  • 消息中心

  • 前端部分只进行简单的开发
    • 登录
    • 样式对齐即可
  • 重点开发后端支撑接口

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.