Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
autoradio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ale
autoradio
Commits
37eb65e6
Commit
37eb65e6
authored
5 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Use net.IPs to build Endpoints
parent
ad23ce5a
No related branches found
Branches containing commit
No related tags found
1 merge request
!1
v2.0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
coordination/presence/presence.go
+4
-12
4 additions, 12 deletions
coordination/presence/presence.go
coordination/presence/registration.go
+18
-12
18 additions, 12 deletions
coordination/presence/registration.go
proto/presence.go
+9
-25
9 additions, 25 deletions
proto/presence.go
with
31 additions
and
49 deletions
coordination/presence/presence.go
+
4
−
12
View file @
37eb65e6
...
...
@@ -2,10 +2,8 @@ package presence
import
(
"context"
"crypto/rand"
"encoding/hex"
"log"
mrand
"math/rand"
"math/rand"
"sync"
"github.com/golang/protobuf/proto"
...
...
@@ -15,12 +13,6 @@ import (
pb
"git.autistici.org/ale/autoradio/proto"
)
func
getUniqueToken
()
string
{
var
b
[
16
]
byte
rand
.
Read
(
b
[
:
])
// nolint
return
hex
.
EncodeToString
(
b
[
:
])
}
// EndpointSet is a container of Endpoints that is synchronizable with
// the contents of a presence tree as created by Register().
type
EndpointSet
struct
{
...
...
@@ -123,7 +115,7 @@ func (n *EndpointSet) RandomEndpoint() *pb.Endpoint {
if
len
(
n
.
epl
)
==
0
{
return
nil
}
return
n
.
epl
[
m
rand
.
Intn
(
len
(
n
.
epl
))]
return
n
.
epl
[
rand
.
Intn
(
len
(
n
.
epl
))]
}
// RandomEndpointExcluding returns a randomly selected Endpoint except
...
...
@@ -145,13 +137,13 @@ func (n *EndpointSet) RandomEndpointExcluding(name string) *pb.Endpoint {
}
// Is the excluded element not in the list?
if
found
<
0
{
return
n
.
epl
[
m
rand
.
Intn
(
l
)]
return
n
.
epl
[
rand
.
Intn
(
l
)]
}
// Is the list empty once we exclude the item?
if
l
==
1
{
return
nil
}
i
:=
m
rand
.
Intn
(
l
-
1
)
i
:=
rand
.
Intn
(
l
-
1
)
if
i
>=
found
{
i
++
}
...
...
This diff is collapsed.
Click to expand it.
coordination/presence/registration.go
+
18
−
12
View file @
37eb65e6
...
...
@@ -2,6 +2,9 @@ package presence
import
(
"context"
"crypto/rand"
"encoding/hex"
"net"
"strings"
pb
"git.autistici.org/ale/autoradio/proto"
...
...
@@ -14,17 +17,17 @@ import (
// endpoint registration. Addresses without ports will use the
// DefaultPort.
type
EndpointRegistration
struct
{
Prefix
string
Addrs
string
Default
Port
int
Prefix
string
Addrs
[]
net
.
IP
Port
int
}
// NewRegistration creates a new EndpointRegistration.
func
NewRegistration
(
prefix
,
addrs
string
,
port
int
)
EndpointRegistration
{
func
NewRegistration
(
prefix
string
,
addrs
[]
net
.
IP
,
port
int
)
EndpointRegistration
{
return
EndpointRegistration
{
Prefix
:
prefix
,
Addrs
:
addrs
,
Default
Port
:
port
,
Prefix
:
prefix
,
Addrs
:
addrs
,
Port
:
port
,
}
}
...
...
@@ -33,11 +36,8 @@ func NewRegistration(prefix, addrs string, port int) EndpointRegistration {
func
Register
(
ctx
context
.
Context
,
session
*
concurrency
.
Session
,
name
string
,
regs
...
EndpointRegistration
)
([]
*
pb
.
Endpoint
,
error
)
{
var
endpoints
[]
*
pb
.
Endpoint
for
_
,
r
:=
range
regs
{
ep
,
err
:=
pb
.
ParseEndpoint
(
name
,
r
.
Addrs
,
r
.
DefaultPort
)
if
err
!=
nil
{
return
nil
,
err
}
if
err
=
registerEndpoint
(
ctx
,
session
,
r
.
Prefix
,
ep
);
err
!=
nil
{
ep
:=
pb
.
NewEndpointWithIPAndPort
(
name
,
r
.
Addrs
,
r
.
Port
)
if
err
:=
registerEndpoint
(
ctx
,
session
,
r
.
Prefix
,
ep
);
err
!=
nil
{
return
nil
,
err
}
endpoints
=
append
(
endpoints
,
ep
)
...
...
@@ -61,3 +61,9 @@ func registerEndpoint(ctx context.Context, session *concurrency.Session, prefix
)
return
err
}
func
getUniqueToken
()
string
{
var
b
[
16
]
byte
rand
.
Read
(
b
[
:
])
// nolint
return
hex
.
EncodeToString
(
b
[
:
])
}
This diff is collapsed.
Click to expand it.
proto/presence.go
+
9
−
25
View file @
37eb65e6
package
autoradio
import
(
"errors"
"fmt"
"net"
"strconv"
"strings"
)
// ParseEndpoint creates an Endpoint with the specified name and
// address. If the address does not specify a port, defaultPort is
// used.
func
ParseEndpoint
(
name
,
s
string
,
defaultPort
int
)
(
*
Endpoint
,
error
)
{
if
s
==
""
{
return
nil
,
errors
.
New
(
"empty endpoint spec"
)
// NewEndpointWithIPAndPort creates an Endpoint with the specified IP
// address and port.
func
NewEndpointWithIPAndPort
(
name
string
,
ips
[]
net
.
IP
,
port
int
)
*
Endpoint
{
ep
:=
Endpoint
{
Name
:
name
,
}
addrs
:=
strings
.
Split
(
s
,
","
)
var
ep
Endpoint
ep
.
Name
=
name
for
_
,
addr
:=
range
addrs
{
host
,
port
,
err
:=
net
.
SplitHostPort
(
addr
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"parsing '%s': %v"
,
addr
,
err
)
}
if
port
==
""
{
port
=
strconv
.
Itoa
(
defaultPort
)
}
if
ip
:=
net
.
ParseIP
(
host
);
ip
==
nil
{
return
nil
,
fmt
.
Errorf
(
"parsing '%s': bad IP address"
,
host
)
}
ep
.
Addrs
=
append
(
ep
.
Addrs
,
net
.
JoinHostPort
(
host
,
port
))
sport
:=
strconv
.
Itoa
(
port
)
for
_
,
ip
:=
range
ips
{
ep
.
Addrs
=
append
(
ep
.
Addrs
,
net
.
JoinHostPort
(
ip
.
String
(),
sport
))
}
return
&
ep
,
nil
return
&
ep
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment