1 squid是以BSD select()loop做驱动,整体由一个非阻塞的单进程实现。
2 squid各个模块。
2.1 client side soket
Here new client connections are accepted, parsed, and reply data sent.
每个连接状态信息由一个叫ConnStateData的数据结构维持,每个request state存储在clientSocketContext。
2.2 client side request
request在这里被processed。 我们决定每个请求是否通过访问控制、是否被redirected、是否为internal request创建client stream。(We determine if the request is to be redirected, if it passes access lists, and setup the initial client stream for internal requests. Temporary state for this processing is held in a clientRequestContext struct.)
2.3 client side reply
This is where we determine if the request is cache HIT, REFRESH, MISS, etc. This involves querying the store (possibly multiple times) to work through Vary lists and the list. Per-request state information is stored in the clientReplyContext structure。
2.4 client streams
这里的程序实现了一个单向的、非阻塞的pull pipeline。
2.5 server side
这里的程序负责转发cache misses 到其他servers。cache misses 可能会被转发到其他源服务器、或者其他代理cache。注意所有的转发给其他代理的请求
均以http 请求的方式发出。
2.6 storage mannager
The Storage Manager is the glue between client and server sides. Every object saved in the cache is allocated a StoreEntry structure. While the object is being accessed, it also has a MemObject structure.(每个存在cache 的object都会分配一个StoreEntry。当object is being accessed,它也会有一个叫MemObject数据结构。
squid可以快速定位cached object因为在内存里它保持了一个storeEntry的hashtable。这里涉及到LRU页面置换,它以一个双端list实现。
每个storeEntry 会被映射到一个cache_dir的目录,通过sdirn和sfilen定位。对于ufs(一种基于磁盘的文件系统),每个sfilen被转换为一个磁盘路径。
a cache swap file 由两部分构成:cache metadata(元数据),the object data。注意the object data包括所有的http reply--header and body。http reply headers 和 metadata 不一样。
当新的数据到达的时候,client-side request会给自己注册一个storeEntry。多个client可能通过一个storeEntry接收数据。
2.7 peer selection
these functiongs负责select the neighber caches作为合适的转发对象。
2.8 access control
这些函数负责通过对一些参数的判定,从而判断是否接收或者拒绝一个request。这些参数包括客户端的IP地址、request源的主机名以及request方法等等。
2.9 authentication framework
这些函数负责鉴定HTTP框架。
2.10 network communication
epoll-event驱动。这里不多说了。
2.11 file/disk IO
读写磁盘文件的模块。将网络和磁盘IO分开是一个historical的问题。磁盘IO支持多块组成的队列读写。可以归并多个磁盘block到一个写请求上。同时,也不一定每个请求都会触发写回调。
2.12 IP/FQDN cache
一个支持“名字到地址”和“地址到名字”查找的cache。这些是通过一个以名字或者地址为key的hashtable实现的。ipcache_nbgethostbyname() and fqdncache_nbgethostbyaddr()implement the non-blocking lookups. Files: ipcache.c, fqdncache.c.
2.13 cache manager
这里提供了一些缓存管理者所需要的信息。cachemgr.cgi(指南)可以通过一个web浏览器使得这些信息有效。The cache mannager提供了本质的只读access to information。它没有提供一种可以在squid运行时配置它的方法。
2.14 network measurement database
在许多情景下,squid需要知道从自己到源服务器之间的大概RTT。一个特别的用处是在peer_select算法中。为了做RTT测量,squid cache需要知道自己还是自己的邻居离源服务器更近。主要通过ping来测量。测量值以两个key值存储在database。主要的index filed是源服务器的代号。第二个域是一个拥有完整的与合适实体连接数据结构的hashtable。这使得squid在你无论是给出一个地址还是一个主机名,他都能很快测量出来。
2.15 redirectors
squid有能力去重写来自客户端的请求。在检查是否hit之前、检查完访问控制之后,被请求的URLS会被重写到一个外部重定向过程。这个程序会返回一个高度符合用户需求的URL来代替源请求的URL。
2.16 autonomous system numbers
就像支持其他访问控制元素一样,squid同样支持AS(自治系统)号。asn.c文件会把AS 号映射到一个CIDR(无类域间路由)前缀的list里面。