Skip to content

Commit

Permalink
client: use ClientOption variadic instead of ClientOptions struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Aug 13, 2018
1 parent 22ad42b commit 3f68426
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 122 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test-integration:
prepush: test lint test-integration
check-api:
@cd api && api -c \
stun1.txt,stun1.14.txt,stun1.15.txt \
stun1.txt,stun1.14.txt,stun1.15.txt,stun1.16.txt \
-except except.txt \
github.com/gortc/stun
write-api:
Expand Down
1 change: 1 addition & 0 deletions api/except.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pkg github.com/gortc/stun, type ClientAgent interface, Start([12]uint8, time.Tim
pkg github.com/gortc/stun, var ErrAttrSizeInvalid error
pkg github.com/gortc/stun, type AgentOptions struct
pkg github.com/gortc/stun, type AgentOptions struct, Handler Handler
pkg github.com/gortc/stun, func NewClient(ClientOptions) (*Client, error)
8 changes: 8 additions & 0 deletions api/stun1.16.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pkg github.com/gortc/stun, func NewClient(Connection, ...ClientOption) (*Client, error)
pkg github.com/gortc/stun, func WithAgent(ClientAgent) ClientOption
pkg github.com/gortc/stun, func WithClock(Clock) ClientOption
pkg github.com/gortc/stun, func WithCollector(Collector) ClientOption
pkg github.com/gortc/stun, func WithHandler(Handler) ClientOption
pkg github.com/gortc/stun, func WithRTO(time.Duration) ClientOption
pkg github.com/gortc/stun, func WithTimeoutRate(time.Duration) ClientOption
pkg github.com/gortc/stun, type ClientOption func(*Client)
110 changes: 65 additions & 45 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,92 @@ func Dial(network, address string) (*Client, error) {
if err != nil {
return nil, err
}
return NewClient(ClientOptions{
Connection: conn,
})
}

// ClientOptions are used to initialize Client.
type ClientOptions struct {
// Connection is the only mandatory field in options. Abstracts network.
Connection Connection

// Handler is called if Agent emits the Event with TransactionID that
// is not currently registered by Client. Useful for handling
// Data indications from TURN server.
Handler Handler // default handler (if no transaction found)
// Agent is optional implementation of STUN Agent. Defaults to
// agent implementation in current package, see agent.go.
Agent ClientAgent
// RTO as defined in STUN RFC.
RTO time.Duration // defaults to 100ms
// TimeoutRate is rate passed to Collector, the minimum duration between
// two calls of collector function (efficient minimum RTO timer resolution).
TimeoutRate time.Duration // defaults to 5ms
// Collector is optional implementation of ticker which calls function on each tick.
Collector Collector // defaults to ticker collector
// Clock is optional source of current time.
// Also Clock is passed to default collector if set.
Clock Clock // defaults to calling time.Now
return NewClient(conn)
}

// ErrNoConnection means that ClientOptions.Connection is nil.
var ErrNoConnection = errors.New("no connection provided")

// ClientOption sets some client option.
type ClientOption func(c *Client)

// WithHandler sets client handler which is called if Agent emits the Event
// with TransactionID that is not currently registered by Client.
// Useful for handling Data indications from TURN server.
func WithHandler(h Handler) ClientOption {
return func(c *Client) {
c.handler = h
}
}

// WithRTO sets client RTO as defined in STUN RFC.
func WithRTO(rto time.Duration) ClientOption {
return func(c *Client) {
c.rto = int64(rto)
}
}

// WithClock sets Clock of client, the source of current time.
// Also clock is passed to default collector if set.
func WithClock(clock Clock) ClientOption {
return func(c *Client) {
c.clock = clock
}
}

// WithTimeoutRate sets RTO timer minimum resolution.
func WithTimeoutRate(d time.Duration) ClientOption {
return func(c *Client) {
c.rtoRate = d
}
}

// WithAgent sets client STUN agent.
//
// Defaults to agent implementation in current package,
// see agent.go.
func WithAgent(a ClientAgent) ClientOption {
return func(c *Client) {
c.a = a
}
}

// WithCollector rests client timeout collector, the implementation
// of ticker which calls function on each tick.
func WithCollector(coll Collector) ClientOption {
return func(c *Client) {
c.collector = coll
}
}

// NewClient initializes new Client from provided options,
// starting internal goroutines and using default options fields
// if necessary. Call Close method after using Client to release
// resources.
func NewClient(options ClientOptions) (*Client, error) {
func NewClient(conn Connection, options ...ClientOption) (*Client, error) {
const (
defaultTimeoutRate = time.Millisecond * 5
defaultRTO = time.Millisecond * 300
defaultMaxAttempts = 7
)
c := &Client{
close: make(chan struct{}),
c: options.Connection,
a: options.Agent,
collector: options.Collector,
clock: options.Clock,
rto: int64(options.RTO),
c: conn,
clock: systemClock,
rto: int64(defaultRTO),
rtoRate: defaultTimeoutRate,
t: make(map[transactionID]*clientTransaction, 100),
maxAttempts: 7,
handler: options.Handler,
maxAttempts: defaultMaxAttempts,
}
for _, o := range options {
o(c)
}
if c.c == nil {
return nil, ErrNoConnection
}
if c.a == nil {
c.a = NewAgent(nil)
}
if options.TimeoutRate == 0 {
options.TimeoutRate = defaultTimeoutRate
}
if c.rto == 0 {
c.rto = int64(defaultRTO)
}
if c.clock == nil {
c.clock = systemClock
}
if err := c.a.SetHandler(c.handleAgentCallback); err != nil {
return nil, err
}
Expand All @@ -95,7 +114,7 @@ func NewClient(options ClientOptions) (*Client, error) {
clock: c.clock,
}
}
if err := c.collector.Start(options.TimeoutRate, func(t time.Time) {
if err := c.collector.Start(c.rtoRate, func(t time.Time) {
closedOrPanic(c.a.Collect(t))
}); err != nil {
return nil, err
Expand Down Expand Up @@ -145,6 +164,7 @@ type Client struct {
c Connection
close chan struct{}
rto int64 // time.Duration
rtoRate time.Duration
maxAttempts int32
closed bool
closedMux sync.RWMutex
Expand Down
Loading

0 comments on commit 3f68426

Please sign in to comment.