AdaCore / AdaCore/aws

minor cleanup in websocket code

Open
#210 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ada
Stars
162
Forks
45
PR merge metrics
No merged PRs in 30d

Description

While reviewing code for PR 209, I noticed the following function aws-net-websocket-registry.adb:

      procedure Watch (WebSocket : Object_Class) is
      begin
         if Is_Registered (WebSocket.Id)
           and then not Watched.Contains (WebSocket.Id)
         then
            Watched.Insert (WebSocket.Id);
            Count := Count + 1;
            Signal_Socket;
         end if;

      exception
         when others =>
            Unregister (WebSocket);
            raise;
      end Watch;

It is slightly un-optimal since it needs to do two lookups when the socket is not watched yet. Perhaps something like the following would be more efficient:

      procedure Watch (WebSocket : Object_Class) is
          Inserted : Boolean;
          Pos : WebSocket_Set.Cursor;
      begin
         if Is_Registered (WebSocket.Id) then
            Watched.Insert (WebSocket.Id, Pos, Inserted);
            if Inserted then
               Count := Count + 1;
               Signal_Socket;
            end if;
        end if;

      exception
         when others =>
            --  ??? Not clear why we would unregister the socket here ? 
            --  also, it is possible that it is still in Watched at this point, and given the first test in this procedure this
            --  might be unexpected behavior.
            Unregister (WebSocket);
            raise;
      end Watch;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading aws-net-websocket-registry.adb and the Watch procedure's surrounding exception handling. Compare the proposed single-lookup insertion with the current behavior, especially whether Unregister should remain and how Watched is left on failure. Done means the lookup is not duplicated and the exception behavior is consistent.

Written by the indexing model from the issue text.

Assessment

Domain
backend, networking
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.