Skip to content
This repository has been archived by the owner on Apr 22, 2022. It is now read-only.

Commit

Permalink
Fix golint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiro1 committed Nov 26, 2018
1 parent 1a9fb1a commit 3c51d0b
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 47 deletions.
20 changes: 14 additions & 6 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func prepareQueryString(params url.Values) string {
return strings.Join(pieces, "&")
}

// Authenticate pusher
// Authentication Authenticate pusher
// see: https://gist.github.com/mloughran/376898
//
// The signature is a HMAC SHA256 hex digest.
Expand Down Expand Up @@ -90,7 +90,7 @@ func Authentication(storage storage.Storage) func(http.Handler) http.Handler {
}
}

// Check if the application is disabled
// CheckAppDisabled Check if the application is disabled
func CheckAppDisabled(storage storage.Storage) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -117,13 +117,15 @@ func CheckAppDisabled(storage storage.Storage) func(http.Handler) http.Handler {
}
}

// PostEvents handle post events
type PostEvents struct{ storage storage.Storage }

// NewPostEvents return a new PostEvents handler
func NewPostEvents(storage storage.Storage) *PostEvents {
return &PostEvents{storage: storage}
}

// ServeHTTPC An event consists of a name and data (typically JSON) which may be sent to all subscribers to a particular channel or channels.
// ServeHTTP An event consists of a name and data (typically JSON) which may be sent to all subscribers to a particular channel or channels.
// This is conventionally known as triggering an event.
//
// The body should contain a Hash of parameters encoded as JSON where data parameter itself is JSON encoded.
Expand Down Expand Up @@ -192,13 +194,15 @@ func (h *PostEvents) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// GetChannels handle get channels
type GetChannels struct{ storage storage.Storage }

// NewGetChannels return a new GetChannels handler
func NewGetChannels(storage storage.Storage) *GetChannels {
return &GetChannels{storage: storage}
}

// Allows fetching a hash of occupied channels (optionally filtered by prefix),
// ServeHTTP Allows fetching a hash of occupied channels (optionally filtered by prefix),
// and optionally one or more attributes for each channel.
//
// Notes:
Expand Down Expand Up @@ -288,13 +292,15 @@ func (h *GetChannels) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// GetChannel handle get channel
type GetChannel struct{ storage storage.Storage }

// NewGetChannel return a new GetChannel handler
func NewGetChannel(storage storage.Storage) *GetChannel {
return &GetChannel{storage: storage}
}

// Fetch info for one channel
// ServeHTTP Fetch info for one channel
//
// Example:
// {
Expand Down Expand Up @@ -380,13 +386,15 @@ func (h *GetChannel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// GetChannelUsers handle get users from a channel
type GetChannelUsers struct{ storage storage.Storage }

// NewGetChannelUsers return a new GetChannelUsers handler
func NewGetChannelUsers(storage storage.Storage) *GetChannelUsers {
return &GetChannelUsers{storage: storage}
}

// Allowed only for presence-channels
// ServeHTTP Allowed only for presence-channels
//
// Example:
// {
Expand Down
2 changes: 1 addition & 1 deletion api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func Test_getChannels_filter_by_presence_prefix_and_user_count(t *testing.T) {
}
}

// User count only alowed in Presence channels
// User count only allowed in Presence channels
func Test_getChannels_filter_by_private_prefix_and_info_user_count(t *testing.T) {
appID := testApp.AppID

Expand Down
24 changes: 15 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"ipe/subscription"
)

// An App
// Application represents a Pusher application
type Application struct {
sync.RWMutex

Expand All @@ -38,6 +38,7 @@ type Application struct {
Stats *expvar.Map `json:"-"`
}

// NewApplication returns a new Application
func NewApplication(
name,
appID,
Expand Down Expand Up @@ -83,7 +84,7 @@ func (a *Application) Channels() []*channel.Channel {
return channels
}

// Only Presence channels
// PresenceChannels Only Presence channels
func (a *Application) PresenceChannels() []*channel.Channel {
a.RLock()
defer a.RUnlock()
Expand All @@ -99,7 +100,7 @@ func (a *Application) PresenceChannels() []*channel.Channel {
return channels
}

// Only Private channels
// PrivateChannels Only Private channels
func (a *Application) PrivateChannels() []*channel.Channel {
a.RLock()
defer a.RUnlock()
Expand All @@ -115,7 +116,7 @@ func (a *Application) PrivateChannels() []*channel.Channel {
return channels
}

// Only Public channels
// PublicChannels Only Public channels
func (a *Application) PublicChannels() []*channel.Channel {
a.RLock()
defer a.RUnlock()
Expand Down Expand Up @@ -179,7 +180,7 @@ func (a *Application) Connect(conn *connection.Connection) {
a.Stats.Add("TotalConnections", 1)
}

// Find a Connection on this Application
// FindConnection Find a Connection on this Application
func (a *Application) FindConnection(socketID string) (*connection.Connection, error) {
a.RLock()
defer a.RUnlock()
Expand All @@ -193,7 +194,7 @@ func (a *Application) FindConnection(socketID string) (*connection.Connection, e
return nil, errors.New("connection not found")
}

// DeleteChannel removes the Channel from Application
// RemoveChannel removes the Channel from Application
func (a *Application) RemoveChannel(c *channel.Channel) {
log.Infof("remove the Channel %s from Application %s", c.ID, a.Name)
a.Lock()
Expand All @@ -216,7 +217,7 @@ func (a *Application) RemoveChannel(c *channel.Channel) {
a.Stats.Add("TotalChannels", -1)
}

// Add a new Channel to this APP
// AddChannel Add a new Channel to this APP
func (a *Application) AddChannel(c *channel.Channel) {
log.Infof("adding a new Channel %s to Application %s", c.ID, a.Name)

Expand All @@ -240,7 +241,7 @@ func (a *Application) AddChannel(c *channel.Channel) {
a.Stats.Add("TotalChannels", 1)
}

// Returns a Channel from this Application
// FindOrCreateChannelByChannelID Returns a Channel from this Application
// If not found then the Channel is created and added to this Application
func (a *Application) FindOrCreateChannelByChannelID(n string) *channel.Channel {
c, err := a.FindChannelByChannelID(n)
Expand Down Expand Up @@ -270,7 +271,7 @@ func (a *Application) FindOrCreateChannelByChannelID(n string) *channel.Channel
return c
}

// Find the Channel by Channel ID
// FindChannelByChannelID Find the Channel by Channel ID
func (a *Application) FindChannelByChannelID(n string) (*channel.Channel, error) {
a.RLock()
defer a.RUnlock()
Expand All @@ -284,12 +285,16 @@ func (a *Application) FindChannelByChannelID(n string) (*channel.Channel, error)
return nil, errors.New("channel does not exists")
}

// Publish an event into the channel
// skip the ignore connection
func (a *Application) Publish(c *channel.Channel, event events.Raw, ignore string) error {
a.Stats.Add("TotalUniqueMessages", 1)

return c.Publish(event, ignore)
}

// Unsubscribe unsubscribe the given connection from the channel
// remove the channel from the application if it is empty
func (a *Application) Unsubscribe(c *channel.Channel, conn *connection.Connection) error {
err := c.Unsubscribe(conn)
if err != nil {
Expand All @@ -303,6 +308,7 @@ func (a *Application) Unsubscribe(c *channel.Channel, conn *connection.Connectio
return nil
}

// Subscribe the connection into the given channel
func (a *Application) Subscribe(c *channel.Channel, conn *connection.Connection, data string) error {
return c.Subscribe(conn, data)
}
5 changes: 4 additions & 1 deletion app/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (a *Application) TriggerChannelOccupiedHook(c *channel.Channel) {
}
}

// channel_vacated
// TriggerChannelVacatedHook channel_vacated
// { "name": "channel_vacated", "channel": "test_channel" }
func (a *Application) TriggerChannelVacatedHook(c *channel.Channel) {
event := newChannelVacatedHook(c)
Expand All @@ -98,6 +98,7 @@ func (a *Application) TriggerChannelVacatedHook(c *channel.Channel) {
}
}

// TriggerClientEventHook client_events
// {
// "name": "client_event",
// "channel": "name of the channel the event was published on",
Expand All @@ -121,6 +122,7 @@ func (a *Application) TriggerClientEventHook(c *channel.Channel, s *subscription
}
}

// TriggerMemberAddedHook member_added
// {
// "name": "member_added",
// "channel": "presence-your_channel_name",
Expand All @@ -136,6 +138,7 @@ func (a *Application) TriggerMemberAddedHook(c *channel.Channel, s *subscription
}
}

// TriggerMemberRemovedHook member_removed
// {
// "name": "member_removed",
// "channel": "presence-your_channel_name",
Expand Down
33 changes: 22 additions & 11 deletions channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ import (
"ipe/utils"
)

// Option constructor function for Channel
type Option func(*Channel)

// ListenerFunc listener function
type ListenerFunc func(*Channel, *subscription.Subscription)

// ClientEventListenerFunc listener for client events
type ClientEventListenerFunc func(*Channel, *subscription.Subscription, string, interface{})

// A Channel
Expand Down Expand Up @@ -51,30 +56,35 @@ func New(channelID string, options ...Option) *Channel {
return c
}

// WithMemberAddedListener appends the given ListenerFunc into the memberAddedListeners list
func WithMemberAddedListener(f ListenerFunc) func(*Channel) {
return func(c *Channel) {
c.memberAddedListeners = append(c.memberAddedListeners, f)
}
}

// WithMemberRemovedListener appends the given ListenerFunc into the memberRemovedListeners list
func WithMemberRemovedListener(f ListenerFunc) func(*Channel) {
return func(c *Channel) {
c.memberRemovedListeners = append(c.memberRemovedListeners, f)
}
}

// WithChannelOccupiedListener appends the given ListenerFunc into the channelOccupiedListeners list
func WithChannelOccupiedListener(f ListenerFunc) func(*Channel) {
return func(c *Channel) {
c.channelOccupiedListeners = append(c.channelOccupiedListeners, f)
}
}

// WithChannelVacatedListener appends the given ListenerFunc into the channelVacatedListeners list
func WithChannelVacatedListener(f ListenerFunc) func(*Channel) {
return func(c *Channel) {
c.channelVacatedListeners = append(c.channelVacatedListeners, f)
}
}

// WithClientEventListener appends the given ListenerFunc into the clientEventListeners list
func WithClientEventListener(f ClientEventListenerFunc) func(*Channel) {
return func(c *Channel) {
c.clientEventListeners = append(c.clientEventListeners, f)
Expand All @@ -95,40 +105,40 @@ func (c *Channel) Subscriptions() []*subscription.Subscription {
return subscriptions
}

// Return true if the Channel has at least one subscriber
// IsOccupied Return true if the Channel has at least one subscriber
func (c *Channel) IsOccupied() bool {
return c.TotalSubscriptions() > 0
}

// Check if the type of the Channel is presence or is private
// IsPresenceOrPrivate Check if the type of the Channel is presence or is private
func (c *Channel) IsPresenceOrPrivate() bool {
return c.IsPresence() || c.IsPrivate()
}

// Check if the type of the Channel is public
// IsPublic Check if the type of the Channel is public
func (c *Channel) IsPublic() bool {
return !c.IsPresenceOrPrivate()
}

// Check if the type of the Channel is presence
// IsPresence Check if the type of the Channel is presence
func (c *Channel) IsPresence() bool {
return utils.IsPresenceChannel(c.ID)
}

// Check if the type of the Channel is private
// IsPrivate Check if the type of the Channel is private
func (c *Channel) IsPrivate() bool {
return utils.IsPrivateChannel(c.ID)
}

// Get the total of subscribers
// TotalSubscriptions Get the total of subscribers
func (c *Channel) TotalSubscriptions() int {
c.RLock()
defer c.RUnlock()

return len(c.subscriptions)
}

// Get the total of users.
// TotalUsers Get the total of users.
func (c *Channel) TotalUsers() int {
c.RLock()
defer c.RUnlock()
Expand All @@ -142,7 +152,7 @@ func (c *Channel) TotalUsers() int {
return len(total)
}

// Add a new subscriber to the Channel
// Subscribe Add a new subscriber to the Channel
func (c *Channel) Subscribe(conn *connection.Connection, channelData string) error {
log.Infof("Subscribing %s to Channel %s", conn.SocketID, c.ID)

Expand Down Expand Up @@ -219,7 +229,7 @@ func (c *Channel) IsSubscribed(conn *connection.Connection) bool {
return exists
}

// Remove the subscriber from the Channel
// Unsubscribe Remove the subscriber from the Channel
// It destroy the Channel if the channels does not have any subscribers.
func (c *Channel) Unsubscribe(conn *connection.Connection) error {
log.Infof("unsubscribe %s from Channel %s", conn.SocketID, c.ID)
Expand Down Expand Up @@ -254,7 +264,7 @@ func (c *Channel) Unsubscribe(conn *connection.Connection) error {
return nil
}

// Publish a MemberAddedEvent to all subscriptions
// PublishMemberAddedEvent Publish a MemberAddedEvent to all subscriptions
func (c *Channel) PublishMemberAddedEvent(data string, subscription *subscription.Subscription) {
c.RLock()
defer c.RUnlock()
Expand All @@ -266,7 +276,7 @@ func (c *Channel) PublishMemberAddedEvent(data string, subscription *subscriptio
}
}

// Publish a MemberRemovedEvent to all subscriptions
// PublishMemberRemovedEvent Publish a MemberRemovedEvent to all subscriptions
func (c *Channel) PublishMemberRemovedEvent(subscription *subscription.Subscription) {
c.RLock()
defer c.RUnlock()
Expand All @@ -279,6 +289,7 @@ func (c *Channel) PublishMemberRemovedEvent(subscription *subscription.Subscript
}

// Publish messages to all Subscribers
// skip the ignore connection
func (c *Channel) Publish(event events.Raw, ignore string) error {
c.RLock()
defer c.RUnlock()
Expand Down
Loading

0 comments on commit 3c51d0b

Please sign in to comment.