Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gorush: A push notification server written in Go

Bo-Yi Wu
October 28, 2017

Gorush: A push notification server written in Go

* Why I create the Gorush Project?
* Detail implementation in Golang
* Testing and deploy Go project.
* Run Gorush in Kubernetes.

Bo-Yi Wu

October 28, 2017
Tweet

More Decks by Bo-Yi Wu

Other Decks in Technology

Transcript

  1. A push notification server written in Go
    Gorush
    1
    Bo-Yi Wu


    Mopcon @ 2017.10.28


    View Slide

  2. ●Why I create the Gorush Project?


    ●Detail implementation in Golang


    ●Testing and deploy Go project.


    ●Run Gorush in Kubernetes.
    2
    Outline

    View Slide

  3. ● Mediatek Engineer


    ● DevOps


    ● Golang, PHP, Node.js ..


    ● Some open source


    ● Gitea


    ● Gin


    ● Drone


    ● appleboy @GitHub


    ● appleboy @twitter


    ● appleboy @slideshare


    ● appleboy46 @facebook
    About Me
    3

    View Slide

  4. ● drone/drone


    ● go-gitea/gitea


    ● gin-gonic/gin


    ● appleboy/gorush
    Go Open Source in GitHub
    4

    View Slide

  5. ● drone/drone


    ● go-gitea/gitea


    ● gin-gonic/gin


    ● appleboy/gorush
    Go Open Source in GitHub
    5

    View Slide

  6. https://mcs.mediatek.com/


    6

    View Slide

  7. Mediatek


    Cloud
    Sandbox
    Trigger
    Push
    Email
    hook
    MQTT
    TCP
    HTTP
    Mediatek Cloud Sandbox
    7

    View Slide

  8. Mediatek


    Cloud
    Sandbox
    Trigger
    Push
    Email
    hook
    MQTT
    TCP
    HTTP
    Mediatek Cloud Sandbox
    8
    Forcus on Push notification

    View Slide

  9. https://github.com/appleboy/gorush
    Gorush
    9

    View Slide

  10. A general push notification server


    for smartphone application
    Gorush
    10

    View Slide

  11. ●Written in Go


    ●Push requests to APNs and FCM


    ●Simple HTTP APIs (JSON)


    ●RPC Protocol (gRPC)
    Gorush
    11

    View Slide

  12. Overview
    Client
    FCM
    APN
    s
    Gorush
    POST /push
    JSON Body
    HTTPS
    APNs


    Protocol
    12

    View Slide

  13. ●System Requirement


    ●Good Performance


    ●High Concurrency


    ●Simple net/http Package


    ●Easy to Learn
    Why I Choose Go
    13

    View Slide

  14. https://github.com/golang/go/wiki/FromXToGo
    Why we switch

    From XX to Go
    14

    View Slide

  15. https://github.com/golangtw/jobs
    Who use Go in Taiwan
    15

    View Slide

  16. 16
    Push System Architecture

    View Slide

  17. Before Seven years ago
    FCM
    APNs
    API Server
    API Server
    API Server
    PUSH
    17

    View Slide

  18. Before Two Years ago
    FCM
    APNs
    Worker
    Worker
    Worker
    PUSH
    Queue
    API
    API
    API
    de-queue
    Asynchronous Notification
    AWS
    en-queue
    18

    View Slide

  19. Nginx


    Traefik
    Now…
    FCM
    APNs
    Gorush
    Gorush
    Gorush
    PUSH
    API
    API
    API
    Http


    Post /push


    Gorush = Queue + Worker
    19

    View Slide

  20. How to implement simple notification service
    Gorush Architecture
    20

    View Slide

  21. How to send notification
    Client
    FCM
    APN
    s
    Gorush
    POST /push
    JSON Body
    HTTPS
    APNs


    Protocol
    21

    View Slide

  22. ●Http Server


    ●gRPC Server


    ●Push API


    ●Worker
    22
    Gorush Component

    View Slide

  23. net/http + Gin Route+ Facebook Grace
    23

    View Slide

  24. Push API using Gin Framework
    24

    View Slide

  25. Initial Worker Queue
    25

    View Slide

  26. API Request and Response
    26

    View Slide

  27. Send notification to Queue
    27

    View Slide

  28. Start Worker
    28

    View Slide

  29. Gorush Major Feature
    29

    View Slide

  30. ●GET /api/stat/go


    ●GET /api/stat/app


    ●GET /sys/stats


    ●GET /metrics


    ●POST /api/push


    ●GET /healthz
    API List
    30

    View Slide

  31. GET /api/stat/go
    31

    View Slide

  32. GET /api/stat/app
    32

    View Slide

  33. GET /sys/stats
    33

    View Slide

  34. GET /metrics (Prometheus)
    34

    View Slide

  35. GET /metrics (Prometheus + Grafana)
    $ for i in {1..99999}; do bat -b.N=1000 -b.C=100 POST local:8088/api/push
    35

    View Slide

  36. GET /metrics (Prometheus + Grafana)
    36

    View Slide

  37. https://github.com/appleboy/gorush/#post-apipush
    POST /api/push
    37

    View Slide

  38. Push API Support Sync Mode
    Async vs. Sync Push
    38

    View Slide

  39. Notification
    Notification
    Notification
    Notification
    worker
    worker
    worker
    Client
    Notification
    worker
    WaitGroup
    39

    View Slide

  40. Notification
    Log
    Wait response
    Send to Work
    Queue
    40

    View Slide

  41. 41

    View Slide

  42. $ gorush
    Start Gorush Server
    42

    View Slide

  43. $ gorush –c config.yml
    Custom config file
    43

    View Slide

  44. $ GORUSH_CORE_PORT=8089 gorush
    Global Variable
    44

    View Slide

  45. ● /etc/gorush/config.yml


    ● $HOME/.gorush/config.yml


    ● . (Current Folder)
    45
    Auto Load Config file

    View Slide

  46. Server Confg
    Route
    Config
    46

    View Slide

  47. Server Config
    Route
    Config
    47

    View Slide

  48. 48
    Go Support

    Auto TLS
    Integrate Let's Encrypt

    View Slide

  49. Autocert package in Go
    49

    View Slide

  50. Write one line code to support

    renew Let’s Encrypt automatically
    https://goo.gl/pfeC5Q
    50

    View Slide

  51. Android Config
    iOS Config
    Log Config
    51

    View Slide

  52. Send Android Notification
    API Key From Request
    52

    View Slide

  53. iOS Support .p12, .pem or .p8
    53
    Supports new Apple Token Based Authentication (JWT)

    View Slide

  54. Send iOS Notification
    54

    View Slide

  55. Success or fail records
    55

    View Slide

  56. memory, blotdb, buntdb, leveldb, redis
    Storage
    56

    View Slide

  57. Interface
    57

    View Slide

  58. Init Interface
    58

    View Slide

  59. package flg
    Send Notification

    From CLI
    59

    View Slide

  60. Flag in Go
    60

    View Slide

  61. Flag doesn’t support

    Global variable
    Docker Container
    61

    View Slide

  62. Configuration Management
    62

    View Slide

  63. $ gorush -android –m="message" -k="API Key" –t="token"
    Sned Android
    63

    View Slide

  64. $ gorush -ios -m="message" -i="certificate path"


    -t="token" -topic="topic"
    Send iOS
    64

    View Slide

  65. write once run anywhere
    Cross Platform
    65

    View Slide

  66. https://github.com/mitchellh/gox
    Simple Go Cross Compilation
    66

    View Slide

  67. 67

    View Slide

  68. https://github.com/facebookgo/grace
    Graceful restart & zero downtime
    68

    View Slide

  69. How to resolve this problem?
    Facebook Grace

    doesn’t Support Windows
    69

    View Slide

  70. // +build windows


    https://golang.org/pkg/go/build/
    Go build Flag
    70

    View Slide

  71. Build Binary into

    Docker Container
    71

    View Slide

  72. Docker Multi-Stage Build
    72

    View Slide


  73. Build small image using docker multi-stage
    $ docker build -t appleboy/gorush .


    https://goo.gl/zQbhmu
    73

    View Slide

  74. Auto testing and deploy in GO
    74

    View Slide

  75. Drone
    75

    View Slide

  76. Continuous Delivery system


    built on container technology
    What is Drone?
    76

    View Slide

  77. How to setup

    deploy process?
    77
    Using .drone.yml file

    View Slide

  78. Git Test Release Docker Notify
    How to build Go binary in Drone
    78

    View Slide

  79. ● Testing Code


    ● Analytic Code


    ● Code Quality


    ● Build Binary


    ● Deploy Binary


    ● Build Docker Image


    ● Deploy Docker Container


    ● Send Notification
    Pipeline Process in Go
    79

    View Slide

  80. Git Test Release Docker Notify
    How to build Go binary in Drone
    80

    View Slide

  81. First Step: Git Clone
    81

    View Slide

  82. Git Test Release Docker Notify
    How to build Go binary in Drone
    82

    View Slide

  83. Choose Image
    Testing Process
    83

    View Slide

  84. Code Quality in Go (golint)
    84

    View Slide

  85. Check comment typos (Misspell)
    85

    View Slide

  86. Check code format in Go (gofmt)
    86

    View Slide

  87. Check vendor folder (govendor)
    87

    View Slide

  88. Testing in Go
    $ coverage all


    https://github.com/appleboy/golang-testing
    88

    View Slide

  89. Git Test Release Docker Notify
    How to build Go binary in Drone
    89

    View Slide

  90. Choose Image
    Step Name
    Run on for Git tag event
    90

    View Slide

  91. Build binary for Cross Platform (gox)
    91

    View Slide

  92. Deploy to GitHub Release Page
    92

    View Slide

  93. Git Test Release Docker Notify
    How to build Go binary in Drone
    93

    View Slide

  94. Drone Envars
    Drone Secrets
    Plugin Image
    94

    View Slide

  95. Git Test Release Docker Notify
    How to build Go binary in Drone
    95

    View Slide

  96. Plugin Image
    Push Notification From FB Bot
    96

    View Slide

  97. Happy Coding 97

    View Slide

  98. Run Gorush in Kubernets
    98

    View Slide

  99. 99
    Container Health Check

    View Slide

  100. 100
    Global Configuration Map

    View Slide

  101. 101

    View Slide

  102. $ kubectl create -f k8s


    $ kubectl get services


    $ minikube service frontend
    Three Steps
    102

    View Slide

  103. Interesting In Drone
    http://bit.ly/devops-drone
    103

    View Slide

  104. Thanks All coming.
    Any Problem?
    104

    View Slide