File: redis.c

package info (click to toggle)
proftpd-mod-proxy 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,476 kB
  • sloc: ansic: 43,512; perl: 43,487; sh: 3,479; makefile: 248
file content (1181 lines) | stat: -rw-r--r-- 32,469 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
/*
 * ProFTPD - mod_proxy reverse datastore implementation
 * Copyright (c) 2012-2020 TJ Saunders
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 *
 * As a special exemption, TJ Saunders and other respective copyright holders
 * give permission to link this program with OpenSSL, and distribute the
 * resulting executable, without including the source code for OpenSSL in the
 * source distribution.
 */

#include "mod_proxy.h"
#include "redis.h"

#include "proxy/conn.h"
#include "proxy/reverse.h"
#include "proxy/reverse/redis.h"
#include "proxy/random.h"
#include "proxy/tls.h"
#include "proxy/ftp/ctrl.h"

/* PerHost/PerUser/PerGroup table limits */
#define PROXY_REVERSE_REDIS_PERHOST_MAX_ENTRIES		8192
#define PROXY_REVERSE_REDIS_PERUSER_MAX_ENTRIES		8192
#define PROXY_REVERSE_REDIS_PERGROUP_MAX_ENTRIES	8192

static array_header *redis_backends = NULL;

static const char *trace_channel = "proxy.reverse.redis";

static void *redis_prefix = NULL;
static size_t redis_prefixsz = 0;

static char *make_key(pool *p, const char *policy, unsigned int vhost_id,
    const char *name) {
  char *key;
  size_t keysz;

  /* It's 21 characters for "proxy_reverse:" and ":vhost#", and one for the
   * trailing NUL.  Allocate enough room for a large vhost ID, e.g.
   * optimistically in the thousands.
   */
  keysz = 22 + 6 + strlen(policy);
  if (name != NULL) {
    keysz += strlen(name) + 1;
  }

  key = pcalloc(p, keysz + 1);

  if (name == NULL) {
    snprintf(key, keysz, "proxy_reverse:%s:vhost#%u", policy, vhost_id);

  } else {
    snprintf(key, keysz, "proxy_reverse:%s:vhost#%u:%s", policy, vhost_id,
      name);
  }

  return key;
}

static unsigned int str2hash(const void *key, size_t keysz) {
  unsigned int i = 0;
  size_t sz = !keysz ? strlen((const char *) key) : keysz;

  while (sz--) {
    const char *k = key;
    unsigned int c;

    pr_signals_handle();

    c = k[sz];
    i = (i * 33) + c;
  }

  return i;
}

/* Given an index into the array_header of backend pconns, return the URI
 * for the indexed conn.
 */
static const char *backend_uri_by_idx(int idx) {
  const struct proxy_conn *pconn;

  if (redis_backends == NULL) {
    errno = EPERM;
    return NULL;
  }

  if (idx < 0) {
    errno = EPERM;
    return NULL;
  }

  pconn = ((struct proxy_conn **) redis_backends->elts)[idx];
  return proxy_conn_get_uri(pconn);
}

/* Redis List helpers */
static array_header *redis_get_list_backend_uris(pool *p,
    pr_redis_t *redis, const char *policy, unsigned int vhost_id,
    const char *name) {
  int res;
  pool *tmp_pool;
  char *key;
  array_header *backend_uris, *values = NULL, *valueszs = NULL;

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, policy, vhost_id, name);

  res = pr_redis_list_getall(tmp_pool, redis, &proxy_module, key, &values,
    &valueszs);
  if (res < 0) {
    int xerrno = errno;

    pr_trace_msg(trace_channel, 3,
      "error retrieving %s Redis entries using key '%s': %s", policy, key,
      strerror(xerrno));

    destroy_pool(tmp_pool);
    errno = xerrno;
    return NULL;
  }

  backend_uris = copy_array_str(p, values);
  destroy_pool(tmp_pool);

  return backend_uris;
}

static int redis_set_list_backends(pool *p, pr_redis_t *redis,
    const char *policy, unsigned int vhost_id, const char *name,
    array_header *backends) {
  register unsigned int i;
  int res = 0, xerrno;
  pool *tmp_pool;
  char *key;
  array_header *backend_uris, *backend_uriszs;

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, policy, vhost_id, name);
  backend_uris = make_array(tmp_pool, 0, sizeof(char *));
  backend_uriszs = make_array(tmp_pool, 0, sizeof(size_t));

  for (i = 0; i < backends->nelts; i++) {
    struct proxy_conn *pconn;
    const char *backend_uri;
    size_t backend_urisz;

    pconn = ((struct proxy_conn **) backends->elts)[i];
    backend_uri = proxy_conn_get_uri(pconn);
    *((char **) push_array(backend_uris)) = pstrdup(tmp_pool, backend_uri);

    backend_urisz = strlen(backend_uri);
    *((size_t *) push_array(backend_uriszs)) = backend_urisz;

    pr_trace_msg(trace_channel, 19, "adding %s list backend #%u: '%.*s'",
      policy, i+1, (int) backend_urisz, backend_uri);
  }

  res = pr_redis_list_setall(redis, &proxy_module, key, backend_uris,
    backend_uriszs);
  xerrno = errno;

  if (res < 0) {
    pr_trace_msg(trace_channel, 6,
      "error adding %s Redis entries: %s", policy, strerror(xerrno));
  }

  destroy_pool(tmp_pool);
  errno = xerrno;
  return res;
}

/* Redis Sorted Set helpers */

static int redis_set_sorted_set_backends(pool *p, pr_redis_t *redis,
    const char *policy, unsigned int vhost_id, array_header *backends,
    float init_score) {
  register unsigned int i;
  int res = 0, xerrno;
  pool *tmp_pool;
  char *key;
  array_header *backend_uris, *backend_uriszs, *backend_scores;

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, policy, vhost_id, NULL);
  backend_uris = make_array(tmp_pool, 0, sizeof(char *));
  backend_uriszs = make_array(tmp_pool, 0, sizeof(size_t));
  backend_scores = make_array(tmp_pool, 0, sizeof(float));

  for (i = 0; i < backends->nelts; i++) {
    struct proxy_conn *pconn;
    const char *backend_uri;
    size_t backend_urisz;

    pconn = ((struct proxy_conn **) backends->elts)[i];
    backend_uri = proxy_conn_get_uri(pconn);
    *((char **) push_array(backend_uris)) = pstrdup(tmp_pool, backend_uri);

    backend_urisz = strlen(backend_uri);
    *((size_t *) push_array(backend_uriszs)) = backend_urisz;

    *((float *) push_array(backend_scores)) = init_score;

    pr_trace_msg(trace_channel, 19,
      "adding %s sorted set backend #%u: '%.*s' (%0.3f)", policy, i+1,
      (int) backend_urisz, backend_uri, init_score);
  }

  res = pr_redis_sorted_set_setall(redis, &proxy_module, key, backend_uris,
    backend_uriszs, backend_scores);
  xerrno = errno;

  if (res < 0) {
    pr_trace_msg(trace_channel, 6,
      "error adding %s Redis entries: %s", policy, strerror(xerrno));
  }

  destroy_pool(tmp_pool);
  errno = xerrno;
  return res;
}

/* ProxyReverseConnectPolicy: Shuffle */

/* The implementation of shuffling here requires two Redis lists, the A and B
 * lists.  URIs are consumed (via random selection) from the A list and added
 * to the B list, until the A list is empty.  At which point, the B list is
 * renamed to the A list, and we start again.
 */

static int reverse_redis_shuffle_init(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, array_header *backends) {
  return redis_set_list_backends(p, redis, "Shuffle", vhost_id, "A", backends);
}

static long reverse_redis_shuffle_next(pool *p, pr_redis_t *redis,
    unsigned int vhost_id) {
  int res, xerrno;
  pool *tmp_pool;
  char *akey, *bkey;
  const char *val;
  size_t valsz;
  uint64_t count = 0;
  long idx;

  tmp_pool = make_sub_pool(p);
  akey = make_key(tmp_pool, "Shuffle", vhost_id, "A");
  res = pr_redis_list_count(redis, &proxy_module, akey, &count);
  xerrno = errno;

  if (res < 0) {
    if (xerrno != ENOENT) {
      pr_trace_msg(trace_channel, 6,
        "error getting count of Redis list '%s': %s", akey, strerror(xerrno));

      destroy_pool(tmp_pool);
      errno = xerrno;
      return -1;
    }

    count = 0;
  }

  if (count == 0) {
    res = reverse_redis_shuffle_init(p, redis, vhost_id, redis_backends);
    xerrno = errno;

    if (res < 0) {
      destroy_pool(tmp_pool);
      errno = xerrno;
      return -1;
    }

    count = redis_backends->nelts;
  }

  idx = proxy_random_next(0, count-1);

/* XXX Now we want to remove that backend URI from the A list, and add it to
 * the B list.
 */

  val = backend_uri_by_idx((int) idx);
  xerrno = errno;

  if (val == NULL) {
    destroy_pool(tmp_pool);
    errno = xerrno;
    return -1;
  }

  valsz = strlen(val);
  res = pr_redis_list_delete(redis, &proxy_module, akey, (void *) val, valsz);
  xerrno = errno;

  if (res < 0) {
    destroy_pool(tmp_pool);
    errno = xerrno;
    return -1;
  }

  bkey = make_key(tmp_pool, "Shuffle", vhost_id, "B");

  res = pr_redis_list_append(redis, &proxy_module, bkey, (void *) val, valsz);
  xerrno = errno;

  if (res < 0) {
    destroy_pool(tmp_pool);
    errno = xerrno;
    return -1;
  }

  /* If count is one, it means we just removed the last backend from the A
   * list.  Thus rename the B list to be the A list.
   */
  if (count == 1) {
    res = pr_redis_rename(redis, &proxy_module, bkey, akey);
    xerrno = errno;

    if (res < 0) {
      pr_trace_msg(trace_channel, 3,
        "error renaming Shuffle key '%s' to '%s': %s", bkey, akey,
        strerror(xerrno));
      idx = -1;
    }
  }

  destroy_pool(tmp_pool);
  errno = xerrno;
  return idx;
}

static int reverse_redis_shuffle_used(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx) {
  /* Nothing to do here. */
  return 0;
}

/* ProxyReverseConnectPolicy: RoundRobin */

static int reverse_redis_roundrobin_init(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, array_header *backends) {
  return redis_set_list_backends(p, redis, "RoundRobin", vhost_id, NULL,
    backends);
}

static const struct proxy_conn *reverse_redis_roundrobin_next(pool *p,
    pr_redis_t *redis, unsigned int vhost_id) {
  int res, xerrno;
  pool *tmp_pool;
  char *key, *backend_uri = NULL;
  size_t backend_urisz = 0;
  const struct proxy_conn *pconn;

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, "RoundRobin", vhost_id, NULL);

  res = pr_redis_list_rotate(tmp_pool, redis, &proxy_module, key,
    (void **) &backend_uri, &backend_urisz);
  xerrno = errno;

  if (res < 0) {
    pr_trace_msg(trace_channel, 3,
      "error rotating RoundRobin Redis list using key '%s': %s", key,
      strerror(xerrno));

    destroy_pool(tmp_pool);
    errno = xerrno;
    return NULL;
  }

  pconn = proxy_conn_create(p, pstrndup(tmp_pool, backend_uri, backend_urisz),
    0);
  xerrno = errno;

  if (pconn == NULL) {
    pr_trace_msg(trace_channel, 3,
      "error creating proxy connection from URI '%.*s': %s",
      (int) backend_urisz, backend_uri, strerror(xerrno));
  }

  destroy_pool(tmp_pool);

  errno = xerrno;
  return pconn;
}

static int reverse_redis_roundrobin_used(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx) {
  /* Nothing to do here. */
  return 0;
}

/* ProxyReverseConnectPolicy: LeastConns */

static int reverse_redis_leastconns_init(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, array_header *backends) {
  return redis_set_sorted_set_backends(p, redis, "LeastConns", vhost_id,
    backends, 0.0);
}

static const struct proxy_conn *reverse_redis_leastconns_next(pool *p,
    pr_redis_t *redis, unsigned int vhost_id) {
  int res, xerrno;
  pool *tmp_pool;
  char *key;
  array_header *vals = NULL, *valszs = NULL;
  const struct proxy_conn *pconn = NULL;

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, "LeastConns", vhost_id, NULL);

  res = pr_redis_sorted_set_getn(tmp_pool, redis, &proxy_module, key, 0, 1,
    &vals, &valszs, PR_REDIS_SORTED_SET_FL_ASC);
  xerrno = errno;

  if (res == 0) {
    char *backend_uri;

    backend_uri = ((char **) vals->elts)[0];
    pconn = proxy_conn_create(p, backend_uri, 0);
  }

  destroy_pool(tmp_pool);
  errno = xerrno;
  return pconn;
}

static int reverse_redis_leastconns_used(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx) {
  /* Nothing to do here. */
  return 0;
}

static int reverse_redis_leastconns_update(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx, int conn_incr, long connect_ms) {
  int res, xerrno;
  pool *tmp_pool;
  char *key;
  const char *val;
  float score;
  size_t valsz;

  val = backend_uri_by_idx(backend_idx);
  if (val == NULL) {
    return -1;
  }

  valsz = strlen(val);

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, "LeastConns", vhost_id, NULL);

  score = (float) conn_incr;
  res = pr_redis_sorted_set_set(redis, &proxy_module, key, (void *) val, valsz,
    score);
  xerrno = errno;

  destroy_pool(tmp_pool);
  errno = xerrno;
  return res;
}

/* ProxyReverseConnectPolicy: LeastResponseTime */

/* Note: "least response time" is determined by calculating the following
 * for each backend server:
 *
 *  N = connection count * connect time (ms)
 *
 * and choosing the backend with the lowest value for N.  If there are no
 * backend servers with connect time values, choose the one with the lowest
 * connection count.
 */

static int reverse_redis_leastresponsetime_init(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, array_header *backends) {
  return redis_set_sorted_set_backends(p, redis, "LeastResponseTime", vhost_id,
    backends, 0.0);
}

static const struct proxy_conn *reverse_redis_leastresponsetime_next(pool *p,
    pr_redis_t *redis, unsigned int vhost_id) {
  int res, xerrno;
  pool *tmp_pool;
  char *key;
  array_header *vals = NULL, *valszs = NULL;
  const struct proxy_conn *pconn = NULL;

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, "LeastResponseTime", vhost_id, NULL);

  res = pr_redis_sorted_set_getn(tmp_pool, redis, &proxy_module, key, 0, 1,
    &vals, &valszs, PR_REDIS_SORTED_SET_FL_ASC);
  xerrno = errno;

  if (res == 0) {
    char *backend_uri;

    backend_uri = ((char **) vals->elts)[0];
    pconn = proxy_conn_create(p, backend_uri, 0);
  }

  destroy_pool(tmp_pool);
  errno = xerrno;
  return pconn;
}

static int reverse_redis_leastresponsetime_used(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx) {
  /* TODO: anything to do here? */
  return 0;
}

static int reverse_redis_leastresponsetime_update(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx, int conn_incr, long connect_ms) {
  int res, xerrno;
  pool *tmp_pool;
  char *key;
  const char *val;
  float score;
  size_t valsz;

  val = backend_uri_by_idx(backend_idx);
  if (val == NULL) {
    return -1;
  }

  valsz = strlen(val);

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, "LeastResponseTime", vhost_id, NULL);

  score = (float) conn_incr;
  if (connect_ms > 0) {
    score *= (float) connect_ms;
  }

  res = pr_redis_sorted_set_set(redis, &proxy_module, key, (void *) val, valsz,
    score);
  xerrno = errno;

  destroy_pool(tmp_pool);
  errno = xerrno;
  return res;
}

/* ProxyReverseConnectPolicy: PerUser */

static array_header *reverse_redis_peruser_get(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, const char *user) {
  return redis_get_list_backend_uris(p, redis, "PerUser", vhost_id, user);
}

static const struct proxy_conn *reverse_redis_peruser_init(pool *p,
    pr_redis_t *redis, unsigned int vhost_id, const char *user) {
  int res;
  const struct proxy_conn *pconn = NULL;
  struct proxy_conn **conns = NULL;
  unsigned int backend_count;
  array_header *backends;

  backends = proxy_reverse_pername_backends(p, user, TRUE);
  if (backends == NULL) {
    return NULL;
  }

  /* Store these backends for later use. */
  res = redis_set_list_backends(p, redis, "PerUser", vhost_id, user, backends);
  if (res < 0) {
    return NULL;
  }

  backend_count = backends->nelts;
  conns = backends->elts;

  if (backend_count == 1) {
    pconn = conns[0];

  } else {
    size_t user_len;
    unsigned int h;
    int idx;

    user_len = strlen(user);
    h = str2hash(user, user_len);
    idx = h % backend_count;

    pconn = conns[idx];
  }

  return pconn;
}

static const struct proxy_conn *reverse_redis_peruser_next(pool *p,
    pr_redis_t *redis, unsigned int vhost_id, const char *user) {
  array_header *backend_uris;
  const struct proxy_conn *pconn = NULL;

  pconn = reverse_redis_peruser_init(p, redis, vhost_id, user);
  if (pconn == NULL &&
      errno != ENOENT) {
    backend_uris = reverse_redis_peruser_get(p, redis, vhost_id, user);
    if (backend_uris != NULL) {
      char **vals;

      vals = backend_uris->elts;
      pconn = proxy_conn_create(p, vals[0], 0);
    }
  }

  if (pconn != NULL) {
    return pconn;
  }

  (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
    "error preparing PerUser Redis entries for user '%s': %s", user,
    strerror(ENOENT));
  errno = EPERM;
  return NULL;
}

static int reverse_redis_peruser_used(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx) {
  /* Nothing to do here. */
  return 0;
}

/* ProxyReverseConnectPolicy: PerGroup */

static array_header *reverse_redis_pergroup_get(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, const char *group) {
  return redis_get_list_backend_uris(p, redis, "PerGroup", vhost_id, group);
}

static const struct proxy_conn *reverse_redis_pergroup_init(pool *p,
    pr_redis_t *redis, unsigned int vhost_id, const char *group) {
  int res;
  const struct proxy_conn *pconn = NULL;
  struct proxy_conn **conns = NULL;
  unsigned int backend_count;
  array_header *backends;

  backends = proxy_reverse_pername_backends(p, group, FALSE);
  if (backends == NULL) {
    return NULL;
  }

  /* Store these backends for later use. */
  res = redis_set_list_backends(p, redis, "PerGroup", vhost_id, group,
    backends);
  if (res < 0) {
    return NULL;
  }

  backend_count = backends->nelts;
  conns = backends->elts;

  if (backend_count == 1) {
    pconn = conns[0];

  } else {
    size_t group_len;
    unsigned int h;
    int idx;

    group_len = strlen(group);
    h = str2hash(group, group_len);
    idx = h % backend_count;

    pconn = conns[idx];
  }

  return pconn;
}

static const struct proxy_conn *reverse_redis_pergroup_next(pool *p,
    pr_redis_t *redis, unsigned int vhost_id, const char *group) {
  array_header *backend_uris;
  const struct proxy_conn *pconn = NULL;

  pconn = reverse_redis_pergroup_init(p, redis, vhost_id, group);
  if (pconn == NULL &&
      errno != ENOENT) {
    backend_uris = reverse_redis_pergroup_get(p, redis, vhost_id, group);
    if (backend_uris != NULL) {
      char **vals;

      vals = backend_uris->elts;
      pconn = proxy_conn_create(p, vals[0], 0);
    }
  }

  if (pconn != NULL) {
    return pconn;
  }

  (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
    "error preparing PerGroup Redis entries for group '%s': %s", group,
    strerror(ENOENT));
  errno = EPERM;
  return NULL;
}

static int reverse_redis_pergroup_used(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx) {
  /* Nothing to do here. */
  return 0;
}

/* ProxyReverseConnectPolicy: PerHost */

static array_header *reverse_redis_perhost_get(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, const pr_netaddr_t *addr) {
  return redis_get_list_backend_uris(p, redis, "PerHost", vhost_id,
    pr_netaddr_get_ipstr(addr));
}

static const struct proxy_conn *reverse_redis_perhost_init(pool *p,
    pr_redis_t *redis, unsigned int vhost_id, array_header *backends,
    const pr_netaddr_t *addr) {
  int res;
  const struct proxy_conn *pconn = NULL;
  struct proxy_conn **conns;
  const char *ip;

  ip = pr_netaddr_get_ipstr(addr);

  /* Store these backends for later use. */
  res = redis_set_list_backends(p, redis, "PerHost", vhost_id, ip, backends);
  if (res < 0) {
    return NULL;
  }

  conns = backends->elts;

  if (backends->nelts == 1) {
    pconn = conns[0];

  } else {
    size_t iplen;
    unsigned int h;
    int idx;

    iplen = strlen(ip);
    h = str2hash(ip, iplen);
    idx = h % backends->nelts;

    pconn = conns[idx];
  }

  return pconn;
}

static const struct proxy_conn *reverse_redis_perhost_next(pool *p,
    pr_redis_t *redis, unsigned int vhost_id, const pr_netaddr_t *addr) {
  array_header *backend_uris;
  const struct proxy_conn *pconn = NULL;

  backend_uris = reverse_redis_perhost_get(p, redis, vhost_id, addr);
  if (backend_uris == NULL &&
      errno == ENOENT) {

    /* This can happen the very first time; perform an on-demand discovery
     * of the backends for this host, and try again.
     */
    pconn = reverse_redis_perhost_init(p, redis, vhost_id, redis_backends,
      addr);
    if (pconn == NULL) {
      (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
        "error preparing PerHost Redis entries for host '%s': %s",
        pr_netaddr_get_ipstr(addr), strerror(errno));
      errno = EPERM;
      return NULL;
    }

  } else {
    char **vals;

    vals = backend_uris->elts;
    pconn = proxy_conn_create(p, vals[0], 0);
  }

  return pconn;
}

static int reverse_redis_perhost_used(pool *p, pr_redis_t *redis,
    unsigned int vhost_id, int backend_idx) {
  /* Nothing to do here. */
  return 0;
}

/* ProxyReverseServers API/handling */

static int reverse_redis_policy_init(pool *p, void *redis, int policy_id,
    unsigned int vhost_id, array_header *backends, unsigned long opts) {
  int res = 0, xerrno;

  switch (policy_id) {
    case PROXY_REVERSE_CONNECT_POLICY_RANDOM:
    case PROXY_REVERSE_CONNECT_POLICY_PER_USER:
    case PROXY_REVERSE_CONNECT_POLICY_PER_HOST:
      /* No preparation needed at this time. */
      break;

    case PROXY_REVERSE_CONNECT_POLICY_ROUND_ROBIN:
      if (backends != NULL) {
        res = reverse_redis_roundrobin_init(p, redis, vhost_id, backends);
        if (res < 0) {
          xerrno = errno;
          pr_log_debug(DEBUG3, MOD_PROXY_VERSION
            ": error preparing %s Redis entries: %s",
            proxy_reverse_policy_name(policy_id), strerror(xerrno));
          errno = xerrno;
        }
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_SHUFFLE:
      if (backends != NULL) {
        res = reverse_redis_shuffle_init(p, redis, vhost_id, backends);
        if (res < 0) {
          xerrno = errno;
          pr_log_debug(DEBUG3, MOD_PROXY_VERSION
            ": error preparing %s Redis entries: %s",
            proxy_reverse_policy_name(policy_id), strerror(xerrno));
          errno = xerrno;
        }
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_LEAST_CONNS:
      if (backends != NULL) {
        res = reverse_redis_leastconns_init(p, redis, vhost_id, backends);
        if (res < 0) {
          xerrno = errno;
          pr_log_debug(DEBUG3, MOD_PROXY_VERSION
            ": error preparing %s Redis entries: %s",
            proxy_reverse_policy_name(policy_id), strerror(xerrno));
          errno = xerrno;
        }
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_LEAST_RESPONSE_TIME:
      if (backends != NULL) {
        res = reverse_redis_leastresponsetime_init(p, redis, vhost_id,
          backends);
        if (res < 0) {
          xerrno = errno;
          pr_log_debug(DEBUG3, MOD_PROXY_VERSION
            ": error preparing %s Redis entries: %s",
            proxy_reverse_policy_name(policy_id), strerror(xerrno));
          errno = xerrno;
        }
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_PER_GROUP:
      if (!(opts & PROXY_OPT_USE_REVERSE_PROXY_AUTH)) {
        pr_log_pri(PR_LOG_NOTICE, MOD_PROXY_VERSION
          ": PerGroup ProxyReverseConnectPolicy requires the "
          "UseReverseProxyAuth ProxyOption");
        errno = EPERM;
        res = -1;
      }
      break;

    default:
      errno = EINVAL;
      res = -1;
      break;
  }

  return res;
}

static const struct proxy_conn *reverse_redis_policy_next_backend(pool *p,
    void *redis, int policy_id, unsigned int vhost_id,
    array_header *default_backends, const void *policy_data, int *backend_id) {
  const struct proxy_conn *pconn = NULL;
  struct proxy_conn **conns = NULL;
  int idx = -1, nelts = 0;

  if (redis_backends != NULL) {
    conns = redis_backends->elts;
    nelts = redis_backends->nelts;
  }

  if (proxy_reverse_policy_is_sticky(policy_id) != TRUE) {
    if (conns == NULL &&
        default_backends != NULL &&
        redis_backends == NULL) {
      conns = default_backends->elts;
      nelts = default_backends->nelts;
    }
  }

  switch (policy_id) {
    case PROXY_REVERSE_CONNECT_POLICY_RANDOM:
      idx = (int) proxy_random_next(0, nelts-1);
      if (idx >= 0) {
        pr_trace_msg(trace_channel, 11, "%s policy: selected index %d of %u",
          proxy_reverse_policy_name(policy_id), idx, nelts-1);
        pconn = conns[idx];
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_ROUND_ROBIN:
      pconn = reverse_redis_roundrobin_next(p, redis, vhost_id);
      if (pconn != NULL) {
        pr_trace_msg(trace_channel, 11,
          "%s policy: selected backend '%.100s'",
          proxy_reverse_policy_name(policy_id), proxy_conn_get_uri(pconn));
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_SHUFFLE:
      idx = (int) reverse_redis_shuffle_next(p, redis, vhost_id);
      if (idx >= 0) {
        pr_trace_msg(trace_channel, 11, "%s policy: selected index %d of %u",
          proxy_reverse_policy_name(policy_id), idx, nelts-1);
        pconn = conns[idx];
      }
      break;

      break;

    case PROXY_REVERSE_CONNECT_POLICY_LEAST_CONNS:
      pconn = reverse_redis_leastconns_next(p, redis, vhost_id);
      if (pconn != NULL) {
        pr_trace_msg(trace_channel, 11,
          "%s policy: selected backend '%.100s'",
          proxy_reverse_policy_name(policy_id), proxy_conn_get_uri(pconn));
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_LEAST_RESPONSE_TIME:
      pconn = reverse_redis_leastresponsetime_next(p, redis, vhost_id);
      if (pconn != NULL) {
        pr_trace_msg(trace_channel, 11,
          "%s policy: selected backend '%.100s'",
          proxy_reverse_policy_name(policy_id), proxy_conn_get_uri(pconn));
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_PER_USER:
      pconn = reverse_redis_peruser_next(p, redis, vhost_id, policy_data);
      if (pconn != NULL) {
        pr_trace_msg(trace_channel, 11,
          "%s policy: selected backend '%.100s' for user '%s'",
          proxy_reverse_policy_name(policy_id), proxy_conn_get_uri(pconn),
          (const char *) policy_data);
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_PER_GROUP:
      pconn = reverse_redis_pergroup_next(p, redis, vhost_id, policy_data);
      if (pconn != NULL) {
        pr_trace_msg(trace_channel, 11,
          "%s policy: selected backend '%.100s' for user '%s'",
          proxy_reverse_policy_name(policy_id), proxy_conn_get_uri(pconn),
          (const char *) policy_data);
      }
      break;

    case PROXY_REVERSE_CONNECT_POLICY_PER_HOST:
      pconn = reverse_redis_perhost_next(p, redis, vhost_id,
        session.c->remote_addr);
      if (pconn != NULL) {
        pr_trace_msg(trace_channel, 11,
          "%s policy: selected backend '%.100s' for host '%s'",
          proxy_reverse_policy_name(policy_id), proxy_conn_get_uri(pconn),
          pr_netaddr_get_ipstr(session.c->remote_addr));
      }
      break;

    default:
      errno = ENOSYS;
      return NULL;
  }

  if (backend_id != NULL) {
    *backend_id = idx;
  }

  return pconn;
}

static int reverse_redis_policy_update_backend(pool *p, void *redis,
    int policy_id, unsigned vhost_id, int backend_idx, int conn_incr,
    long connect_ms) {
  int res = 0, xerrno = 0;

  /* If our ReverseConnectPolicy is one of PerUser, PerGroup, or PerHost,
   * we can skip this step: those policies do not use the connection count/time.
   * This also helps avoid contention under load for these policies.
   */
  if (proxy_reverse_policy_is_sticky(policy_id) == TRUE) {
    pr_trace_msg(trace_channel, 17,
      "sticky policy %s does not require updates, skipping",
      proxy_reverse_policy_name(policy_id));

    return 0;
  }

  /* TODO: Right now, we simply overwrite/track the very latest connect ms.
   * But this could unfairly skew policies such as LeastResponseTime, as when
   * the server in question had higher latency for that particular connection,
   * due to e.g. OCSP response cache expiration.
   *
   * Another way would to be average the given connect ms with the previous
   * one (if present), and store that.  Something to ponder for the future.
   */

  switch (policy_id) {
    case PROXY_REVERSE_CONNECT_POLICY_LEAST_CONNS:
      res = reverse_redis_leastconns_update(p, redis, vhost_id, backend_idx,
        conn_incr, connect_ms);
      xerrno = errno;
      break;

    case PROXY_REVERSE_CONNECT_POLICY_LEAST_RESPONSE_TIME:
      res = reverse_redis_leastresponsetime_update(p, redis, vhost_id,
        backend_idx, conn_incr, connect_ms);
      xerrno = errno;
      break;

    default:
      res = 0;
      break;
  }

  errno = xerrno;
  return res;
}

static int reverse_redis_policy_used_backend(pool *p, void *redis,
    int policy_id, unsigned int vhost_id, int backend_idx) {
  int res, xerrno = 0;

  switch (policy_id) {
    case PROXY_REVERSE_CONNECT_POLICY_RANDOM:
      res = 0;
      break;

    case PROXY_REVERSE_CONNECT_POLICY_ROUND_ROBIN:
      res = reverse_redis_roundrobin_used(p, redis, vhost_id, backend_idx);
      xerrno = errno;
      break;

    case PROXY_REVERSE_CONNECT_POLICY_SHUFFLE:
      res = reverse_redis_shuffle_used(p, redis, vhost_id, backend_idx);
      xerrno = errno;
      break;

    case PROXY_REVERSE_CONNECT_POLICY_LEAST_CONNS:
      res = reverse_redis_leastconns_used(p, redis, vhost_id, backend_idx);
      xerrno = errno;
      break;

    case PROXY_REVERSE_CONNECT_POLICY_LEAST_RESPONSE_TIME:
      res = reverse_redis_leastresponsetime_used(p, redis, vhost_id,
        backend_idx);
      xerrno = errno;
      break;

    case PROXY_REVERSE_CONNECT_POLICY_PER_USER:
      res = reverse_redis_peruser_used(p, redis, vhost_id, backend_idx);
      xerrno = errno;
      break;

    case PROXY_REVERSE_CONNECT_POLICY_PER_GROUP:
      res = reverse_redis_pergroup_used(p, redis, vhost_id, backend_idx);
      xerrno = errno;
      break;

    case PROXY_REVERSE_CONNECT_POLICY_PER_HOST:
      res = reverse_redis_perhost_used(p, redis, vhost_id, backend_idx);
      xerrno = errno;
      break;

    default:
      xerrno = ENOSYS;
      res = -1;
      break;
  }

  errno = xerrno;
  return res;
}

static void *reverse_redis_init(pool *p, const char *tables_path, int flags) {
  int xerrno = 0;
  pr_redis_t *redis;

  (void) tables_path;
  (void) flags;

  redis = pr_redis_conn_new(p, &proxy_module, 0);
  xerrno = errno;

  if (redis == NULL) {
    (void) pr_log_pri(PR_LOG_NOTICE, MOD_PROXY_VERSION
      ": error opening Redis connection: %s", strerror(xerrno));
    errno = xerrno;
    return NULL;
  }

  (void) pr_redis_conn_set_namespace(redis, &proxy_module, redis_prefix,
    redis_prefixsz);
  return redis;
}

static int reverse_redis_close(pool *p, void *redis) {
  if (redis != NULL) {
    if (pr_redis_conn_close(redis) < 0) {
      (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
        "error closing Redis connection: %s", strerror(errno));
    }
  }

  return 0;
}

static void *reverse_redis_open(pool *p, const char *tables_path,
    array_header *backends) {
  int xerrno = 0;
  pr_redis_t *redis;

  redis = pr_redis_conn_new(p, &proxy_module, 0);
  xerrno = errno;

  if (redis == NULL) {
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "error opening Redis connection: %s", strerror(xerrno));
    errno = xerrno;
    return NULL;
  }

  (void) pr_redis_conn_set_namespace(redis, &proxy_module, redis_prefix,
    redis_prefixsz);
  redis_backends = backends;
  return redis;
}

int proxy_reverse_redis_as_datastore(struct proxy_reverse_datastore *ds,
    void *ds_data, size_t ds_datasz) {

  if (ds == NULL) {
    errno = EINVAL;
    return -1;
  }

  ds->policy_init = reverse_redis_policy_init;
  ds->policy_next_backend = reverse_redis_policy_next_backend;
  ds->policy_used_backend = reverse_redis_policy_used_backend;
  ds->policy_update_backend = reverse_redis_policy_update_backend;
  ds->init = reverse_redis_init;
  ds->open = reverse_redis_open;
  ds->close = reverse_redis_close;

  redis_prefix = ds_data;
  redis_prefixsz = ds_datasz;

  return 0;
}