-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eliminate grpc dispatch proto marshaling overhead with Codec (#5)
* changing fiber response request to byte. add new implementation of new codec * tidy up codec and interface changes for request response * fix import syntax * add default codec as part of fibercodec * patch fiber dispatcher servce method syntax * fix test syntax for method name * minor update on codec syntax Co-authored-by: ningjie.lee <ningjie.lee@gojek.com>
- Loading branch information
Showing
18 changed files
with
145 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package grpc | ||
|
||
import ( | ||
"io" | ||
|
||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
// CodecName is the name registered for the proto compressor. | ||
const codecName = "fiber" | ||
|
||
// FiberCodec is a custom codec to prevent marshaling and unmarshalling | ||
// when unnecessary, base on the inputs | ||
type FiberCodec struct { | ||
defaultCodec encoding.Codec | ||
} | ||
|
||
// Marshal will attempt to pass the request directly if it is a byte slice, | ||
// otherwise unmarshal the request proto using the default implementation | ||
func (fc *FiberCodec) Marshal(v interface{}) ([]byte, error) { | ||
b, ok := v.([]byte) | ||
if ok { | ||
return b, nil | ||
} | ||
return fc.getDefaultCodec().Marshal(v) | ||
} | ||
|
||
// Unmarshal will attempt to write the request directly if it is a writer, | ||
// otherwise unmarshal the request proto using the default implementation | ||
func (fc *FiberCodec) Unmarshal(data []byte, v interface{}) error { | ||
writer, ok := v.(io.Writer) | ||
if ok { | ||
_, err := writer.Write(data) | ||
return err | ||
} | ||
return fc.getDefaultCodec().Unmarshal(data, v) | ||
} | ||
|
||
func (*FiberCodec) Name() string { | ||
return codecName | ||
} | ||
|
||
func (fc *FiberCodec) getDefaultCodec() encoding.Codec { | ||
if fc.defaultCodec == nil { | ||
fc.defaultCodec = encoding.GetCodec("proto") | ||
} | ||
return fc.defaultCodec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.