分区和位移的参数从url输入
This commit is contained in:
@@ -37,13 +37,14 @@ var (
|
|||||||
newline = []byte{'\n'}
|
newline = []byte{'\n'}
|
||||||
addr = flag.String("addr", ":9000", "http service address")
|
addr = flag.String("addr", ":9000", "http service address")
|
||||||
bootstrapServers = flag.String("bootstrap-servers", "localhost:9092", "kafka bootstrap servers")
|
bootstrapServers = flag.String("bootstrap-servers", "localhost:9092", "kafka bootstrap servers")
|
||||||
partition = flag.Int("partition", 0, "kafka tipic partion")
|
// partition = flag.Int("partition", 0, "kafka tipic partion")
|
||||||
wait = flag.Duration("graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
|
wait = flag.Duration("graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
|
||||||
upgrader = websocket.Upgrader{
|
upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
}
|
}
|
||||||
hubMap = make(map[string]Hub)
|
hubMap = make(map[string]Hub)
|
||||||
|
router = mux.NewRouter()
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client is a middleman between the websocket connection and the hub.
|
// Client is a middleman between the websocket connection and the hub.
|
||||||
@@ -57,20 +58,20 @@ type Client struct {
|
|||||||
send chan []byte
|
send chan []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func create(topic string, offset int64) *kafka.Reader {
|
func create(topic string, offset int64, partition int) *kafka.Reader {
|
||||||
group, _ := uuid.NewV4()
|
group := uuid.NewV4()
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"topic": topic,
|
"topic": topic,
|
||||||
"bootstrap servers": *bootstrapServers,
|
"bootstrap servers": *bootstrapServers,
|
||||||
"partition": *partition,
|
"partition": partition,
|
||||||
"group": group,
|
"group": group,
|
||||||
"offset": offset}).Info("creating kafka client ... ")
|
"offset": offset}).Info("creating kafka client ... ")
|
||||||
|
|
||||||
reader := kafka.NewReader(kafka.ReaderConfig{
|
reader := kafka.NewReader(kafka.ReaderConfig{
|
||||||
Brokers: strings.Split(*bootstrapServers, ","),
|
Brokers: strings.Split(*bootstrapServers, ","),
|
||||||
Topic: topic,
|
Topic: topic,
|
||||||
Partition: *partition,
|
Partition: partition,
|
||||||
MinBytes: 10e1,
|
MinBytes: 10e1,
|
||||||
MaxBytes: 10e6, // 10MB
|
MaxBytes: 10e6, // 10MB
|
||||||
})
|
})
|
||||||
@@ -161,10 +162,12 @@ func (c *Client) writePump() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// serveWs handles websocket requests from the peer.
|
// serveWs handles websocket requests from the peer.
|
||||||
func serveWs(w http.ResponseWriter, r *http.Request) {
|
func websocketHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
topic := vars["topic"]
|
topic := vars["topic"]
|
||||||
offsetString := vars["offset"]
|
partitionString := vars["partition"]
|
||||||
|
partition, _ := strconv.Atoi(partitionString)
|
||||||
|
offsetString := r.FormValue("offset")
|
||||||
offset, _ := strconv.ParseInt(offsetString, 10, 64)
|
offset, _ := strconv.ParseInt(offsetString, 10, 64)
|
||||||
|
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
@@ -172,11 +175,14 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Error("upgrade connection failed ", err)
|
log.Error("upgrade connection failed ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
key := topic + "|" + offsetString
|
key := topic + "|" + partitionString + "|" + offsetString
|
||||||
h, ok := hubMap[key]
|
h, ok := hubMap[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.WithFields(log.Fields{"topic": topic, "offset": offset}).Info("create new hub ")
|
log.WithFields(
|
||||||
h = *newHub(topic, offset)
|
log.Fields{"topic": topic,
|
||||||
|
"offset": offset,
|
||||||
|
"partition": partition}).Info("create new hub ")
|
||||||
|
h = *newHub(topic, offset, partition)
|
||||||
go h.run()
|
go h.run()
|
||||||
hubMap[key] = h
|
hubMap[key] = h
|
||||||
} else {
|
} else {
|
||||||
@@ -198,6 +204,8 @@ type Hub struct {
|
|||||||
|
|
||||||
topic string
|
topic string
|
||||||
|
|
||||||
|
partition int
|
||||||
|
|
||||||
offset int64
|
offset int64
|
||||||
|
|
||||||
reader *kafka.Reader
|
reader *kafka.Reader
|
||||||
@@ -215,11 +223,13 @@ type Hub struct {
|
|||||||
unregister chan *Client
|
unregister chan *Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHub(topic string, offset int64) *Hub {
|
func newHub(topic string, offset int64, partition int) *Hub {
|
||||||
return &Hub{
|
return &Hub{
|
||||||
running: true,
|
running: true,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
|
partition: partition,
|
||||||
offset: offset,
|
offset: offset,
|
||||||
|
reader: create(topic, offset, partition),
|
||||||
broadcast: make(chan []byte),
|
broadcast: make(chan []byte),
|
||||||
register: make(chan *Client),
|
register: make(chan *Client),
|
||||||
unregister: make(chan *Client),
|
unregister: make(chan *Client),
|
||||||
@@ -233,7 +243,7 @@ func (h *Hub) read() {
|
|||||||
if h.reader == nil {
|
if h.reader == nil {
|
||||||
log.Info("waiting ... ")
|
log.Info("waiting ... ")
|
||||||
time.Sleep(writeWait)
|
time.Sleep(writeWait)
|
||||||
h.reader = create(h.topic, h.offset)
|
h.reader = create(h.topic, h.offset, h.partition)
|
||||||
}
|
}
|
||||||
m, err := h.reader.ReadMessage(context.Background())
|
m, err := h.reader.ReadMessage(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -249,7 +259,7 @@ func (h *Hub) read() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) getKey() string {
|
func (h *Hub) getKey() string {
|
||||||
return h.topic + "|" + strconv.FormatInt(h.offset, 10)
|
return h.topic + "|" + strconv.Itoa(h.partition) + "|" + strconv.FormatInt(h.offset, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) run() {
|
func (h *Hub) run() {
|
||||||
@@ -286,8 +296,7 @@ func (h *Hub) run() {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
r := mux.NewRouter()
|
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
|
||||||
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: *addr,
|
Addr: *addr,
|
||||||
// Good practice to set timeouts to avoid Slowloris attacks.
|
// Good practice to set timeouts to avoid Slowloris attacks.
|
||||||
@@ -299,9 +308,10 @@ func main() {
|
|||||||
|
|
||||||
// hub := newHub()
|
// hub := newHub()
|
||||||
// go hub.run()
|
// go hub.run()
|
||||||
r.HandleFunc("/ws/{topic}/{offset}", func(w http.ResponseWriter, r *http.Request) {
|
// r.HandleFunc("/ws/{topic}/{offset}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
serveWs(w, r)
|
// serveWs(w, r)
|
||||||
})
|
// })
|
||||||
|
router.Path("/ws/{topic}/{partition}").Queries("offset", "{offset}").HandlerFunc(websocketHandler).Name("web-socket")
|
||||||
|
|
||||||
// Run our server in a goroutine so that it doesn't block.
|
// Run our server in a goroutine so that it doesn't block.
|
||||||
go func() {
|
go func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user