diff --git a/.vscode/settings.json b/.vscode/settings.json index 46c249c..20af2f6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,11 +1,3 @@ // Place your settings in this file to overwrite default and user settings. { - // Specifies Lint tool name. - "go.lintTool": "gometalinter", - - // Flags to pass to Lint tool (e.g. ["-min_confidence=.8"]) - "go.lintFlags": [ - "--config=${workspaceRoot}/golint.json" - ] - } \ No newline at end of file diff --git a/glide.yaml b/glide.yaml index 88d38f8..4625fd5 100644 --- a/glide.yaml +++ b/glide.yaml @@ -17,3 +17,4 @@ import: - package: git.loafle.net/commons_go/util subpackages: - channel +- package: git.loafle.net/commons_go/logging diff --git a/golint.json b/golint.json deleted file mode 100644 index 6380714..0000000 --- a/golint.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "DisableAll": true, - "Enable": [ - "aligncheck", - "deadcode", - "dupl", - "errcheck", - "gas", - "goconst", - "gocyclo", - "gofmt", - "goimports", - "golint", - "gotype", - "ineffassign", - "interfacer", - "lll", - "megacheck", - "misspell", - "structcheck", - "test", - "testify", - "unconvert", - "varcheck", - "vet", - "vetshadow" - ], - "Aggregate": true, - "Concurrency": 16, - "Cyclo": 60, - "Deadline": "60s", - "DuplThreshold": 50, - "EnableGC": true, - "LineLength": 120, - "MinConfidence": 0.8, - "MinOccurrences": 3, - "MinConstLength": 3, - "Sort": ["severity"] -} \ No newline at end of file diff --git a/protocol/jsonrpc/handler.go b/protocol/jsonrpc/handler.go index 1de5cd4..945c240 100644 --- a/protocol/jsonrpc/handler.go +++ b/protocol/jsonrpc/handler.go @@ -1,11 +1,13 @@ package jsonrpc import ( + "context" "encoding/json" - "fmt" "io" - "log" + "go.uber.org/zap" + + "git.loafle.net/commons_go/logging" gws "git.loafle.net/overflow/overflow_gateway_websocket" ) @@ -14,12 +16,16 @@ type MessageHandler interface { } type messageHandler struct { - o *Options + ctx context.Context + logger *zap.Logger + o *Options } -func NewHandler(o *Options) MessageHandler { +func NewHandler(ctx context.Context, o *Options) MessageHandler { h := &messageHandler{ - o: o, + ctx: ctx, + logger: logging.WithContext(ctx), + o: o, } return h @@ -56,7 +62,7 @@ func (h *messageHandler) onRequest(soc gws.Socket, req *ServerRequest) []byte { j, err := json.Marshal(res) if nil != err { - log.Println(fmt.Errorf("%v", err)) + h.logger.Error("JSON RPC error", zap.Any("err", err)) } return j @@ -65,6 +71,6 @@ func (h *messageHandler) onRequest(soc gws.Socket, req *ServerRequest) []byte { func (h *messageHandler) onNotify(soc gws.Socket, req *ServerRequest) { err := h.o.OnNotify(soc, req.Method, req.Params) if nil != err { - log.Println(fmt.Errorf("%v", err)) + h.logger.Error("JSON RPC error", zap.Any("err", err)) } } diff --git a/server.go b/server.go index 216aeb5..0528fa2 100644 --- a/server.go +++ b/server.go @@ -2,9 +2,11 @@ package overflow_gateway_websocket import ( "context" - "log" "net/http" + "go.uber.org/zap" + + "git.loafle.net/commons_go/logging" channelUtil "git.loafle.net/commons_go/util/channel" "git.loafle.net/overflow/overflow_gateway_websocket/websocket" "github.com/valyala/fasthttp" @@ -24,6 +26,7 @@ type Server interface { type server struct { _ctx context.Context + _logger *zap.Logger _option *ServerOptions _upgrader *websocket.Upgrader _handlers map[string]*SocketOptions @@ -34,6 +37,7 @@ type server struct { func NewServer(ctx context.Context, o *ServerOptions) Server { s := &server{ _ctx: ctx, + _logger: logging.WithContext(ctx), _option: o.Validate(), _handlers: make(map[string]*SocketOptions, 1), _sockets: make(map[string]Socket, 100), @@ -94,7 +98,7 @@ func (s *server) onConnection(ctx *fasthttp.RequestCtx) { return } id := s._option.IDGenerator(ctx) - soc := NewSocket(id, path, co, conn) + soc := NewSocket(s._ctx, id, path, co, conn) s.addSocket(soc) s._option.OnConnection(soc) @@ -106,7 +110,7 @@ func (s *server) listenHandler() { for { select { case <-s._ctx.Done(): - log.Println("websocket server: Context Done") + s._logger.Info("websocket server: Context Done") return case ca := <-s._socketsCh: switch ca.Type { diff --git a/socket.go b/socket.go index c1b4976..1e98aa1 100644 --- a/socket.go +++ b/socket.go @@ -1,11 +1,13 @@ package overflow_gateway_websocket import ( - "fmt" + "context" "io" - "log" "time" + "go.uber.org/zap" + + "git.loafle.net/commons_go/logging" "git.loafle.net/overflow/overflow_gateway_websocket/websocket" ) @@ -18,6 +20,8 @@ type Socket interface { } type socket struct { + ctx context.Context + logger *zap.Logger id string o *SocketOptions conn *websocket.Conn @@ -27,8 +31,10 @@ type socket struct { disconnectCh chan bool } -func NewSocket(id string, path string, o *SocketOptions, conn *websocket.Conn) Socket { +func NewSocket(ctx context.Context, id string, path string, o *SocketOptions, conn *websocket.Conn) Socket { c := &socket{ + ctx: ctx, + logger: logging.WithContext(ctx), id: id, o: o, conn: conn, @@ -106,12 +112,12 @@ func (soc *socket) listenWrite() { case w := <-soc.writeCh: if writeTimeout := soc.o.WriteTimeout; writeTimeout > 0 { err := soc.conn.SetWriteDeadline(time.Now().Add(writeTimeout)) - log.Println(fmt.Errorf("%v", err)) + soc.logger.Error("Socket timeout", zap.Any("err", err)) } err := soc.conn.WriteMessage(soc.messageType, w) if nil != err { - log.Println(fmt.Errorf("%v", err)) + soc.logger.Error("Socket Write error", zap.Any("err", err)) } // receive done request