Documentation
¶
Index ¶
- Variables
- func JSPipe(w io.Writer) msgp.Unmarshaler
- func ListenAndServe(network string, laddr string, h Handler) error
- func ListenAndServeTLS(network, laddr string, certFile, keyFile string, h Handler) error
- func RegisterName(m Method, name string)
- func Serve(l net.Listener, h Handler) error
- func ServeConn(c net.Conn, h Handler)
- type Client
- type Handler
- type Method
- type Request
- type ResponseError
- type ResponseWriter
- type RouteTable
- type Status
- type String
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosed is returns when a call is attempted // on a closed client. ErrClosed = errors.New("synapse: client is closed") // ErrTimeout is returned when a server // doesn't respond to a request before // the client's timeout scavenger can // free the waiting goroutine ErrTimeout = errors.New("synapse: the server didn't respond in time") // ErrTooLarge is returned when the message // size is larger than 65,535 bytes. ErrTooLarge = errors.New("synapse: message body too large") )
Functions ¶
func JSPipe ¶
func JSPipe(w io.Writer) msgp.Unmarshaler
JSPipe is a decoder that can be used to translate a messagepack object directly into JSON as it is being decoded.
For example, you can trivially print the response to a request as JSON to stdout by writing something like the following:
client.Call("name", in, synapse.JSPipe(os.Stdout))
func ListenAndServe ¶
ListenAndServe opens up a network listener on the provided network and local address and begins serving with the provided handler. ListenAndServe blocks until there is a fatal listener error.
func ListenAndServeTLS ¶
ListenAndServeTLS acts identically to ListenAndServe, except that it expects connections over TLS1.2 (see crypto/tls). Additionally, files containing a certificate and matching private key for the Server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate followed by the CA's certificate.
func RegisterName ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a client to a single synapse server.
func Dial ¶
Dial creates a new client by dialing the provided network and remote address. The provided timeout is used as the timeout for requests, in milliseconds.
func DialTLS ¶
DialTLS acts identically to Dial, except that it dials the connection over TLS using the provided *tls.Config.
func NewClient ¶
NewClient creates a new client from an existing net.Conn. Timeout is the maximum time, in milliseconds, to wait for server responses before sending an error to the caller. NewClient fails with an error if it cannot ping the server over the connection.
type Handler ¶
type Handler interface { // ServeCall handles a synapse request and // writes a response. It should be safe to // call ServeCall from multiple goroutines. // Both the request and response interfaces // become invalid after the call is returned; // no implementation of ServeCall should // maintain a reference to either object // after the function returns. ServeCall(req Request, res ResponseWriter) }
Handler is the interface used to register server response callbacks.
type Request ¶
type Request interface { // Method returns the // request method. Method() Method // RemoteAddr returns the remote // address that made the request. RemoteAddr() net.Addr // Decode reads the data of the request // into the argument. Decode(msgp.Unmarshaler) error // IsNil returns whether or not // the body of the request is 'nil'. IsNil() bool }
Request is the interface that Handlers use to interact with requests.
type ResponseError ¶
ResponseError is the type of error returned by the client when the server sends a response with ResponseWriter.Error()
type ResponseWriter ¶
type ResponseWriter interface { // Error sets an error status // to be returned to the caller, // along with an explanation // of the error. Error(Status, string) // Send sets the body to be // returned to the caller. Send(msgp.Marshaler) error }
A ResponseWriter is the interface through which Handlers write responses. Handlers can either return a body (through Send) or an error (through Error); whichever call is made first should 'win'. Subsequent calls to either method should no-op.
type RouteTable ¶
type RouteTable []Handler
RouteTable is the simplest and fastest form of routing. Request methods map directly to an index in the routing table.
Route tables should be used by defining your methods in a const-iota block, and then using those as indices in the routing table.
func (*RouteTable) ServeCall ¶
func (r *RouteTable) ServeCall(req Request, res ResponseWriter)
type Status ¶
type Status int
Status represents a response status code
const ( StatusInvalid Status = iota // zero-value for Status StatusOK // OK StatusNotFound // no handler for the request method StatusCondition // precondition failure StatusBadRequest // mal-formed request StatusNotAuthed // not authorized StatusServerError // server-side error StatusOther // other error )
These are the status codes that can be passed to a ResponseWriter as an error to send to the client.
type String ¶
type String string
String is a convenience type for reading and writing go strings to the wire.
It can be used like:
router.HandleFunc("ping", func(_ synapse.Request, res synapse.Response) { res.Send(synapse.String("pong")) })
func (String) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler