Commit 328c516ac103f48abbd3ef2cb9514aad7e6115e5
1 parent
77d35b36
Small optimization on reading headers
Showing
1 changed file
with
60 additions
and
52 deletions
Show diff stats
calexium_lib/web/CXM_multihost_http_server.anubis
| ... | ... | @@ -499,6 +499,28 @@ define String |
| 499 | 499 | |
| 500 | 500 | |
| 501 | 501 | variable UTime t0 = utime(0,0). |
| 502 | +variable UTime t1 = utime(0,0). | |
| 503 | + | |
| 504 | +define One | |
| 505 | + accumulate_t1 | |
| 506 | + ( | |
| 507 | + UTime start | |
| 508 | + ) = | |
| 509 | + with delta = (UTime)now - start, | |
| 510 | + t1 <- delta + *t1; | |
| 511 | + unique. | |
| 512 | + | |
| 513 | +variable UTime t2 = utime(0,0). | |
| 514 | + | |
| 515 | +define One | |
| 516 | + accumulate_t2 | |
| 517 | + ( | |
| 518 | + UTime start | |
| 519 | + ) = | |
| 520 | + with delta = (UTime)now - start, | |
| 521 | + t2 <- delta + *t2; | |
| 522 | + unique. | |
| 523 | + | |
| 502 | 524 | |
| 503 | 525 | define One |
| 504 | 526 | print_delta |
| ... | ... | @@ -508,7 +530,6 @@ define One |
| 508 | 530 | println(utime_to_string((UTime)now - *t0) + " : " + txt). |
| 509 | 531 | |
| 510 | 532 | |
| 511 | - | |
| 512 | 533 | ----------------------------------- Table of Contents --------------------------------- |
| 513 | 534 | |
| 514 | 535 | *** [1] Types which are private to this file. |
| ... | ... | @@ -721,9 +742,11 @@ define ReadResult |
| 721 | 742 | |
| 722 | 743 | if *connection.read_pos < length(*connection.buffer) then |
| 723 | 744 | //println(pid + " reading from buffer (size = " + length(*connection.buffer) + ", pos = " + *connection.read_pos); |
| 745 | + //with t1_tmp = (UTime) now, | |
| 724 | 746 | with result = extract(*connection.buffer, *connection.read_pos, *connection.read_pos + size), |
| 725 | 747 | size_read = length(result), |
| 726 | 748 | connection.read_pos <- *connection.read_pos + size_read; |
| 749 | + //accumulate_t1(t1_tmp); | |
| 727 | 750 | if size > size_read |
| 728 | 751 | then |
| 729 | 752 | //println("Wanted " + size + ", read only " + size_read); |
| ... | ... | @@ -757,56 +780,28 @@ define Result(Error,Word8) |
| 757 | 780 | DenialOfService dos |
| 758 | 781 | ) = |
| 759 | 782 | //if now > dead_line then record_dubious_connection(connection,dead_line,dos) else |
| 760 | - if read(connection,1,600) is // the connection is closed after 10 minutes of inactivity | |
| 761 | - { | |
| 762 | - error then error(cannot_read_from_connection), | |
| 763 | - timeout then error(timeout(600)), | |
| 764 | - //record_dubious_connection(connection,dead_line,dos), | |
| 765 | - ok(ba) then if nth(0,ba) is | |
| 783 | + if nth(*connection.read_pos, *connection.buffer) is | |
| 784 | + { | |
| 785 | + failure then | |
| 786 | + if read(connection,1,600) is // the connection is closed after 10 minutes of inactivity | |
| 766 | 787 | { |
| 767 | - failure then error(cannot_read_from_connection), | |
| 768 | - success(c) then | |
| 769 | -// println("-" + pid + "read [" + implode([c]) + "]\t"); | |
| 770 | - ok(c) | |
| 771 | - } | |
| 772 | - }. | |
| 773 | - | |
| 774 | - | |
| 775 | -variable ByteArray read_buffer = constant_byte_array(0, 0). | |
| 776 | -variable Int32 read_offset = 0. | |
| 777 | - | |
| 778 | - define Result(Error,Word8) | |
| 779 | - read_one_byte | |
| 780 | - ( | |
| 781 | - BufferedConnection connection, | |
| 782 | - Int32 dead_line, | |
| 783 | - DenialOfService dos | |
| 784 | - ) = | |
| 785 | - if *read_offset < length(*read_buffer) then | |
| 786 | - if nth(*read_offset, *read_buffer) is | |
| 787 | - { | |
| 788 | - failure then println(pid + "nth failed)"); error(cannot_read_from_connection), | |
| 788 | + error then error(cannot_read_from_connection), | |
| 789 | + timeout then error(timeout(600)), | |
| 790 | + //record_dubious_connection(connection,dead_line,dos), | |
| 791 | + ok(ba) then if nth(0,ba) is | |
| 792 | + { | |
| 793 | + failure then error(cannot_read_from_connection), | |
| 794 | + success(c) then | |
| 795 | + // println("-" + pid + "read [" + implode([c]) + "]\t"); | |
| 796 | + ok(c) | |
| 797 | + } | |
| 798 | + }, | |
| 789 | 799 | success(c) then |
| 790 | - println("-" + pid + "read [" + implode([c]) + "]\tat " + *read_offset); | |
| 791 | - read_offset <- *read_offset + 1; | |
| 800 | + connection.read_pos <- *connection.read_pos + 1; | |
| 792 | 801 | ok(c) |
| 793 | - } | |
| 794 | - else | |
| 795 | - //if now > dead_line then record_dubious_connection(connection,dead_line,dos) else | |
| 796 | - println(pid + "len = " + length(*read_buffer)); | |
| 797 | - if read(connection, 16384, 600) is // the connection is closed after 10 minutes of inactivity | |
| 798 | - { | |
| 799 | - error then println(pid + "read failed)"); error(cannot_read_from_connection), | |
| 800 | - timeout then error(timeout(600)), | |
| 801 | - //record_dubious_connection(connection,dead_line,dos), | |
| 802 | - ok(ba) then | |
| 803 | - println(pid + "ba = " + length(ba)); | |
| 804 | - read_buffer <- ba; | |
| 805 | - read_offset <- 0; | |
| 806 | - println(pid + "rb = " + length(*read_buffer)); | |
| 807 | - read_one_byte(connection, dead_line, dos) | |
| 808 | - }. | |
| 809 | - | |
| 802 | + }. | |
| 803 | + | |
| 804 | + | |
| 810 | 805 | |
| 811 | 806 | define Result(Error,Word8) |
| 812 | 807 | next_char // reading a character (check the list first, and read on the connection |
| ... | ... | @@ -816,16 +811,27 @@ define Result(Error,Word8) |
| 816 | 811 | Int32 dead_line, |
| 817 | 812 | DenialOfService dos |
| 818 | 813 | ) = |
| 814 | + with t2_tmp = (UTime) now, | |
| 819 | 815 | if *unput_chars is |
| 820 | 816 | { |
| 821 | - [ ] then read_one_byte(connection,dead_line,dos), | |
| 817 | + [ ] then with ret = read_one_byte(connection,dead_line,dos), accumulate_t2(t2_tmp); ret, | |
| 818 | +// if read(connection.conn, 1, 600) is // the connection is closed after 10 minutes of inactivity | |
| 819 | +// { | |
| 820 | +// error then accumulate_t2(t2_tmp); println(pid + "read failed)"); error(cannot_read_from_connection), | |
| 821 | +// timeout then accumulate_t2(t2_tmp); error(timeout(600)), | |
| 822 | +// ok(ba) then if nth(0,ba) is | |
| 823 | +// { | |
| 824 | +// failure then accumulate_t2(t2_tmp); error(cannot_read_from_connection), | |
| 825 | +// success(c) then accumulate_t2(t2_tmp); | |
| 826 | +// ok(c) | |
| 827 | +// } | |
| 828 | +// }, | |
| 822 | 829 | |
| 823 | 830 | [h . t] then |
| 824 | - unput_chars <- t; | |
| 831 | + unput_chars <- t; accumulate_t2(t2_tmp); | |
| 825 | 832 | ok(h) |
| 826 | 833 | }. |
| 827 | 834 | |
| 828 | - | |
| 829 | 835 | |
| 830 | 836 | |
| 831 | 837 | |
| ... | ... | @@ -2990,7 +2996,9 @@ define One |
| 2990 | 2996 | send_answer(host_name, desc,connection.conn, request_line, headers, body, |
| 2991 | 2997 | make_generate_trust_ticket(dos)); |
| 2992 | 2998 | with duration = (UTime) now - *t0, |
| 2993 | - println("Request duration: " + utime_to_string(duration)) | |
| 2999 | + println("Request duration: " + utime_to_string(duration)); | |
| 3000 | + //println("BufferRead duration: " + utime_to_string(*t1)); | |
| 3001 | + println("next_char duration: " + utime_to_string(*t2)) | |
| 2994 | 3002 | } |
| 2995 | 3003 | } |
| 2996 | 3004 | } | ... | ... |