connectionmanager

package module
v0.0.0-...-a7e5766 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2012 License: MIT Imports: 3 Imported by: 0

README

Connection Manager

Do Not Use

This is not done at all.

This is what I wrote to learn Go. It is a package that allows goroutines to pass messages over channels, identifying the recipient by ID (or broadcasting).

I originally posted this because I was getting weird low-level crashes. It was due to multiple goroutines accessing the same map without a lock. Thanks, go-nuts!

Code Notes

Since I'm a Go beginner, if anyone has feedback in general, I'll take it.

I purposefully didn't optimize, here. I figured I'd just use a pile of language features and see how it held up.

Files

connectionmanager.go: the package file

examples/chat.go: a sample long-poll chat server that uses a connectionmanager.

examples/webchat: HTML/JS page for talking to the chat server

examples/rabbits/rabbits.go: bot chatters for hammering the server

Install and Run

See the README in the examples/chat directory.

TODO

  • Get rid of ConnectRequest? Just add new UIDs when events happen?
  • Add timeout to eliminate old connections
  • Throw away old messages if list gets large
  • Helper functions for SendMessage?
  • Allow user to turn polling off explicitly
  • Unicast message
  • Multicast message
  • Multicast groups (pub/sub-style)

Bugs

All kinds, I'm sure.

Rabbits sucks up all the connections pretty fast and starts giving Dial errors after a little bit. I'm not sure how to get around that, but my gut says that's not related to the crashes.

Documentation

Overview

Package connectionmanager provides communication between connections from different goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Information about a particular connection

type ConnectionManager

type ConnectionManager struct {
	// contains filtered or unexported fields
}

Manages connections

func New

func New() *ConnectionManager

Create a new ConnectionManager

func (*ConnectionManager) SendMessage

func (cm *ConnectionManager) SendMessage(r *Message) *Message

Sends a Message and receives a Message

to be called from other threads

func (*ConnectionManager) SetActive

func (cm *ConnectionManager) SetActive(active bool)

Start or stop a connection manager service

type Message

type Message struct {
	// Type of message
	Type MessageType

	// ID associated with this message
	Id string

	// ID of recipient
	DestId string

	// Additional payload to be passed to recipient (or broadcast)
	Payload *MessagePayload

	// Channel for response
	RChan chan *Message

	// Channel for polling
	PollChan chan *[]*Message

	// Generic field for data passing
	General interface{}

	// Status for SendMessage return
	Err error
}

Messages to and from the ConnectionManager

type MessagePayload

type MessagePayload map[string]interface{}

Message payload for Message struct

type MessageType

type MessageType int32

Message type identifiers

const (
	StopRequest       MessageType = 0
	StopResponse      MessageType = 1
	ConnectRequest    MessageType = 2
	ConnectResponse   MessageType = 3
	BroadcastRequest  MessageType = 4
	BroadcastResponse MessageType = 5
	PollRequest       MessageType = 6
	PollResponse      MessageType = 7
	Broadcast         MessageType = 8
)

Directories

Path Synopsis
examples
chat
A long-polling HTTP chat server User Manager code for chat.go visible from chat.go
A long-polling HTTP chat server User Manager code for chat.go visible from chat.go
chat/rabbits
Hammer away at the chat server with fake connections
Hammer away at the chat server with fake connections

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL