@@ -468,18 +468,35 @@ func (conn *Connection) dial() (err error) {
468
468
return
469
469
}
470
470
471
- func (conn * Connection ) writeAuthRequest (w * bufio.Writer , scramble []byte ) (err error ) {
472
- request := & Future {
473
- requestId : 0 ,
474
- requestCode : AuthRequestCode ,
471
+ func pack (h * smallWBuf , enc * msgpack.Encoder , reqid uint32 , req Request , res SchemaResolver ) (err error ) {
472
+ hl := h .Len ()
473
+ h .Write ([]byte {
474
+ 0xce , 0 , 0 , 0 , 0 , // Length.
475
+ 0x82 , // 2 element map.
476
+ KeyCode , byte (req .Code ()), // Request code.
477
+ KeySync , 0xce ,
478
+ byte (reqid >> 24 ), byte (reqid >> 16 ),
479
+ byte (reqid >> 8 ), byte (reqid ),
480
+ })
481
+
482
+ if err = req .Body (res , enc ); err != nil {
483
+ return
475
484
}
485
+
486
+ l := uint32 (h .Len () - 5 - hl )
487
+ h .b [hl + 1 ] = byte (l >> 24 )
488
+ h .b [hl + 2 ] = byte (l >> 16 )
489
+ h .b [hl + 3 ] = byte (l >> 8 )
490
+ h .b [hl + 4 ] = byte (l )
491
+
492
+ return
493
+ }
494
+
495
+ func (conn * Connection ) writeAuthRequest (w * bufio.Writer , scramble []byte ) (err error ) {
476
496
var packet smallWBuf
477
- err = request .pack (& packet , msgpack .NewEncoder (& packet ), func (enc * msgpack.Encoder ) error {
478
- return enc .Encode (map [uint32 ]interface {}{
479
- KeyUserName : conn .opts .User ,
480
- KeyTuple : []interface {}{string ("chap-sha1" ), string (scramble )},
481
- })
482
- })
497
+ req := newAuthRequest (conn .opts .User , string (scramble ))
498
+ err = pack (& packet , msgpack .NewEncoder (& packet ), 0 , req , conn .Schema )
499
+
483
500
if err != nil {
484
501
return errors .New ("auth: pack error " + err .Error ())
485
502
}
@@ -704,7 +721,7 @@ func (conn *Connection) reader(r *bufio.Reader, c net.Conn) {
704
721
}
705
722
}
706
723
707
- func (conn * Connection ) newFuture (requestCode int32 ) (fut * Future ) {
724
+ func (conn * Connection ) newFuture () (fut * Future ) {
708
725
fut = NewFuture ()
709
726
if conn .rlimit != nil && conn .opts .RLimitAction == RLimitDrop {
710
727
select {
@@ -720,7 +737,6 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
720
737
}
721
738
}
722
739
fut .requestId = conn .nextRequestId ()
723
- fut .requestCode = requestCode
724
740
shardn := fut .requestId & (conn .opts .Concurrency - 1 )
725
741
shard := & conn .shard [shardn ]
726
742
shard .rmut .Lock ()
@@ -769,23 +785,16 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
769
785
return
770
786
}
771
787
772
- func (conn * Connection ) sendFuture (fut * Future , body func (* msgpack.Encoder ) error ) * Future {
788
+ func (conn * Connection ) send (req Request ) * Future {
789
+ fut := conn .newFuture ()
773
790
if fut .ready == nil {
774
791
return fut
775
792
}
776
- conn .putFuture (fut , body )
777
- return fut
778
- }
779
-
780
- func (conn * Connection ) failFuture (fut * Future , err error ) * Future {
781
- if f := conn .fetchFuture (fut .requestId ); f == fut {
782
- fut .SetError (err )
783
- conn .markDone (fut )
784
- }
793
+ conn .putFuture (fut , req )
785
794
return fut
786
795
}
787
796
788
- func (conn * Connection ) putFuture (fut * Future , body func ( * msgpack. Encoder ) error ) {
797
+ func (conn * Connection ) putFuture (fut * Future , req Request ) {
789
798
shardn := fut .requestId & (conn .opts .Concurrency - 1 )
790
799
shard := & conn .shard [shardn ]
791
800
shard .bufmut .Lock ()
@@ -801,10 +810,11 @@ func (conn *Connection) putFuture(fut *Future, body func(*msgpack.Encoder) error
801
810
shard .enc = msgpack .NewEncoder (& shard .buf )
802
811
}
803
812
blen := shard .buf .Len ()
804
- if err := fut .pack (& shard .buf , shard .enc , body ); err != nil {
813
+ reqid := fut .requestId
814
+ if err := pack (& shard .buf , shard .enc , reqid , req , conn .Schema ); err != nil {
805
815
shard .buf .Trunc (blen )
806
816
shard .bufmut .Unlock ()
807
- if f := conn .fetchFuture (fut . requestId ); f == fut {
817
+ if f := conn .fetchFuture (reqid ); f == fut {
808
818
fut .SetError (err )
809
819
conn .markDone (fut )
810
820
} else if f != nil {
@@ -983,10 +993,7 @@ func (conn *Connection) nextRequestId() (requestId uint32) {
983
993
// An error is returned if the request was formed incorrectly, or failed to
984
994
// communicate by the connection, or unable to decode the response.
985
995
func (conn * Connection ) Do (req Request ) (* Response , error ) {
986
- fut , err := conn .DoAsync (req )
987
- if err != nil {
988
- return nil , err
989
- }
996
+ fut := conn .DoAsync (req )
990
997
return fut .Get ()
991
998
}
992
999
@@ -995,24 +1002,16 @@ func (conn *Connection) Do(req Request) (*Response, error) {
995
1002
// An error is returned if the request was formed incorrectly, or failed to
996
1003
// communicate by the connection, or unable to decode the response.
997
1004
func (conn * Connection ) DoTyped (req Request , result interface {}) error {
998
- fut , err := conn .DoAsync (req )
999
- if err != nil {
1000
- return err
1001
- }
1005
+ fut := conn .DoAsync (req )
1002
1006
return fut .GetTyped (result )
1003
1007
}
1004
1008
1005
1009
// DoAsync verifies, sends the request and returns a future.
1006
1010
//
1007
1011
// An error is returned if the request was formed incorrectly, or failed to
1008
1012
// create the future.
1009
- func (conn * Connection ) DoAsync (req Request ) (* Future , error ) {
1010
- bodyFunc , err := req .BodyFunc (conn .Schema )
1011
- if err != nil {
1012
- return nil , err
1013
- }
1014
- future := conn .newFuture (req .Code ())
1015
- return conn .sendFuture (future , bodyFunc ), nil
1013
+ func (conn * Connection ) DoAsync (req Request ) * Future {
1014
+ return conn .send (req )
1016
1015
}
1017
1016
1018
1017
// ConfiguredTimeout returns a timeout from connection config.
0 commit comments