http-push: improve remote lock management
Associate the remote locks with the remote repo, add a function to check and refresh all current locks. Signed-off-by: Junio C Hamano <junkio@cox.net>
Nick Hengeveld committed
Mar 10, 2006 at 20:18 UTC
512d632cf25c7c8c32fe4d7eb96aff9d24fa247d
1 file changed
+76
-73
http-push.c
+76
-73
@@ -80,10 +80,10 @@ struct repo
80
char *url;
81
int path_len;
82
struct packed_git *packs;
83
+ struct remote_lock *locks;
84
};
85
86
static struct repo *remote = NULL;
86
-static struct remote_lock *remote_locks = NULL;
87
88
enum transfer_state {
89
NEED_PUSH,
@@ -135,7 +135,6 @@ struct remote_lock
135
char *token;
136
time_t start_time;
137
long timeout;
138
- int active;
138
int refreshing;
139
struct remote_lock *next;
140
};
@@ -311,64 +310,69 @@ static void start_move(struct transfer_request *request)
310
}
311
}
312
314
-static int refresh_lock(struct remote_lock *check_lock)
313
+static int refresh_lock(struct remote_lock *lock)
314
{
315
struct active_request_slot *slot;
316
struct slot_results results;
317
char *if_header;
318
char timeout_header[25];
319
struct curl_slist *dav_headers = NULL;
321
- struct remote_lock *lock;
322
- int time_remaining;
323
- time_t current_time;
320
+ int rc = 0;
321
325
- /* Refresh all active locks if they're close to expiring */
326
- for (lock = remote_locks; lock; lock = lock->next) {
327
- if (!lock->active)
328
- continue;
322
+ lock->refreshing = 1;
323
330
- current_time = time(NULL);
331
- time_remaining = lock->start_time + lock->timeout
332
- - current_time;
333
- if (time_remaining > LOCK_REFRESH)
334
- continue;
324
+ if_header = xmalloc(strlen(lock->token) + 25);
325
+ sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
326
+ sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
327
+ dav_headers = curl_slist_append(dav_headers, if_header);
328
+ dav_headers = curl_slist_append(dav_headers, timeout_header);
329
336
- lock->refreshing = 1;
330
+ slot = get_active_slot();
331
+ slot->results = &results;
332
+ curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
333
+ curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
334
+ curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
335
+ curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
336
+ curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
337
338
- if_header = xmalloc(strlen(lock->token) + 25);
339
- sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
340
- sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
341
- dav_headers = curl_slist_append(dav_headers, if_header);
342
- dav_headers = curl_slist_append(dav_headers, timeout_header);
338
+ if (start_active_slot(slot)) {
339
+ run_active_slot(slot);
340
+ if (results.curl_result != CURLE_OK) {
341
+ fprintf(stderr, "LOCK HTTP error %ld\n",
342
+ results.http_code);
343
+ } else {
344
+ lock->start_time = time(NULL);
345
+ rc = 1;
346
+ }
347
+ }
348
344
- slot = get_active_slot();
345
- slot->results = &results;
346
- curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
347
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
348
- curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
349
- curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
350
- curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
349
+ lock->refreshing = 0;
350
+ curl_slist_free_all(dav_headers);
351
+ free(if_header);
352
352
- if (start_active_slot(slot)) {
353
- run_active_slot(slot);
354
- if (results.curl_result != CURLE_OK) {
355
- fprintf(stderr, "Got HTTP error %ld\n", results.http_code);
356
- lock->active = 0;
357
- } else {
358
- lock->active = 1;
359
- lock->start_time = time(NULL);
353
+ return rc;
354
+}
355
+
356
+static void check_locks()
357
+{
358
+ struct remote_lock *lock = remote->locks;
359
+ time_t current_time = time(NULL);
360
+ int time_remaining;
361
+
362
+ while (lock) {
363
+ time_remaining = lock->start_time + lock->timeout -
364
+ current_time;
365
+ if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
366
+ if (!refresh_lock(lock)) {
367
+ fprintf(stderr,
368
+ "Unable to refresh lock for %s\n",
369
+ lock->url);
370
+ aborted = 1;
371
+ return;
372
}
373
}
362
-
363
- lock->refreshing = 0;
364
- curl_slist_free_all(dav_headers);
365
- free(if_header);
374
+ lock = lock->next;
375
}
367
-
368
- if (check_lock)
369
- return check_lock->active;
370
- else
371
- return 0;
376
}
377
378
static void release_request(struct transfer_request *request)
@@ -396,7 +400,7 @@ static void finish_request(struct transfer_request *request)
400
request->slot = NULL;
401
402
/* Keep locks active */
399
- refresh_lock(request->lock);
403
+ check_locks();
404
405
if (request->headers != NULL)
406
curl_slist_free_all(request->headers);
@@ -483,6 +487,9 @@ static void add_request(struct object *obj, struct remote_lock *lock)
487
struct transfer_request *request = request_queue_head;
488
struct packed_git *target;
489
490
+ /* Keep locks active */
491
+ check_locks();
492
+
493
/*
494
* Don't push the object if it's known to exist on the remote
495
* or is already in the request queue
@@ -893,7 +900,7 @@ static struct remote_lock *lock_remote(char *path, long timeout)
900
char *url;
901
char *ep;
902
char timeout_header[25];
896
- struct remote_lock *lock = remote_locks;
903
+ struct remote_lock *lock = NULL;
904
XML_Parser parser = XML_ParserCreate(NULL);
905
enum XML_Status result;
906
struct curl_slist *dav_headers = NULL;
@@ -902,18 +909,6 @@ static struct remote_lock *lock_remote(char *path, long timeout)
909
url = xmalloc(strlen(remote->url) + strlen(path) + 1);
910
sprintf(url, "%s%s", remote->url, path);
911
905
- /* Make sure the url is not already locked */
906
- while (lock && strcmp(lock->url, url)) {
907
- lock = lock->next;
908
- }
909
- if (lock) {
910
- free(url);
911
- if (refresh_lock(lock))
912
- return lock;
913
- else
914
- return NULL;
915
- }
916
-
912
/* Make sure leading directories exist for the remote ref */
913
ep = strchr(url + strlen(remote->url) + 11, '/');
914
while (ep) {
@@ -971,10 +966,7 @@ static struct remote_lock *lock_remote(char *path, long timeout)
966
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
967
968
lock = xcalloc(1, sizeof(*lock));
974
- lock->owner = NULL;
975
- lock->token = NULL;
969
lock->timeout = -1;
977
- lock->refreshing = 0;
970
971
if (start_active_slot(slot)) {
972
run_active_slot(slot);
@@ -1016,10 +1008,9 @@ static struct remote_lock *lock_remote(char *path, long timeout)
1008
lock = NULL;
1009
} else {
1010
lock->url = url;
1019
- lock->active = 1;
1011
lock->start_time = time(NULL);
1021
- lock->next = remote_locks;
1022
- remote_locks = lock;
1012
+ lock->next = remote->locks;
1013
+ remote->locks = lock;
1014
}
1015
1016
return lock;
@@ -1029,6 +1020,7 @@ static int unlock_remote(struct remote_lock *lock)
1020
{
1021
struct active_request_slot *slot;
1022
struct slot_results results;
1023
+ struct remote_lock *prev = remote->locks;
1024
char *lock_token_header;
1025
struct curl_slist *dav_headers = NULL;
1026
int rc = 0;
@@ -1050,16 +1042,29 @@ static int unlock_remote(struct remote_lock *lock)
1042
if (results.curl_result == CURLE_OK)
1043
rc = 1;
1044
else
1053
- fprintf(stderr, "Got HTTP error %ld\n",
1045
+ fprintf(stderr, "UNLOCK HTTP error %ld\n",
1046
results.http_code);
1047
} else {
1056
- fprintf(stderr, "Unable to start request\n");
1048
+ fprintf(stderr, "Unable to start UNLOCK request\n");
1049
}
1050
1051
curl_slist_free_all(dav_headers);
1052
free(lock_token_header);
1053
1062
- lock->active = 0;
1054
+ if (remote->locks == lock) {
1055
+ remote->locks = lock->next;
1056
+ } else {
1057
+ while (prev && prev->next != lock)
1058
+ prev = prev->next;
1059
+ if (prev)
1060
+ prev->next = prev->next->next;
1061
+ }
1062
+
1063
+ if (lock->owner != NULL)
1064
+ free(lock->owner);
1065
+ free(lock->url);
1066
+ free(lock->token);
1067
+ free(lock);
1068
1069
return rc;
1070
}
@@ -1597,7 +1602,7 @@ int main(int argc, char **argv)
1602
struct transfer_request *next_request;
1603
int nr_refspec = 0;
1604
char **refspec = NULL;
1600
- struct remote_lock *ref_lock;
1605
+ struct remote_lock *ref_lock = NULL;
1606
struct rev_info revs;
1607
int rc = 0;
1608
int i;
@@ -1605,10 +1610,7 @@ int main(int argc, char **argv)
1610
setup_git_directory();
1611
setup_ident();
1612
1608
- remote = xmalloc(sizeof(*remote));
1609
- remote->url = NULL;
1610
- remote->path_len = 0;
1611
- remote->packs = NULL;
1613
+ remote = xcalloc(sizeof(*remote), 1);
1614
1615
argv++;
1616
for (i = 1; i < argc; i++, argv++) {
@@ -1787,6 +1789,7 @@ int main(int argc, char **argv)
1789
if (!rc)
1790
fprintf(stderr, " done\n");
1791
unlock_remote(ref_lock);
1792
+ check_locks();
1793
}
1794
1795
cleanup: