Go: efficiently handling fragmented data received via TCP -


i writing small tcp-server suppose read data, parse receiving data (dynamic length frames) , handling these frames. consider following code:

func clienthandler(conn net.conn) {     buf := make([]byte, 4096)     n, err := conn.read(buf)     if err != nil {         fmt.println("error reading:", err.error())         return     }     fmt.println("received ", n, " bytes of data =", string(buf))     handledata(buf) 

this pretty essence of code is. read x bytes empty buffer , handle data.

the problem occurs when:

  1. a frame of data bigger buffer trying read tcp connection. in case cannot handle data until receive rest (but receving buffer occupied previous data).
  2. when buffer contains 1 , half frame of data , need handle first frame, while keeping incomplete frame later... in case need remove handled part buffer, , move incomplete part there room remainder of frame.

both scenarios require reallocation , copying of data, may perhaps costly operation? furthermore, have no ideas on how handle frames larger buffer except expanding buffer... ever-increasing buffer may perhaps lead performance issues , denial of service. last, not least, not know standard library of golang enough know if there package built explicitly handling these situations.

so questions are:

  1. is there best practice handling these situations?
  2. is there golang package or of me?

thank you.

byte slices should support pretty optimized resizing (i.e. reserving more bytes needed, not copying if can, growing exponentially, copying code not written in go part of runtime, etc).

so can use append , see how works.

another, more idiomatic approach, using bufio.reader wrap connection, , handle automatically. i've used in tcp server i've written , extremely fast. do:

r := bufio.newreader(conn) 

and can either read until delimiter or given amount of bytes if know in advance.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -