Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
1ff11223
Commit
1ff11223
authored
Jan 11, 2020
by
yuyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rem: utils
parent
193d5d02
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
17 additions
and
90 deletions
+17
-90
client.go
client.go
+5
-4
go.mod
go.mod
+1
-2
go.sum
go.sum
+5
-0
server.go
server.go
+2
-1
session.go
session.go
+4
-3
utils.go
utils.go
+0
-80
No files found.
client.go
View file @
1ff11223
...
...
@@ -26,6 +26,7 @@ import (
"github.com/dubbogo/gost/bytes"
"github.com/gorilla/websocket"
perrors
"github.com/pkg/errors"
"github.com/dubbogo/gost/net"
)
const
(
...
...
@@ -144,7 +145,7 @@ func (c *client) dialTCP() Session {
return
nil
}
conn
,
err
=
net
.
DialTimeout
(
"tcp"
,
c
.
addr
,
connectTimeout
)
if
err
==
nil
&&
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
if
err
==
nil
&&
gxnet
.
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
conn
.
Close
()
err
=
errSelfConnect
}
...
...
@@ -179,7 +180,7 @@ func (c *client) dialUDP() Session {
return
nil
}
conn
,
err
=
net
.
DialUDP
(
"udp"
,
localAddr
,
peerAddr
)
if
err
==
nil
&&
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
if
err
==
nil
&&
gxnet
.
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
conn
.
Close
()
err
=
errSelfConnect
}
...
...
@@ -229,7 +230,7 @@ func (c *client) dialWS() Session {
}
conn
,
_
,
err
=
dialer
.
Dial
(
c
.
addr
,
nil
)
log
.
Infof
(
"websocket.dialer.Dial(addr:%s) = error:%+v"
,
c
.
addr
,
err
)
if
err
==
nil
&&
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
if
err
==
nil
&&
gxnet
.
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
conn
.
Close
()
err
=
errSelfConnect
}
...
...
@@ -306,7 +307,7 @@ func (c *client) dialWSS() Session {
return
nil
}
conn
,
_
,
err
=
dialer
.
Dial
(
c
.
addr
,
nil
)
if
err
==
nil
&&
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
if
err
==
nil
&&
gxnet
.
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
conn
.
Close
()
err
=
errSelfConnect
}
...
...
go.mod
View file @
1ff11223
module github.com/dubbogo/getty
require (
github.com/dubbogo/gost v1.
1.1
github.com/dubbogo/gost v1.
5.2
github.com/golang/snappy v0.0.1
github.com/gorilla/websocket v1.4.0
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.10.0
...
...
go.sum
View file @
1ff11223
...
...
@@ -4,6 +4,8 @@ github.com/dubbogo/gost v1.0.1-0.20190706005735-65c3ecbba418 h1:7OsAjhWpX0m6o1b/
github.com/dubbogo/gost v1.0.1-0.20190706005735-65c3ecbba418/go.mod h1:R7wZm1DrmrKGr50mBZVcg6C9ekG8aL5hP+sgWcIDwQg=
github.com/dubbogo/gost v1.1.1 h1:JCM7vx5edPIjDA5ovJTuzEEXuw2t7xLyrlgi2mi5jHI=
github.com/dubbogo/gost v1.1.1/go.mod h1:R7wZm1DrmrKGr50mBZVcg6C9ekG8aL5hP+sgWcIDwQg=
github.com/dubbogo/gost v1.5.2 h1:ri/03971hdpnn3QeCU+4UZgnRNGDXLDGDucR/iozZm8=
github.com/dubbogo/gost v1.5.2/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
...
...
@@ -15,6 +17,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
...
...
@@ -28,3 +31,5 @@ golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
server.go
View file @
1ff11223
...
...
@@ -25,6 +25,7 @@ import (
import
(
"github.com/gorilla/websocket"
perrors
"github.com/pkg/errors"
"github.com/dubbogo/gost/net"
)
var
(
...
...
@@ -213,7 +214,7 @@ func (s *server) accept(newSession NewSessionCallback) (Session, error) {
if
err
!=
nil
{
return
nil
,
perrors
.
WithStack
(
err
)
}
if
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
if
gxnet
.
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
log
.
Warnf
(
"conn.localAddr{%s} == conn.RemoteAddr"
,
conn
.
LocalAddr
()
.
String
(),
conn
.
RemoteAddr
()
.
String
())
return
nil
,
errSelfConnect
}
...
...
session.go
View file @
1ff11223
...
...
@@ -22,6 +22,7 @@ import (
import
(
gxbytes
"github.com/dubbogo/gost/bytes"
gxcontext
"github.com/dubbogo/gost/context"
gxsync
"github.com/dubbogo/gost/sync"
gxtime
"github.com/dubbogo/gost/time"
"github.com/gorilla/websocket"
...
...
@@ -86,7 +87,7 @@ type session struct {
done
chan
struct
{}
// attribute
attrs
*
ValuesContext
attrs
*
gxcontext
.
ValuesContext
// goroutines sync
grNum
int32
...
...
@@ -108,7 +109,7 @@ func newSession(endPoint EndPoint, conn Connection) *session {
done
:
make
(
chan
struct
{}),
wait
:
pendingDuration
,
attrs
:
NewValuesContext
(
nil
),
attrs
:
gxcontext
.
NewValuesContext
(
nil
),
rDone
:
make
(
chan
struct
{}),
}
...
...
@@ -149,7 +150,7 @@ func (s *session) Reset() {
s
.
done
=
make
(
chan
struct
{})
s
.
period
=
period
s
.
wait
=
pendingDuration
s
.
attrs
=
NewValuesContext
(
nil
)
s
.
attrs
=
gxcontext
.
NewValuesContext
(
nil
)
s
.
rDone
=
make
(
chan
struct
{})
s
.
grNum
=
0
...
...
utils.go
deleted
100644 → 0
View file @
193d5d02
package
getty
import
(
"context"
"net"
"strings"
)
// refers from https://github.com/facebookgo/grace/blob/master/gracenet/net.go#L180:6
func
IsSameAddr
(
a1
,
a2
net
.
Addr
)
bool
{
if
a1
.
Network
()
!=
a2
.
Network
()
{
return
false
}
a1s
:=
a1
.
String
()
a2s
:=
a2
.
String
()
if
a1s
==
a2s
{
return
true
}
// This allows for ipv6 vs ipv4 local addresses to compare as equal. This
// scenario is common when listening on localhost.
const
ipv6prefix
=
"[::]"
a1s
=
strings
.
TrimPrefix
(
a1s
,
ipv6prefix
)
a2s
=
strings
.
TrimPrefix
(
a2s
,
ipv6prefix
)
const
ipv4prefix
=
"0.0.0.0"
a1s
=
strings
.
TrimPrefix
(
a1s
,
ipv4prefix
)
a2s
=
strings
.
TrimPrefix
(
a2s
,
ipv4prefix
)
return
a1s
==
a2s
}
var
(
defaultCtxKey
int
=
1
)
type
Values
struct
{
m
map
[
interface
{}]
interface
{}
}
func
(
v
Values
)
Get
(
key
interface
{})
(
interface
{},
bool
)
{
i
,
b
:=
v
.
m
[
key
]
return
i
,
b
}
func
(
c
Values
)
Set
(
key
interface
{},
value
interface
{})
{
c
.
m
[
key
]
=
value
}
func
(
c
Values
)
Delete
(
key
interface
{})
{
delete
(
c
.
m
,
key
)
}
type
ValuesContext
struct
{
context
.
Context
}
func
NewValuesContext
(
ctx
context
.
Context
)
*
ValuesContext
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
return
&
ValuesContext
{
Context
:
context
.
WithValue
(
ctx
,
defaultCtxKey
,
Values
{
m
:
make
(
map
[
interface
{}]
interface
{})},
),
}
}
func
(
c
*
ValuesContext
)
Get
(
key
interface
{})
(
interface
{},
bool
)
{
return
c
.
Context
.
Value
(
defaultCtxKey
)
.
(
Values
)
.
Get
(
key
)
}
func
(
c
*
ValuesContext
)
Delete
(
key
interface
{})
{
c
.
Context
.
Value
(
defaultCtxKey
)
.
(
Values
)
.
Delete
(
key
)
}
func
(
c
*
ValuesContext
)
Set
(
key
interface
{},
value
interface
{})
{
c
.
Context
.
Value
(
defaultCtxKey
)
.
(
Values
)
.
Set
(
key
,
value
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment