Skip to content

Commit

Permalink
add seq id
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeburo committed Feb 5, 2019
1 parent 5d6f482 commit b1ec1c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kazeburo/wsgate-server/dumper"
"github.com/kazeburo/wsgate-server/mapping"
"github.com/kazeburo/wsgate-server/publickey"
"github.com/kazeburo/wsgate-server/seq"
"go.uber.org/zap"
)

Expand All @@ -30,6 +31,7 @@ type Handler struct {
mp *mapping.Mapping
pk *publickey.Publickey
dumpTCP uint
sq *seq.Seq
}

// New new handler
Expand Down Expand Up @@ -59,6 +61,7 @@ func New(
mp: mp,
pk: pk,
dumpTCP: dumpTCP,
sq: seq.New(),
}, nil
}

Expand All @@ -81,6 +84,7 @@ func (h *Handler) Proxy() func(w http.ResponseWriter, r *http.Request) {
disconnectAt := ""

logger := h.logger.With(
zap.Uint64("seq", h.sq.Next()),
zap.String("user-email", r.Header.Get("X-Goog-Authenticated-User-Email")),
zap.String("x-forwarded-for", r.Header.Get("X-Forwarded-For")),
zap.String("remote-addr", r.RemoteAddr),
Expand Down Expand Up @@ -148,6 +152,8 @@ func (h *Handler) Proxy() func(w http.ResponseWriter, r *http.Request) {
for {
select {
case <-r.Context().Done():
dr.Flush()
ds.Flush()
return
case _ = <-ticker.C:
dr.Flush()
Expand Down
25 changes: 25 additions & 0 deletions seq/seq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seq

import "sync"

// Seq struct
type Seq struct {
i uint64
mu *sync.RWMutex
}

// New create sequencer
func New() *Seq {
return &Seq{
i: 0,
mu: new(sync.RWMutex),
}
}

// Next fetch new sequence
func (s *Seq) Next() uint64 {
s.mu.Lock()
defer s.mu.Unlock()
s.i = s.i + 1
return s.i
}

0 comments on commit b1ec1c4

Please sign in to comment.