Commit 020cac6d authored by AlexStocks's avatar AlexStocks

Add: go lint

parent 614f0c73
...@@ -59,6 +59,12 @@ jobs: ...@@ -59,6 +59,12 @@ jobs:
go fmt ./... && [[ -z `git status -s` ]] go fmt ./... && [[ -z `git status -s` ]]
/tmp/tools/license/license-header-checker -v -a -r -i vendor /tmp/tools/license/license.txt . go && [[ -z `git status -s` ]] /tmp/tools/license/license-header-checker -v -a -r -i vendor /tmp/tools/license/license.txt . go && [[ -z `git status -s` ]]
- name: Install go ci lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.27.0
- name: Run Linter
run: golangci-lint run --timeout=10m -v --disable-all --enable=govet --enable=staticcheck --enable=ineffassign --enable=misspell
- name: Test - name: Test
run: go mod vendor && go test $(go list ./... | grep -v vendor | grep -v demo) -coverprofile=coverage.txt -covermode=atomic run: go mod vendor && go test $(go list ./... | grep -v vendor | grep -v demo) -coverprofile=coverage.txt -covermode=atomic
......
...@@ -126,7 +126,7 @@ func NewWSSClient(opts ...ClientOption) Client { ...@@ -126,7 +126,7 @@ func NewWSSClient(opts ...ClientOption) Client {
c := newClient(WSS_CLIENT, opts...) c := newClient(WSS_CLIENT, opts...)
if c.cert == "" { if c.cert == "" {
panic(fmt.Sprintf("@certs:%s", c.cert)) panic(fmt.Sprintf("@cert:%s", c.cert))
} }
if !strings.HasPrefix(c.addr, "wss://") { if !strings.HasPrefix(c.addr, "wss://") {
panic(fmt.Sprintf("the prefix @serverAddr:%s is not wss://", c.addr)) panic(fmt.Sprintf("the prefix @serverAddr:%s is not wss://", c.addr))
...@@ -135,11 +135,11 @@ func NewWSSClient(opts ...ClientOption) Client { ...@@ -135,11 +135,11 @@ func NewWSSClient(opts ...ClientOption) Client {
return c return c
} }
func (c client) ID() EndPointID { func (c *client) ID() EndPointID {
return c.endPointID return c.endPointID
} }
func (c client) EndPointType() EndPointType { func (c *client) EndPointType() EndPointType {
return c.endPointType return c.endPointType
} }
...@@ -154,7 +154,7 @@ func (c *client) dialTCP() Session { ...@@ -154,7 +154,7 @@ func (c *client) dialTCP() Session {
return nil return nil
} }
if c.sslEnabled { if c.sslEnabled {
if sslConfig, err := c.tlsConfigBuilder.BuildTlsConfig(); err == nil && sslConfig != nil { if sslConfig, buildTlsConfErr := c.tlsConfigBuilder.BuildTlsConfig(); buildTlsConfErr == nil && sslConfig != nil {
d := &net.Dialer{Timeout: connectTimeout} d := &net.Dialer{Timeout: connectTimeout}
conn, err = tls.DialWithDialer(d, "tcp", c.addr, sslConfig) conn, err = tls.DialWithDialer(d, "tcp", c.addr, sslConfig)
} }
...@@ -285,7 +285,7 @@ func (c *client) dialWSS() Session { ...@@ -285,7 +285,7 @@ func (c *client) dialWSS() Session {
if c.cert != "" { if c.cert != "" {
certPEMBlock, err := ioutil.ReadFile(c.cert) certPEMBlock, err := ioutil.ReadFile(c.cert)
if err != nil { if err != nil {
panic(fmt.Sprintf("ioutil.ReadFile(certs:%s) = error:%+v", c.cert, perrors.WithStack(err))) panic(fmt.Sprintf("ioutil.ReadFile(cert:%s) = error:%+v", c.cert, perrors.WithStack(err)))
} }
var cert tls.Certificate var cert tls.Certificate
...@@ -307,7 +307,7 @@ func (c *client) dialWSS() Session { ...@@ -307,7 +307,7 @@ func (c *client) dialWSS() Session {
for _, c := range config.Certificates { for _, c := range config.Certificates {
roots, err = x509.ParseCertificates(c.Certificate[len(c.Certificate)-1]) roots, err = x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
if err != nil { if err != nil {
panic(fmt.Sprintf("error parsing server's root certs: %+v\n", perrors.WithStack(err))) panic(fmt.Sprintf("error parsing server's root cert: %+v\n", perrors.WithStack(err)))
} }
for _, root = range roots { for _, root = range roots {
certPool.AddCert(root) certPool.AddCert(root)
......
...@@ -293,8 +293,10 @@ func TestNewWSClient(t *testing.T) { ...@@ -293,8 +293,10 @@ func TestNewWSClient(t *testing.T) {
assert.Equal(t, beforeWriteBytes+5, atomic.LoadUint32(&conn.writeBytes)) assert.Equal(t, beforeWriteBytes+5, atomic.LoadUint32(&conn.writeBytes))
beforeWritePkgNum := atomic.LoadUint32(&conn.writePkgNum) beforeWritePkgNum := atomic.LoadUint32(&conn.writePkgNum)
err = ss.WriteBytes([]byte("hello")) err = ss.WriteBytes([]byte("hello"))
assert.Nil(t, err)
assert.Equal(t, beforeWritePkgNum+1, atomic.LoadUint32(&conn.writePkgNum)) assert.Equal(t, beforeWritePkgNum+1, atomic.LoadUint32(&conn.writePkgNum))
err = ss.WriteBytesArray([]byte("hello"), []byte("hello")) err = ss.WriteBytesArray([]byte("hello"), []byte("hello"))
assert.Nil(t, err)
assert.Equal(t, beforeWritePkgNum+3, atomic.LoadUint32(&conn.writePkgNum)) assert.Equal(t, beforeWritePkgNum+3, atomic.LoadUint32(&conn.writePkgNum))
err = conn.writePing() err = conn.writePing()
assert.Nil(t, err) assert.Nil(t, err)
......
...@@ -27,13 +27,14 @@ var ( ...@@ -27,13 +27,14 @@ var (
func ClientRequest() { func ClientRequest() {
for _, session := range Sessions { for _, session := range Sessions {
ss := session
go func() { go func() {
echoTimes := 10 echoTimes := 10
for i := 0; i < echoTimes; i++ { for i := 0; i < echoTimes; i++ {
err := session.WritePkg("hello", WritePkgTimeout) err := ss.WritePkg("hello", WritePkgTimeout)
if err != nil { if err != nil {
log.Infof("session.WritePkg(session{%s}, error{%v}", session.Stat(), err) log.Infof("session.WritePkg(session{%s}, error{%v}", ss.Stat(), err)
session.Close() ss.Close()
} }
} }
log.Infof("after loop %d times", echoTimes) log.Infof("after loop %d times", echoTimes)
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
func SetLimit() {
}
...@@ -29,7 +29,7 @@ type Closer interface { ...@@ -29,7 +29,7 @@ type Closer interface {
func WaitCloseSignals(closer Closer) { func WaitCloseSignals(closer Closer) {
signals := make(chan os.Signal, 1) signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) signal.Notify(signals, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
<-signals <-signals
closer.Close() closer.Close()
} }
...@@ -106,20 +106,15 @@ func NewWSSServer(opts ...ServerOption) Server { ...@@ -106,20 +106,15 @@ func NewWSSServer(opts ...ServerOption) Server {
return s return s
} }
func (s server) ID() int32 { func (s *server) ID() int32 {
return s.endPointID return s.endPointID
} }
func (s server) EndPointType() EndPointType { func (s *server) EndPointType() EndPointType {
return s.endPointType return s.endPointType
} }
func (s *server) stop() { func (s *server) stop() {
var (
err error
ctx context.Context
)
select { select {
case <-s.done: case <-s.done:
return return
...@@ -128,12 +123,13 @@ func (s *server) stop() { ...@@ -128,12 +123,13 @@ func (s *server) stop() {
close(s.done) close(s.done)
s.lock.Lock() s.lock.Lock()
if s.server != nil { if s.server != nil {
ctx, _ = context.WithTimeout(context.Background(), serverFastFailTimeout) ctx, cancel := context.WithTimeout(context.Background(), serverFastFailTimeout)
if err = s.server.Shutdown(ctx); err != nil { if err := s.server.Shutdown(ctx); err != nil {
// if the log output is "shutdown ctx: context deadline exceeded", it means that // if the log output is "shutdown ctx: context deadline exceeded", it means that
// there are still some active connections. // there are still some active connections.
log.Errorf("server shutdown ctx:%s error:%v", ctx, err) log.Errorf("server shutdown ctx:%s error:%v", ctx, err)
} }
cancel()
} }
s.server = nil s.server = nil
s.lock.Unlock() s.lock.Unlock()
...@@ -179,7 +175,7 @@ func (s *server) listenTCP() error { ...@@ -179,7 +175,7 @@ func (s *server) listenTCP() error {
} }
} else { } else {
if s.sslEnabled { if s.sslEnabled {
if sslConfig, err := s.tlsConfigBuilder.BuildTlsConfig(); err == nil && sslConfig != nil { if sslConfig, buildTlsConfErr := s.tlsConfigBuilder.BuildTlsConfig(); buildTlsConfErr == nil && sslConfig != nil {
streamListener, err = tls.Listen("tcp", s.addr, sslConfig) streamListener, err = tls.Listen("tcp", s.addr, sslConfig)
} }
} else { } else {
...@@ -420,7 +416,6 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) { ...@@ -420,7 +416,6 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
if certificate, err = tls.LoadX509KeyPair(s.cert, s.privateKey); err != nil { if certificate, err = tls.LoadX509KeyPair(s.cert, s.privateKey); err != nil {
panic(fmt.Sprintf("tls.LoadX509KeyPair(certs{%s}, privateKey{%s}) = err:%+v", panic(fmt.Sprintf("tls.LoadX509KeyPair(certs{%s}, privateKey{%s}) = err:%+v",
s.cert, s.privateKey, perrors.WithStack(err))) s.cert, s.privateKey, perrors.WithStack(err)))
return
} }
config = &tls.Config{ config = &tls.Config{
InsecureSkipVerify: true, // do not verify peer certs InsecureSkipVerify: true, // do not verify peer certs
......
...@@ -19,6 +19,7 @@ package getty ...@@ -19,6 +19,7 @@ package getty
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"net" "net"
...@@ -120,7 +121,7 @@ func newSession(endPoint EndPoint, conn Connection) *session { ...@@ -120,7 +121,7 @@ func newSession(endPoint EndPoint, conn Connection) *session {
once: &sync.Once{}, once: &sync.Once{},
done: make(chan struct{}), done: make(chan struct{}),
wait: pendingDuration, wait: pendingDuration,
attrs: gxcontext.NewValuesContext(nil), attrs: gxcontext.NewValuesContext(context.Background()),
rDone: make(chan struct{}), rDone: make(chan struct{}),
} }
...@@ -162,7 +163,7 @@ func (s *session) Reset() { ...@@ -162,7 +163,7 @@ func (s *session) Reset() {
done: make(chan struct{}), done: make(chan struct{}),
period: period, period: period,
wait: pendingDuration, wait: pendingDuration,
attrs: gxcontext.NewValuesContext(nil), attrs: gxcontext.NewValuesContext(context.Background()),
rDone: make(chan struct{}), rDone: make(chan struct{}),
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment