mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-13 12:08:11 -07:00
initial work on publish
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
||||
// Use of this source code is governed by the BSD 2-Clause license,
|
||||
// which can be found in the LICENSE file.
|
||||
package server
|
||||
|
||||
import (
|
||||
zmq "github.com/alecthomas/gozmq"
|
||||
)
|
||||
|
||||
func (s *Server) publish() {
|
||||
context, err := zmq.NewContext()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer context.Close()
|
||||
|
||||
socket, err := context.NewSocket(zmq.PUB)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer socket.Close()
|
||||
|
||||
socket.Bind(s.conf.PubAddr)
|
||||
|
||||
for msg := range s.pubChan {
|
||||
socket.Send([]byte(msg), 0)
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,8 @@ type Server struct {
|
||||
|
||||
waitgroup sync.WaitGroup
|
||||
|
||||
pubChan chan string
|
||||
|
||||
http.Server
|
||||
}
|
||||
|
||||
@@ -46,6 +48,7 @@ func New(conf *config.Config) (*Server, error) {
|
||||
s := &Server{
|
||||
conf: conf,
|
||||
dbConnPool: pool,
|
||||
pubChan: make(chan string),
|
||||
Server: http.Server{
|
||||
Addr: conf.Addr,
|
||||
ReadTimeout: conf.ReadTimeout.Duration,
|
||||
@@ -66,6 +69,7 @@ func (s *Server) ListenAndServe() error {
|
||||
s.startTime = time.Now()
|
||||
|
||||
go s.updateRPM()
|
||||
go s.publish()
|
||||
s.Serve(s.listener)
|
||||
|
||||
s.waitgroup.Wait()
|
||||
@@ -79,6 +83,7 @@ func (s *Server) Stop() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
close(s.pubChan)
|
||||
return s.listener.Close()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user