Use type uint8 to replace boolean fields in response
This commit is contained in:
parent
b1e7ded874
commit
ff5f211a83
@ -105,7 +105,7 @@ func (c *Client) Call(ctx context.Context, rcvr, method string, arg interface{},
|
|||||||
c.chMtx.Unlock()
|
c.chMtx.Unlock()
|
||||||
|
|
||||||
// If response is an error, return error
|
// If response is an error, return error
|
||||||
if resp.IsError {
|
if resp.Type == types.ResponseTypeError {
|
||||||
return errors.New(resp.Error)
|
return errors.New(resp.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +118,7 @@ func (c *Client) Call(ctx context.Context, rcvr, method string, arg interface{},
|
|||||||
retVal := reflect.ValueOf(ret)
|
retVal := reflect.ValueOf(ret)
|
||||||
|
|
||||||
// If response is a channel
|
// If response is a channel
|
||||||
if resp.IsChannel {
|
if resp.Type == types.ResponseTypeChannel {
|
||||||
// If return value is not a channel, return error
|
// If return value is not a channel, return error
|
||||||
if retVal.Kind() != reflect.Chan {
|
if retVal.Kind() != reflect.Chan {
|
||||||
return ErrReturnNotChannel
|
return ErrReturnNotChannel
|
||||||
@ -137,7 +137,7 @@ func (c *Client) Call(ctx context.Context, rcvr, method string, arg interface{},
|
|||||||
// For every value received from channel
|
// For every value received from channel
|
||||||
for val := range c.chs[chID] {
|
for val := range c.chs[chID] {
|
||||||
//s := time.Now()
|
//s := time.Now()
|
||||||
if val.ChannelDone {
|
if val.Type == types.ResponseTypeChannelDone {
|
||||||
// Close and delete channel
|
// Close and delete channel
|
||||||
c.chMtx.Lock()
|
c.chMtx.Lock()
|
||||||
close(c.chs[chID])
|
close(c.chs[chID])
|
||||||
|
@ -26,12 +26,18 @@ type Request struct {
|
|||||||
Arg any
|
Arg any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ResponseType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
ResponseTypeError ResponseType = iota
|
||||||
|
ResponseTypeChannel
|
||||||
|
ResponseTypeChannelDone
|
||||||
|
)
|
||||||
|
|
||||||
// Response represents a response returned by the server
|
// Response represents a response returned by the server
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
Type ResponseType
|
||||||
ID string
|
ID string
|
||||||
ChannelDone bool
|
|
||||||
IsChannel bool
|
|
||||||
IsError bool
|
|
||||||
Error string
|
Error string
|
||||||
Return any
|
Return any
|
||||||
}
|
}
|
||||||
|
@ -312,7 +312,7 @@ func (s *Server) handleConn(c codec.Codec) {
|
|||||||
// If function has created a channel
|
// If function has created a channel
|
||||||
if ctx.isChannel {
|
if ctx.isChannel {
|
||||||
// Set IsChannel to true
|
// Set IsChannel to true
|
||||||
res.IsChannel = true
|
res.Type = types.ResponseTypeChannel
|
||||||
// Overwrite return value with channel ID
|
// Overwrite return value with channel ID
|
||||||
res.Return = ctx.channelID
|
res.Return = ctx.channelID
|
||||||
|
|
||||||
@ -342,8 +342,8 @@ func (s *Server) handleConn(c codec.Codec) {
|
|||||||
|
|
||||||
codecMtx.Lock()
|
codecMtx.Lock()
|
||||||
c.Encode(types.Response{
|
c.Encode(types.Response{
|
||||||
ID: ctx.channelID,
|
Type: types.ResponseTypeChannelDone,
|
||||||
ChannelDone: true,
|
ID: ctx.channelID,
|
||||||
})
|
})
|
||||||
codecMtx.Unlock()
|
codecMtx.Unlock()
|
||||||
}()
|
}()
|
||||||
@ -361,10 +361,10 @@ func (s *Server) handleConn(c codec.Codec) {
|
|||||||
func (s *Server) sendErr(c codec.Codec, req types.Request, val any, err error) {
|
func (s *Server) sendErr(c codec.Codec, req types.Request, val any, err error) {
|
||||||
// Encode error response using codec
|
// Encode error response using codec
|
||||||
c.Encode(types.Response{
|
c.Encode(types.Response{
|
||||||
ID: req.ID,
|
Type: types.ResponseTypeError,
|
||||||
IsError: true,
|
ID: req.ID,
|
||||||
Error: err.Error(),
|
Error: err.Error(),
|
||||||
Return: val,
|
Return: val,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user