0

I have an API to login to a system. It doesn't support concurrent login with the same user id (I guess due to license). However this code can be called by different processes/clients launched by different users from another system, in my case, a ClearCase trigger.

my $conn = new BuildForge::Services::Connection('ccbuildforged01', 3966);
my $token = $conn->authUser('bldforge', 'password');

I have two choices.

  1. The $token returned can be shared by different clients. So how can I persistent this $token?
  2. I have 10 license, so can create 10 users. How can I create a file based persistent stack for all client to share these user ids?

I googled a bit and found this: A single, simple file and a lock seems all you need. You push by lock,append,unlock. You pop by lock,seek,read,truncate,unlock.

Can someone give me a code sample?

  • Are you using a stack to manage user ids by popping an available id from the stack, and pushing freed ones back on the stack? – Rob Dec 21 '12 at 22:50

2 Answers2

1

I would maintain ten files (say 1.conf though 10.conf) with the user information.

To get an available user id, look for a .conf file with no corresponding .pid file (e.g. 1.pid). If you find one, try to get an exclusive lock on the file, and then create a corresponding .pid file with an exclusive lock on it. (If any of these fail, look for another file.)

When you are done, release the lock on the .conf file, then release the lock and delete the .pid file.

If you want to avoid a possible race condition, you could have a queue.lock file that you try to lock exclusively before looking for an available user id. If it's already locked, wait for the lock to be released.

Rob
  • 781
  • 5
  • 19
0
  1. Why we need the extra .pid file? Isn't lock on the .conf file enough?
  2. Using the following code, if I start two instance of this program at the same time, the 2nd one wait for the 1st to unlock, then lock the first file id01.txt. It's waiting to read. How can I ask it go to the next one if a file is locked?

    use FileHandle; use Fcntl qw(:flock);

    for ($count = 1; $count <= 8; $count++) {
    if (open SELF, "< id0$count.txt"); if (flock(SELF, LOCK_EX)) { # Exclusive lock print "Locked id0$count.txt...\n"; sleep(10); close SELF; } else { next }

    } else { next; } print "Unlocked id0$count.txt...\n"; }

Alex
  • 180
  • 2
  • 3
  • 10