Project

General

Profile

Feature #1505 ยป mod_compress_load_avg.patch

origo, 2009-06-11 09:29

View differences:

src/mod_compress.c (working copy)
50 50
	array  *compress;
51 51
	off_t   compress_max_filesize; /** max filesize in kb */
52 52
	int     allowed_encodings;
53
        buffer *compress_max_loadavg;
53 54
} plugin_config;
54 55

  
55 56
typedef struct {
56 57
	PLUGIN_DATA;
57 58
	buffer *ofn;
58 59
	buffer *b;
60
        time_t last_load_ts;
61
        unsigned short load_status;
59 62

  
60 63
	plugin_config **config_storage;
61 64
	plugin_config conf;
......
70 73

  
71 74
	p->ofn = buffer_init();
72 75
	p->b = buffer_init();
76
	p->last_load_ts = 0;
77
	p->load_status = 0;
73 78

  
74 79
	return p;
75 80
}
......
93 98

  
94 99
			array_free(s->compress);
95 100
			buffer_free(s->compress_cache_dir);
101
			buffer_free(s->compress_max_loadavg);
96 102

  
97 103
			free(s);
98 104
		}
......
162 168
		{ "compress.filetype",              NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION },
163 169
		{ "compress.max-filesize",          NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION },
164 170
		{ "compress.allowed-encodings",     NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION },
171
		{ "compress.max-load-avg",          NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },
165 172
		{ NULL,                             NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
166 173
	};
167 174

  
......
176 183
		s->compress = array_init();
177 184
		s->compress_max_filesize = 0;
178 185
		s->allowed_encodings = 0;
186
		s->compress_max_loadavg = buffer_init();
179 187

  
180 188
		cv[0].destination = s->compress_cache_dir;
181 189
		cv[1].destination = s->compress;
182 190
		cv[2].destination = &(s->compress_max_filesize);
183 191
		cv[3].destination = encodings_arr; /* temp array for allowed encodings list */
192
		cv[4].destination = s->compress_max_loadavg;
184 193

  
185 194
		p->config_storage[i] = s;
186 195

  
......
612 621

  
613 622
	PATCH_OPTION(compress_cache_dir);
614 623
	PATCH_OPTION(compress);
615
	PATCH_OPTION(compress_max_filesize);
624
 	PATCH_OPTION(compress_max_filesize);
616 625
	PATCH_OPTION(allowed_encodings);
626
 	PATCH_OPTION(compress_max_loadavg);
617 627

  
618 628
	/* skip the first, the global context */
619 629
	for (i = 1; i < srv->config_context->used; i++) {
......
635 645
				PATCH_OPTION(compress_max_filesize);
636 646
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("compress.allowed-encodings"))) {
637 647
				PATCH_OPTION(allowed_encodings);
648
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("compress.max-load-avg"))) {
649
				PATCH_OPTION(compress_max_loadavg);
638 650
			}
639 651
		}
640 652
	}
......
675 687

  
676 688
	mod_compress_patch_connection(srv, con, p);
677 689

  
690

  
691
	/* Do not compress if load is too high */
692
	if(srv->cur_ts - p->last_load_ts > 10) {
693
	  FILE *f;
694
	  char buf[64];
695
	  if (con->conf.log_request_handling) TRACE("TRACE: %s", "Checking load average");
696

  
697
	  p->last_load_ts = srv->cur_ts;
698

  
699
	  if((f = fopen("/proc/loadavg", "r")) != NULL) {
700
	    if(fgets(buf, sizeof(buf), f) != NULL) {
701
	      double load_avg=0, max_load_avg;
702
	      char *endp;
703
	      load_avg = strtod(buf, &endp);
704
	      if(load_avg > 0.0) {
705
		max_load_avg = strtod(SAFE_BUF_STR(p->conf.compress_max_loadavg), &endp);
706
		if(load_avg > max_load_avg) {
707
		  p->load_status = 1;
708
		  if (con->conf.log_request_handling) TRACE("ATTENTION: Load average too high (%f) %s", load_avg, ". Pausing compression.");
709
		  return HANDLER_GO_ON;
710
		} else
711
		  p->load_status = 0;
712
	      }
713
	    }
714
	    fclose(f);
715
	  }
716

  
717
	} else if(p->load_status != 0) {
718
	  if (con->conf.log_request_handling) TRACE("ATTENTION: %s", "Load too high, skipping compress");
719
	  return HANDLER_GO_ON;
720
	}
721

  
722

  
678 723
	max_fsize = p->conf.compress_max_filesize;
679 724

  
680 725
	if (HANDLER_GO_ON != stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
    (1-1/1)