rpc/servlet_handlers.go

76 lines
2.1 KiB
Go
Raw Permalink Normal View History

2017-11-28 16:19:03 +00:00
package rpc
import (
"fmt"
"strings"
"git.loafle.net/commons_go/rpc/protocol"
cuc "git.loafle.net/commons_go/util/context"
)
2017-11-28 16:24:16 +00:00
type ServletHandlers struct {
2017-11-28 16:19:03 +00:00
// The maximum number of pending messages in the queue.
//
// The number of pending requsts should exceed the expected number
// of concurrent goroutines calling client's methods.
// Otherwise a lot of ClientError.Overflow errors may appear.
//
// Default is DefaultPendingMessages.
PendingResponses int
codecs map[string]protocol.ServerCodec
}
2017-11-28 16:24:16 +00:00
func (sh *ServletHandlers) ServletContext(parent cuc.Context) ServletContext {
2017-11-28 16:33:30 +00:00
return newServletContext(parent)
2017-11-28 16:19:03 +00:00
}
2017-11-28 16:24:16 +00:00
func (sh *ServletHandlers) Init(servletCTX ServletContext) error {
2017-11-28 16:19:03 +00:00
return nil
}
2017-11-28 16:24:16 +00:00
func (sh *ServletHandlers) Invoke(servletCTX ServletContext, requestCodec protocol.RegistryCodec) (result interface{}, err error) {
2017-12-01 07:29:32 +00:00
return nil, nil
2017-11-28 16:19:03 +00:00
}
2017-11-28 16:24:16 +00:00
func (sh *ServletHandlers) Destroy(servletCTX ServletContext) {
2017-11-28 16:19:03 +00:00
// no op
}
// RegisterCodec adds a new codec to the server.
//
// Codecs are defined to process a given serialization scheme, e.g., JSON or
// XML. A codec is chosen based on the "Content-Type" header from the request,
// excluding the charset definition.
2017-11-28 16:24:16 +00:00
func (sh *ServletHandlers) RegisterCodec(contentType string, codec protocol.ServerCodec) {
2017-11-28 16:19:03 +00:00
if nil == sh.codecs {
sh.codecs = make(map[string]protocol.ServerCodec)
}
sh.codecs[strings.ToLower(contentType)] = codec
}
2017-11-28 16:24:16 +00:00
func (sh *ServletHandlers) getCodec(contentType string) (protocol.ServerCodec, error) {
2017-11-28 16:19:03 +00:00
var codec protocol.ServerCodec
if contentType == "" && len(sh.codecs) == 1 {
// If Content-Type is not set and only one codec has been registered,
// then default to that codec.
for _, c := range sh.codecs {
codec = c
}
} else if codec = sh.codecs[strings.ToLower(contentType)]; codec == nil {
return nil, fmt.Errorf("Unrecognized Content-Type: %s", contentType)
}
return codec, nil
}
2017-11-28 16:24:16 +00:00
func (sh *ServletHandlers) GetPendingResponses() int {
2017-11-28 16:19:03 +00:00
return sh.PendingResponses
}
2017-11-28 16:24:16 +00:00
func (sh *ServletHandlers) Validate() {
2017-11-28 16:19:03 +00:00
if 0 >= sh.PendingResponses {
sh.PendingResponses = DefaultPendingResponses
}
}